Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无论iphone的任何屏幕,都可以播放动画_Iphone_Objective C - Fatal编程技术网

无论iphone的任何屏幕,都可以播放动画

无论iphone的任何屏幕,都可以播放动画,iphone,objective-c,Iphone,Objective C,我有一些动画要在某个时间点播放。时间由后台运行的某个线程决定。现在,无论用户在哪个屏幕上,我都要播放动画。这里是屏幕层次结构 登录->homescreenthread从viewdidload开始运行 用户可以从主屏幕导航到任何其他屏幕,我希望播放动画。现在我在主屏幕上有了animationviewcontroller一个membervariable,当动画完成时,我通过allocing、initing、push和popping来调用它。而且只有10%的时间是有效的。我已经尝试过performse

我有一些动画要在某个时间点播放。时间由后台运行的某个线程决定。现在,无论用户在哪个屏幕上,我都要播放动画。这里是屏幕层次结构

登录->homescreenthread从viewdidload开始运行

用户可以从主屏幕导航到任何其他屏幕,我希望播放动画。现在我在主屏幕上有了animationviewcontroller一个membervariable,当动画完成时,我通过allocing、initing、push和popping来调用它。而且只有10%的时间是有效的。我已经尝试过performselectoronmainthread,它仍然是一样的

我如何重新设计我的代码,以便在任何屏幕上播放

这是我的线程代码

[self startTimerThread];   // in viewdidload

-(void)startTimerThread
{
    homescreenthread = [[NSThread alloc] initWithTarget:self selector:@selector(setupTimerThread) object:nil];
    [homescreenthread start];
}
-(void)setupTimerThread
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSTimer* timer = [NSTimer timerWithTimeInterval:5
                                             target:self
                                           selector:@selector(findnewmessages)
                                           userInfo:nil
                                            repeats:YES];
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:timer forMode:NSRunLoopCommonModes];
    //[runLoop addTimer:timer forModes:NSRunLoopCommonModes];
    [runLoop run];
    [pool release];
}

-(void)findnewmessages
{//finding message from server here. i am using the following request
    NSData *serverReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        NSString *replyString = [[NSString alloc] initWithBytes:[serverReply bytes] length:[serverReply length] encoding: NSASCIIStringEncoding];
//我省略了很多其他代码。
}使用应用程序的代理显示动画视图

id appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.m_window addSubview:viewController.view];
如果你想在屏幕上画任何东西,也应该在主线程上画


请让我知道它是否有帮助。

详细说明为什么从主屏幕启动线程,以及在该线程上执行的操作需要在主线程上播放动画。我有一个线程,因为它不断轮询服务器,以检查后台是否有任何消息。如果它发现任何消息,它会将消息插入数据库SQLite,并应允许用户现在使用动画来查看它。您是否考虑过使用计时器,因此使用runloop而不是线程,以所需的间隔执行轮询,然后使用异步NSURLConnections?这将消除线程以及任何可能对您的动画产生负面影响的可能性。@Jasarien我已经编辑了我的帖子,请看一看。您究竟为什么要生成一个全新的线程来运行计时器?计时器可以在主线程上运行,而不会阻塞它。并使用NSURLConnection的异步方法。