第二十九篇、CoreAnimation的使用
使用的的三个步骤
1.初始化演员
2.设置好剧情
3.播放
主要类:
CALayer // 绘图部分
CABaseAnimation // 基本动画(缩放,移动)
CAKeyframeAnimation // 关键帧动画(设置动画执行的路径,常借助贝塞尔曲线)
CAAnimationGroup // 动画组(将动画添加到组,并设置Delegate监听动画结束)
常见的keypath
transform.scale = 比例轉換 transform.scale.x = 闊的比例轉換 transform.scale.y = 高的比例轉換 transform.rotation.z = 平面圖的旋轉 opacity = 透明度 margin zPosition backgroundColor 背景颜色 cornerRadius 圆角 borderWidth bounds contents contentsRect cornerRadius frame hidden mask masksToBounds opacity position shadowColor shadowOffset shadowOpacity shadowRadius
附录:一个把商品添加到购物车的抛物线动画
coreAnimation // 1.初始化演员
CALayer *layer = [[CALayer alloc]init];
layer.bounds = CGRectMake(, , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
layer.position = CGPointMake(, );
// 设置内容
//layer.contents = (id)_imageViewBtn.imageView.image.CGImage
[self.view.layer addSublayer:layer]; // 2.设定剧情
// 贝尔曲线
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:layer.position];
[movePath addQuadCurveToPoint:CGPointMake(, [UIScreen mainScreen].bounds.size.height - - )
controlPoint:CGPointMake(,)]; // 关键帧
CAKeyframeAnimation *positionnAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionnAnimation.path = movePath.CGPath;
positionnAnimation.removedOnCompletion = YES; // 基本动画(缩放)
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.sale"];
scaleAnimation.fromValue = @;
scaleAnimation.toValue = @.;
scaleAnimation.duration = .8f;
scaleAnimation.autoreverses = NO;
scaleAnimation.repeatCount = ;
// 防止动画闪烁
scaleAnimation.removedOnCompletion = NO;
scaleAnimation.fillMode = kCAFillModeForwards; scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; // 组动画
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = 0.8f;
animationGroup.autoreverses = NO;
animationGroup.repeatCount = ;
animationGroup.repeatDuration = NO;
animationGroup.fillMode = kCAFillModeForwards;
[animationGroup setAnimations:[NSArray arrayWithObjects:positionnAnimation,scaleAnimation, nil]]; // 3.播放
[layer addAnimation:animationGroup forKey:nil];
第二种实现方式:
#import "ViewController.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) @interface ViewController ()
@property (nonatomic,strong) UIBezierPath *path;
@end @implementation ViewController{
CALayer *layer;
UILabel *_cntLabel;
NSInteger _cnt;
UIImageView *_imageView;
UIButton *_btn;
} - (void)viewDidLoad {
[super viewDidLoad];
_cnt = ;
[self setUI]; }
-(void)setUI
{
UIColor *customColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:1.0f];
//
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.frame = CGRectMake(, SCREEN_HEIGHT * 0.7, , );
[_btn setTitle:@"立即抢购" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_btn.titleLabel.font = [UIFont boldSystemFontOfSize:];
[_btn setBackgroundImage:[UIImage imageNamed:@"ButtonRedLarge"] forState:UIControlStateNormal];
[_btn addTarget:self action:@selector(startAnimation) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btn];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
_imageView.image = [UIImage imageNamed:@"TabCartSelected@2x.png"];
_imageView.center = CGPointMake(, );
[self.view addSubview:_imageView];
// label
_cntLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
_cntLabel.textColor = customColor;
_cntLabel.textAlignment = NSTextAlignmentCenter;
_cntLabel.font = [UIFont boldSystemFontOfSize:];
_cntLabel.backgroundColor = [UIColor whiteColor];
_cntLabel.layer.cornerRadius = CGRectGetHeight(_cntLabel.bounds)/;
_cntLabel.layer.masksToBounds = YES;
_cntLabel.layer.borderWidth = 1.0f;
_cntLabel.layer.borderColor = customColor.CGColor;
[self.view addSubview:_cntLabel];
if (_cnt == ) {
_cntLabel.hidden = YES;
} self.path = [UIBezierPath bezierPath];
[_path moveToPoint:CGPointMake(, )];
[_path addQuadCurveToPoint:CGPointMake(, ) controlPoint:CGPointMake(, )];
}
-(void)startAnimation
{
if (!layer) {
_btn.enabled = NO;
layer = [CALayer layer];
layer.contents = (__bridge id)[UIImage imageNamed:@"test01.jpg"].CGImage;
layer.contentsGravity = kCAGravityResizeAspectFill;
layer.bounds = CGRectMake(, , , );
[layer setCornerRadius:CGRectGetHeight([layer bounds]) / ];
layer.masksToBounds = YES;
layer.position =CGPointMake(, );
[self.view.layer addSublayer:layer];
}
[self groupAnimation];
}
-(void)groupAnimation
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path = _path.CGPath;
animation.rotationMode = kCAAnimationRotateAuto; CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
expandAnimation.duration = 0.5f;
expandAnimation.fromValue = [NSNumber numberWithFloat:];
expandAnimation.toValue = [NSNumber numberWithFloat:2.0f];
expandAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
narrowAnimation.beginTime = 0.5;
narrowAnimation.fromValue = [NSNumber numberWithFloat:2.0f];
narrowAnimation.duration = 1.5f;
narrowAnimation.toValue = [NSNumber numberWithFloat:0.5f]; narrowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; CAAnimationGroup *groups = [CAAnimationGroup animation];
groups.animations = @[animation,expandAnimation,narrowAnimation];
groups.duration = 2.0f;
groups.removedOnCompletion=NO;
groups.fillMode=kCAFillModeForwards;
groups.delegate = self;
[layer addAnimation:groups forKey:@"group"];
} -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
// [anim def];
if (anim == [layer animationForKey:@"group"]) {
_btn.enabled = YES;
[layer removeFromSuperlayer];
layer = nil;
_cnt++;
if (_cnt) {
_cntLabel.hidden = NO;
} // 数量渐变
CATransition *animation = [CATransition animation];
animation.duration = 3.25f;
_cntLabel.text = [NSString stringWithFormat:@"%ld",(long)_cnt];
[_cntLabel.layer addAnimation:animation forKey:nil]; // 购物车上下跳动
CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
shakeAnimation.duration = 0.25f;
shakeAnimation.fromValue = [NSNumber numberWithFloat:-];
shakeAnimation.toValue = [NSNumber numberWithFloat:];
shakeAnimation.autoreverses = YES;
[_imageView.layer addAnimation:shakeAnimation forKey:nil];
}
}
第二十九篇、CoreAnimation的使用的更多相关文章
- 第二十九篇、UICollectionView瀑布流
1.实现思路 >第一种方案:UIScrollView 镶嵌三个UITableView (不推荐使用) >第二种方案:UIScrollView 镶嵌UIImageView (需要解决循环利用 ...
- Python之路(第二十九篇) 面向对象进阶:内置方法补充、异常处理
一.__new__方法 __init__()是初始化方法,__new__()方法是构造方法,创建一个新的对象 实例化对象的时候,调用__init__()初始化之前,先调用了__new__()方法 __ ...
- 第二十九篇:使用SOUI的SMCListView控件
列表控件是客户端应用最常用的控件之一.列表控件通常只负责显示数据,最多通知一下APP列表行的选中状态变化. 现在的UI经常要求程序猿在列表控件里不光显示内容,还要能和用户交互,显示动画等等,传统的列表 ...
- 第二十九篇-Fragment动态用法
效果图: 上节学习了静态添加Fragment的方法,这节学习动态添加方法. 主页面 layout.xml Fragment页面 layout2.xml 实现功能,当点击主页面的button时,将Fra ...
- flask第二十九篇——一个例子+【更新内容通知】
请关注公众号:自动化测试实战 大家先自己写一下,船长写这个花了半个小时,因为我和大家一样,也是新手: 写一个页面如下,点击书名以后跳转到书的详情页 书的信息如下: books = [ { 'id': ...
- Android UI开发第二十九篇——Android中五种常用的menu(菜单)
Android Menu在手机的应用中起着导航的作用,作者总结了5种常用的Menu. 1.左右推出的Menu 前段时间比较流行,我最早是在海豚浏览器中看到的,当时耳目一新.最早使用左右推出菜单的,听说 ...
- Python之路【第二十九篇】:django ORM模型层
ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...
- 第二十九篇 玩转数据结构——线段树(Segment Tree)
1.. 线段树引入 线段树也称为区间树 为什么要使用线段树:对于某些问题,我们只关心区间(线段) 经典的线段树问题:区间染色,有一面长度为n的墙,每次选择一段墙进行染色(染色允许覆盖),问 ...
- 第二十九篇 -- PY程序返回值问题
今天兴之所至,来写一写关于程序返回值的问题.普通的py程序就不用多说了,sys.exit(result),result就是你想返回的返回值啦.我们今天来讲讲用PyQt5写的带界面的程序如何设置返回值的 ...
随机推荐
- [置顶] stax解析xml文档的6种方式
原文链接:http://blog.csdn.net/u011593278/article/details/9745271 stax解析xml文档的方式: 基于光标的查询: 基于迭代模型的查找: 基于过 ...
- ADO.NET 快速入门(七):使用数据库事务
数据库事务用于控制数据提交到数据库.例如,在标准的账户程序,账户的借贷必须同时完成.由于电脑偶尔发生故障(电力中断.网络中断,等等),可能有些记录被更新或者添加,但是另外一些没有.为了避免这些情况,可 ...
- IPv6 sokcet 编程
IPv6的数据包包头与IPv4的数据包头不一样,所以在IPv6下的socket编程用到的某些结构体和地址转换函数也与IPv4下的socket编程不一样.涉及的结构体有:IPv4中使用sockaddr/ ...
- URAL 1776 C - Anniversary Firework DP
C - Anniversary FireworkTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...
- glsl水包含倒影的实现(rtt) [转]
转自 http://blog.sina.com.cn/s/blog_78ea87380101eixi.html 此文实现一个简单地水面倒影效果,通过rtt相机 获取倒影纹理, 水的基本实现方法(参考前 ...
- android153 笔记 5
52. Linux中跨进程通信的几种方式 . linux编程全部是基于文件管理的. # 管道( pipe ):管道也是一个文件,一个进程负责读一个进程负责写,管道是一种半双工(2边可以通信但是不能是同 ...
- Panopticon跨平台的逆向工程反汇编工具
http://www.freebuf.com/sectool/104045.html Panopticon 使用GPLv3授权许可,其免费. 项目文档:https://panopticon.re. 问 ...
- c#智能感知(设置)及实现
1) 使用工具->选项菜单命令,选择文本编辑器->C#设置, 将上面圈出的2个复选框(自动列出成员和参数信息)打勾, 然后 Intellisense就会工作了.(如果没这么多选项,请勾选S ...
- 《RESTful Web Services》第三章 设计表述
3.1 如何使用实体头来注解表述 表述不仅仅是以某种格式序列化后的数据,它是一连串字节加上用于描述那些字节的元数据. Content-Type,用于描述表述类型.这个标头告诉接收方如何 ...
- 进程间通信之XSI IPC
XSI IPC源自于系统V的IPC功能. 有三种IPC我们称作XSI IPC,即消息队列.信号量以及共享存储器,它们之间有很多相似之处. 1.标识符和键 每个内核中的IPC结构(消息队列.信号量或共享 ...