當(dāng)ios遇上css3動(dòng)畫系列(2) --ios動(dòng)畫之flash 閃爍
這篇文章來看下flash閃爍動(dòng)畫的。
上css3動(dòng)畫代碼
@-webkit-keyframes flash {
0%, 50%, 100% {
opacity: 1;
}
25%, 75% {
opacity: 0;
}
}
依據(jù)css3代碼飒筑,寫出ios動(dòng)畫代碼
- (void)flash{
CAKeyframeAnimation* keyAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
float opacity_1 = 1;
float opacity_2 = 0;
NSMutableArray *values = [NSMutableArray array];
[values addObject:[self getNumberWithFloat:opacity_1]];
[values addObject:[self getNumberWithFloat:opacity_2]];
[values addObject:[self getNumberWithFloat:opacity_1]];
[values addObject:[self getNumberWithFloat:opacity_2]];
[values addObject:[self getNumberWithFloat:opacity_1]];
[keyAnim setValues:values];
NSMutableArray *keyTimes = [NSMutableArray array];
[keyTimes addObject:[self getNumberWithFloat:0]];
[keyTimes addObject:[self getNumberWithFloat:0.25]];
[keyTimes addObject:[self getNumberWithFloat:0.5]];
[keyTimes addObject:[self getNumberWithFloat:0.75]];
[keyTimes addObject:[self getNumberWithFloat:1.0]];
[keyAnim setKeyTimes:keyTimes];
[keyAnim setDuration:1.0];
[keyAnim setFillMode:kCAFillModeBoth];
[self.TextTest.layer addAnimation:keyAnim forKey:nil];
}
gif效果圖
使用了CABasicAnimation 動(dòng)畫來做,比較簡(jiǎn)單墓毒,opacity值的變化恕沫。
-(void)breathingLight{
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.duration = 1;
animation.repeatCount = HUGE_VALF;
animation.fromValue= [NSNumber numberWithFloat:1];
animation.toValue = [NSNumber numberWithFloat:0.2];
animation.autoreverses = YES;
[self.TextTest.layer addAnimation:animation forKey:nil];
}