[英]ViewDidLoad method in UIViewController - when does it get called?
[英]CABasicAnimation is not working when the method is called from the viewDidLoad
我将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];
}
}
我从viewDidLoad
调用此方法:
- (void)viewDidLoad
{
[self switchOnorOff];
}
我的问题是上面的代码没有为imageView设置动画。
但是,当我尝试以下代码时,它可以工作:
[self performSelectorOnMainThread:@selector(animateTheImageview:) withObject:self.lightImage waitUntilDone:YES];
我的问题是为什么这个问题正在发生? 两者之间有什么区别吗
[self performSelectorOnMainThread:@selector(animateTheImageview:) withObject:self.lightImage waitUntilDone:YES];
和
[self animateTheImageview:self.lightImage];
您不应该在viewDidLoad中执行任何动画,还应该在其中调用[super viewDidLoad],因为您只是对其进行了覆盖。 尝试在viewWillAppear或viewDidAppear上显示它。
然后,根据《 NSObject参考》 ,performSelectorOnMainThread:withObject:waitUntilDone:
将消息在主线程的运行循环上排队
因此,队列中的其他消息可能会在执行动画之前先完成。
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.