Mid*_* MP 0 cabasicanimation ios performselector
我有一个imageView添加到一个视图,它呈现为modalViewController,风格水平翻转.我添加了以下代码来动画imageView.
- (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];
}
}
Run Code Online (Sandbox Code Playgroud)
我从以下方法中调用此方法viewDidLoad:
- (void)viewDidLoad
{
[self switchOnorOff];
}
Run Code Online (Sandbox Code Playgroud)
我的问题是上面的代码没有为imageView设置动画.
但是当我尝试下面的代码时,它可以工作:
[self performSelectorOnMainThread:@selector(animateTheImageview:) withObject:self.lightImage waitUntilDone:YES];
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么会出现这个问题?之间有什么区别,
[self performSelectorOnMainThread:@selector(animateTheImageview:) withObject:self.lightImage waitUntilDone:YES];
和
[self animateTheImageview:self.lightImage];
您不应该在viewDidLoad中执行任何动画,并且还要在其中调用[super viewDidLoad],因为您只是覆盖它.尝试在viewWillAppear或viewDidAppear上显示它.
然后,根据NSObject Reference,performSelectorOnMainThread:withObject:waitUntilDone:
在主线程的运行循环上对消息进行排队
因此队列中的其他消息可以在执行动画之前完成.
希望这可以帮助
| 归档时间: |
|
| 查看次数: |
2902 次 |
| 最近记录: |