モーダルビューコントローラーとして表示されるビューにimageViewを追加し、スタイルを水平方向に反転させました。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];