标签:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @" 测试动画 " ;
self.view.backgroundColor = [UIColor lightGrayColor];
_myTest1 = [[UILabel alloc]initWithFrame:CGRectMake( 10 , 100 , 60 , 40 )];
_myTest1.backgroundColor = [ UIColor clearColor];
_myTest1.textAlignment = NSTextAlignmentCenter ;
_myTest1.text = @" 张" ;
_myTest1.textColor = [ UIColor whiteColor ];
[ self.view addSubview:_myTest1 ];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor redColor];
btn.frame = CGRectMake(80, 180, 80, 80);
[btn addTarget:self action:@selector(removeAnimation) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)removeAnimation{
static int tag = 0;
if (tag == 0) {
[_myTest1.layer addAnimation:[self opacityForeverAnimation:1] forKey:@"flash"];
tag++;
}else{
[_myTest1.layer removeAnimationForKey:@"flash"];
tag--;
}
}
-(CABasicAnimation *)opacityForeverAnimation:(float)time
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath : @"opacity" ]; // 必须写 opacity 才行。
animation. fromValue = [ NSNumber numberWithFloat : 1.0f ];
animation. toValue = [ NSNumber numberWithFloat : 0.1f ]; // 这是透明度。
animation. autoreverses = YES ;
animation. duration = time;
animation. repeatCount = MAXFLOAT ;
animation. removedOnCompletion = NO ;
animation. fillMode = kCAFillModeForwards ;
animation.timingFunction =[CAMediaTimingFunction functionWithName : kCAMediaTimingFunctionEaseIn]; /// 没有的话是均匀的动画。
return animation;
}
标签:
原文地址:http://www.cnblogs.com/zhanghuifeng/p/4598412.html