您的位置:首页 > 其它

label字体闪烁效果

2015-06-24 18:46 393 查看
- (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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: