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"]; }); }
相关文章推荐
- iOS开发63-__block 和 __weak的区别
- Python简单读写Excel (xlwt, xlrd, xlutils)
- 使用Genymotion安装APK出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE的解决办法
- 点击A页面链接,跳转至B页面指定位置
- 如何保障流式处理的数据一致性
- qt 版本和linux下字符编码问题
- L14.2 puppert master,agent 应用(2)
- 关于Android弹出输入法后页面需要上移的问题。
- 【主席树】POJ 2014 K-th Number
- 用InterfaceBuilder构造一个App 、 视图控制器和视图 、 控件与事件
- 华为机试——字符串中出现次数最多的子串
- 工程师应该怎样选择Arduino开发板
- Microsoft Office Excel 不能访问文件“XXXXXXXXXXXXX.xls”。 可能的原因有:
- Python的更多内容
- 预加载图像
- C#后台利用正则表达式查找匹配字符
- sublime 怎样跳出括号?
- 详解C++中的成员访问运算符和指针到成员运算符
- java的值传递与引用传递
- github项目开发协作流程图