从viewDidLoad调用该方法时,CABasicAnimation不起作用

我有一个imageView添加到一个视图呈现为一个modalViewController,风格水平翻转。 我已经添加了下面的代码animationimageView。

- (void)animateTheImageview:(UIImageView*) imageViewToAnimate{     CABasicAnimation *fadeAnimation;   fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];   fadeAnimation.duration = 1.5;   fadeAnimation.repeatCount = INFINITY;   fadeAnimation.autoreverses = NO;   fadeAnimation.fromValue = [NSNumber numberWithFloat:1.0];   fadeAnimation.toValue = [NSNumber numberWithFloat:0.5];   fadeAnimation.removedOnCompletion = YES;   fadeAnimation.fillMode = kCAFillModeForwards;   [imageViewToAnimate.layer addAnimation:fadeAnimation forKey:@"animateOpacity"]; } - (void)switchOnorOff {     if (onOffSwitch.on) {         self.lightImage.image = [UIImage imageNamed:@"CFLGlow.jpg"];     [self animateTheImageview:self.lightImage];   }   else {         self.lightImage.image = [UIImage imageNamed:@"CFL-BULB.jpg"];     [self.lightImage.layer removeAllAnimations];   } } 

我从viewDidLoad调用这个方法:

 - (void)viewDidLoad { [self switchOnorOff]; } 

我的问题是上面的代码不animationimageView。

但是当我尝试下面的代码它的作品:

 [self performSelectorOnMainThread:@selector(animateTheImageview:) withObject:self.lightImage waitUntilDone:YES]; 

我的问题是为什么这个问题正在发生? 有什么区别,

[self performSelectorOnMainThread:@selector(animateTheImageview:) withObject:self.lightImage waitUntilDone:YES];

[self animateTheImageview:self.lightImage];

你不应该在viewDidLoad中执行任何animation,并且在其中调用[super viewDidLoad],因为你只是重写它而已。 尝试在viewWillAppear或viewDidAppear上显示它。

然后,根据NSObject的参考 ,performSelectorOnMainThread:withObject:waitUntilDone:

在主线程的运行循环中排队消息

所以在执行animation之前,队列中的其他消息可能会先完成。

希望这可以帮助