2013-10-19 36 views
0

因此,这里是我的代码:无法从的NSTimer的选择触发功能

- (IBAction)run:(id)sender { 

    animationPointer = 0; 

    self.animationArray = [[NSMutableArray alloc] init]; 


    UIView *allViews = [[UIView alloc] 
     initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

    for (int i = 0; i < [self.paths count]; i++) { 

     [allViews addSubview:[self createAnimation:[self.paths objectAtIndex:i]]]; 

     [self.animationArray addObject:allViews]; 
    } 

    timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(animateTurn) 
     userInfo:nil repeats:YES]; 

} 

- (void)animateTurn { 
    UIView *tempView = [self.animationArray objectAtIndex:animationPointer]; 
    [self.view addSubview:tempView]; 
    for (UIImageView *view in tempView.subviews) { 
     [[tempView.subviews objectAtIndex:animationPointer] startAnimating]; 
    } 

    animationPointer++; 

    if (animationPointer >= [self.animationArray count]) { 
     [timer invalidate]; 
    } 
} 

createAnimation回报,你所期望的,一个完全动画UIImageView。计划是每个UIView有几个,然后在UIView上动画,然后在下一个半秒后重复动画。现在每个UIView只有一个。

但是,如果在NSTimer上调用animateTurn,则不会显示动画。 如果我通过正常的[self animateTurn]调用该方法,那么它们会出现,但与NSTimerperformSelector函数均不起作用。

任何帮助(甚至更好的方法建议)?

编辑: 我要提到的功能实际上是被触发,该代码正在被包括startAnimating(与BOOL/NSLog的检查)运行。根本没有显示。

+0

您应该检查是否该方法被称为主线程。 – dasdom

回答

-1

包括你的计时器后,这条线: -

[timer fire]; 
+0

不知何故,工作。非常感谢你! – evilseto

+0

你能解释为什么这个工作或这应该做什么?这对我没有意义。 – Tricertops

+0

@imartin,你可以使用这个方法来触发一个重复计时器而不会中断它的正常开火计划。如果计时器不重复,即使其计划的起火日期尚未到达,它也会在发射后自动失效。请参阅https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html#//apple_ref/occ/instm/NSTimer/fire –

0

这是您的解决方案: -

这是我用现在的选择是工作。 :-)

if (![NSThread isMainThread]) 
    { [self performSelectorOnMainThread:@selector(selectorToRunInMainThread) withObject:nil waitUntilDone:NO]; 
    } 
-(void)selectorToRunInMainThread 
{ 
NSString *str = @"Timer event has fired"; 
NSTimer *atimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateModel:) userInfo:str repeats:YES]; 
} 
+0

这是事情。首先,即使在选择器调用的方法中,[NSthread isMainThread]也会返回true ... YET使用animateTurn调用performSelectorOnMainThread设法使其工作...但是在将动画代码移动到另一个方法并用NStimer再次调用之后, 。有些奇怪的事情正在发生。 – evilseto