IOS-用动画组制作花瓣掉落效果(另附iOS动画图表)
重要的两个方法:1.动画的数组:animations 2.启动的时间 beginTime
注意:动画组设置了持续时间(duration)可能会导致动画组里面的持续时间不管用
代码如下:
#import "ViewController.h"
@interface ViewController ()
{
CALayer * fllowers;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addBgView];
[self addFllower];
}
点击屏幕开始动画
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self addAnimationGroup];
}
背景图
-(void)addBgView{
UIImageView * image = [[UIImageView alloc]initWithFrame:self.view.frame];
image.image = [UIImage imageNamed:@"背景.jpg"];
[self.view addSubview:image];
}
在背景图上添加CALayer(花瓣)
-(void)addFllower{
UIImage * img = [UIImage imageNamed:@"花瓣"];
fllowers = [[CALayer alloc]init];
fllowers.position = CGPointMake(100, 200);
fllowers.bounds = CGRectMake(0, 0, img.size.width, img.size.height);
fllowers.contents = (id)img.CGImage;
[self.view.layer addSublayer:fllowers];
}
#pragma mark -------------添加动画组----------------
-(void)addAnimationGroup{
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[[self rotationAnimation],[self dropAnimation]];
group.duration = 10;
// 动画开始的时间
// CACurrentMediaTime() 获得当前的时间
// 从调用这个方法开始,5秒之后 执行这个动画
group.beginTime = CACurrentMediaTime()+5;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeBoth;
[fllowers addAnimation:group forKey:@"group"];
}
//旋转效果
-(CABasicAnimation *)rotationAnimation{
CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotation.toValue = @(M_2_PI*3);
rotation.removedOnCompletion = NO;
return rotation;
}
//关键帧动画,设置花瓣掉落的曲线
-(CAKeyframeAnimation *)dropAnimation{
CAKeyframeAnimation * drop = [CAKeyframeAnimation animationWithKeyPath:@"position"];
// drop.path =
CGPathRef pathref = CGPathCreateMutable();
CGPathMoveToPoint(pathref, NULL, fllowers.position.x, fllowers.position.y);
// CGFloat cp1x, CGFloat cp1y, 设置两个点,在这两个点之间画曲线
// CGFloat x, CGFloat y 终止点
CGPoint endPoint = CGPointMake(80, 600);
CGPathAddCurveToPoint(pathref, NULL,160 ,280 ,-30 ,500 , endPoint.x, endPoint.y);
drop.path = pathref;
CGPathRelease(pathref);
return drop;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
二.背景图和花瓣图
花瓣:
三.
最新文章
- Windows无法安装到这个磁盘。请确保在计算机的BIOS菜单中启用了磁盘控制器
- ssh config host
- Bootstrap学习笔记系列2-------Bootstrap简单表格处理
- HDU 2117 取(2堆)石子游戏【wzf博弈】
- Eclipse中启用Oracle jdbc logging
- web相关概念
- Linux里如何查找文件内容 (转)
- drag drop小游戏
- oracle中区分audit_file_dest, background_dump_dest, core_dump_dest, user_dump_dest
- 用 jQuery Masonry 插件创建瀑布流式的页面
- QT添加exe文件的图标LOGO
- 网站SEO,HTTP请求的关键数字----6
- WPF 快捷方式
- 《android开发进阶从小工到专家》读书笔记--网络框架的设计与实现
- I used to know
- c# 右下角弹出窗口
- 【转】Android辅助功能AccessibilityService自动全选择文字粘贴模拟输入
- C#_Attribute特性
- ui-router 1.0 001 - resolve, component, sref-active
- 获取Sqlserver上一句语句执行后受影响的行数@@rowCount
热门文章
- iOS block的用法
- FreeModbus 精简版本(Only RTU) for M128 (Modbus Slave)
- Appium Android定位元素与操作
- PHP魔术变量总结
- A Mathematician‘s Survival Guide Graduate School and Early Career Development
- 【转】linux中的sort命令
- PHP函数ip2long转换IP时数值太大产生负数的解决办法
- java并行调度框架封装及演示样例
- [React Fundamentals] Composable Components
- [xml解析]rapidxml读取文件