[CoreAnimation]View を点滅させる

2011/08/31

CoreAnimation で View を点滅させてみる。

CABasicAnimationを利用

- (void)blinkImage:(UIImageView *)target {
    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.duration = 1.0f;
    animation.autoreverses = YES;
    animation.repeatCount = HUGE_VAL;
    animation.fromValue = [NSNumber numberWithFloat:1.0f];
    animation.toValue = [NSNumber numberWithFloat:0.5f];
    [target.layer addAnimation:animation forKey:@"opacityAnimation"];
}

CABasicAnimation で opacity を設定。 無限ループは HUGE_VAL 定数を利用する。

--2012/05/19追加

CAKeyframeAnimationを利用

- (void)blinkImage:(UIImageView *)target {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    animation.duration = 1.0f;
    animation.repeatCount = HUGE_VAL;
    animation.values = [[NSArray alloc] initWithObjects:
                              [NSNumber numberWithFloat:1.0f],
                              [NSNumber numberWithFloat:0.0f],
                              [NSNumber numberWithFloat:1.0f],
                              nil];
    [target.layer addAnimation:animation forKey:@"blink"];
}