IOS开发基础之核心动画 基础动画、关键帧、组动画案例
2024-10-21 15:43:08
//
// ViewController.m
// 30-核心动画
//
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,weak)CALayer *layer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *redView =[[UIView alloc] init];
redView.frame =CGRectMake(100, 100, 20, 20);
redView.backgroundColor =[UIColor redColor];
self.layer=redView.layer ;
[self.view addSubview:redView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//组动画
CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
//创建动画对象 (做什么动画)
CABasicAnimation *anim = [[CABasicAnimation alloc]init];
anim.keyPath = @"transform.rotation";
anim.byValue =@(2*M_PI*10);
CAKeyframeAnimation *anim1=[[CAKeyframeAnimation alloc] init];
anim1.keyPath = @"position";
UIBezierPath * path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2*M_PI clockwise:1];
anim1.path = path.CGPath;
group.animations=@[anim,anim1];
group.duration = 3;
group.repeatCount = INT_MAX; // 重复的次数
//怎么做动画
//添加动画(对谁做动画)
[self.layer addAnimation:group forKey:nil];
}
//关键帧
-(void)test2{
//关键帧
//创建动画对象 (做什么动画)
CAKeyframeAnimation *anim = [[CAKeyframeAnimation alloc] init];
//怎么做动画
anim.keyPath=@"position";
/* NSValue *v1 =[NSValue valueWithCGPoint:CGPointMake(100, 100)];
NSValue *v2 =[NSValue valueWithCGPoint:CGPointMake(150, 100)];
NSValue *v3 =[NSValue valueWithCGPoint:CGPointMake(100, 150)];
NSValue *v4 =[NSValue valueWithCGPoint:CGPointMake(150,150 )];
anim.values =@[v1,v2,v3,v4];
*/
UIBezierPath * path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2*M_PI clockwise:1];
anim.path = path.CGPath;
anim.duration = 2;
anim.repeatCount = INT_MAX; // 重复的次数
//添加动画(对谁做动画)
[self.layer addAnimation:anim forKey:nil];
}
//基础动画
-(void)testbaseAnimation{
//创建动画对象 (做什么动画)
CABasicAnimation *animation = [[CABasicAnimation alloc] init];
//怎么做动画
animation.keyPath = @"position.x";
/*animation.fromValue =@(10); 从哪
animation.toValue = @(300); 到哪
*/
//每次加10px
animation.byValue = @(10);
//不回去原来的地方
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion=NO;
//添加动画(对谁做动画)
[self.layer addAnimation:animation forKey:nil];
}
@end
// LJView.m
// 30-核心动画
//
//
#import "LJView.h"
@implementation LJView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
- (void)drawRect:(CGRect)rect {
// Drawing code
UIBezierPath * path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2*M_PI clockwise:1];
[path stroke];
}
@end
最新文章
- HTTPS, SPDY和 HTTP/2性能的简单对比
- $.when().then()
- Gradle中使用idea插件的一些实践
- Apache配置命令
- windows核心编程-信号量(semaphore)
- [C++程序设计]函数的递归调用
- spring的作用及优势---第一个spring示例
- javascript——ajax应用
- Eclipse中使用Maven新建 Servlet 2.5的 SpringMVC项目
- Groovy闭包详解
- UI组件--element-ui--Upload多组件自定义上传
- 原生JS-旋转木马
- Java 生成三位随机数
- ECC校验
- Seaborn-05-Pairplot多变量图
- Vs.net 常用命令行
- 合并excel的多个sheet
- 数据类型转换(计算mac地址)
- 剑指offer-第三章高质量代码(树的子结构)
- Win8.1和office2013使用电话激活步骤