您的位置:首页 > 其它

layer动画(二)

2016-01-12 16:07 183 查看

layer动画

三:点赞, button.adjustsImageWhenHighlighted = NO 取消button自带的点击效果

// 点赞
self.goodButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.goodButton.frame = CGRectMake(50, 25, 50, 50);
[self.goodButton addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
[self.goodButton setBackgroundImage:[UIImage imageNamed:@"IconGood.png"] forState:UIControlStateNormal];
[self.view addSubview:self.goodButton];

// 把button自带的点击效果取消掉, button类型要设成custom
self.goodButton.adjustsImageWhenHighlighted = NO;
self.isSelect = NO;


2.点赞的实现

// 点赞
- (void)changeImage:(UIButton *)button{
// 关键帧动画
// 用动画完成放大的效果
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
// 需要给它设置一个关键帧的值,这个值就是变化过程
// values是一个数组
animation.values = @[@(0.5), @(1.0), @(1.7)];
// 设置动画时长
animation.duration = 0.2;
// 加到button上
[self.goodButton.layer addAnimation:animation forKey:@"animation"];
// 根据状态更换图片
if (self.isSelect == NO) {
[self.goodButton setBackgroundImage:[UIImage imageNamed:@"IconHgood.png"] forState:UIControlStateNormal];
} else {
[self.goodButton setBackgroundImage:[UIImage imageNamed:@"IconGood.png"] forState:UIControlStateNormal];
}
self.isSelect = !self.isSelect;
}


四:抖动

self.myView = [[UIView alloc] initWithFrame:CGRectMake(100, 430, 150, 50)];
[self.view addSubview:self.myView];
self.myView.backgroundColor = [UIColor redColor];
// 长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.myView addGestureRecognizer:longPress];


2.长按手势里实现抖动

- (void)longPress:(UILongPressGestureRecognizer *)longPress{
// 关键帧动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
float top = M_PI / 20;
float bom = -M_PI / 20;
// 抖动的范围
animation.values = @[@(top), @(0), @(bom), @(0), @(top)];
// 设置动画的时长
animation.duration = 0.1;
// 次数
animation.repeatCount = NSIntegerMax;
[self.myView.layer addAnimation:animation forKey:@"key"];
// 停止动画
// dispatch_after的作用就是会延迟执行内容
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 在block里写停止动画的方法
[self.myView.layer removeAnimationForKey:@"key"];
});

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