从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之前,队列中的其他消息可能会先完成。
希望这可以帮助
- CAShapeLayer在animationSwift中检测触摸
- CABasicAnimation – 两个angular度之间的animation
- 如何使CABasicAnimation后的UI对象响应
- 圆弧段的笔画animation以完整笔画结束
- 为什么当我为我的CABasicAnimation设置一个较低的持续时间值时,它会跳转吗?
- 如何在iOS中的循环path上animation标签?
- 将UIButton的背景颜色从WhiteColoranimation到RedColor
- 我如何同时animationUIImageView的图像和UIImageView本身?
- animation只改变其他属性的图层属性?