![](/img/trans.png)
[英]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.