我有一个 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];