博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[iOS]UIDynamicAnimator动画
阅读量:5136 次
发布时间:2019-06-13

本文共 6318 字,大约阅读时间需要 21 分钟。

创建动画

UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

协议代理

@protocol UIDynamicAnimatorDelegate 
@optional- (void)dynamicAnimatorWillResume:(UIDynamicAnimator *)animator;- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator;@end

属性

// 行为执行时间@property (nonatomic, readonly) NSTimeInterval elapsedTime;// 判断是否正在执行@property (nonatomic, readonly, getter = isRunning) BOOL running;

设置动画组件Item的动力属性

UIDynamicItemBehavior

UIDynamicItemBehavior *dynamic = [[UIDynamicItemBehavior alloc] init];[animator addBehavior:dynamic];[dynamic addItem:view];// 相关属性@property (readwrite, nonatomic) CGFloat elasticity; // Usually between 0 (inelastic) and 1 (collide elastically)@property (readwrite, nonatomic) CGFloat friction; // 0 being no friction between objects slide along each other@property (readwrite, nonatomic) CGFloat density; // 1 by default@property (readwrite, nonatomic) CGFloat resistance; // 0: no velocity damping@property (readwrite, nonatomic) CGFloat angularResistance; // 0: no angular velocity damping

为动画组件添加具体行为

吸引行为 UISnapBehavior

UISnapBehavior *snap = [[UISnapBehavior alloc]                        initWithItem:view                        snapToPoint:CGPointMake(200, 300)];snap.damping = 0.9;//阻尼系数[animator addBehavior:snap];

重力行为 UIGravityBehavior

UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init];// 重力矢量方向 默认为 (0,1)gravity.gravityDirection = CGVectorMake(0, 1);// 重力大小gravity.magnitude = 5;[animator addBehavior:gravity];[gravity addItem:view];

碰撞行为 UICollisionBehavior

UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];// 边界刚体碰撞collision.translatesReferenceBoundsIntoBoundary = YES;[animator addBehavior:collision];[collision addItem:view];

作用力行为 UIPushBehavior

UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[view]                                                        mode:UIPushBehaviorModeInstantaneous];//    UIPushBehaviorModeContinuous 持续作用力//    UIPushBehaviorModeInstantaneous 瞬间作用力push.active = YES;push.pushDirection = CGVectorMake(1, 0);[animator addBehavior:push];

效果演示

UIDynamicItemBehavior+UIGravityBehavior+UICollisionBehavior

- (void)viewDidLoad{  [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.    animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];    dynamic = [[UIDynamicItemBehavior alloc]init];  dynamic.elasticity = 0.7;// 弹力系数  [animator addBehavior:dynamic];    gravity = [[UIGravityBehavior alloc]init];  gravity.gravityDirection = CGVectorMake(0, 1);// 重力矢量方向  [animator addBehavior:gravity];    collision = [[UICollisionBehavior alloc]init];  collision.translatesReferenceBoundsIntoBoundary = YES;// 边界刚体碰撞  [animator addBehavior:collision];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{  CGFloat width = self.view.frame.size.width;    int x = arc4random()%(int)width;  int z = arc4random()%20;    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(x, 10, 20+z, 20+z)];  view.backgroundColor = [UIColor greenColor];  view.layer.borderColor = [UIColor blueColor].CGColor;  view.layer.borderWidth = 1.0;  view.layer.masksToBounds = YES;  [self.view addSubview:view];      [dynamic addItem:view];  [gravity addItem:view];  [collision addItem:view];}

UIPushBehavior+UIGravityBehavior

- (void)viewDidLoad{  [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.    animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];  [self.view addGestureRecognizer:tap];    gravity = [[UIGravityBehavior alloc] init];  gravity.gravityDirection = CGVectorMake(0, 1);  gravity.magnitude = 10.0;  [animator addBehavior:gravity];}- (void)tap:(UITapGestureRecognizer *)tap{  CGPoint point = [tap locationInView:self.view];    for (CGFloat i = 0 ; i < M_PI*2; i = i + 0.2)  {    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];    view.center = point;    view.backgroundColor = [UIColor blueColor];    view.layer.cornerRadius = 5;    view.layer.masksToBounds = YES;    [self.view addSubview:view];        UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[view]                                                            mode:UIPushBehaviorModeInstantaneous];    push.active = YES;    push.angle = i;    push.magnitude = 0.05;    [animator addBehavior:push];        [gravity addItem:view];  }}

 

UIDynamicItemBehavior+UICollisionBehavior+UISnapBehavior

- (void)viewDidLoad{  [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.    animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];    dynamic = [[UIDynamicItemBehavior alloc]init];  dynamic.elasticity = 0.7;// 弹力系数  [animator addBehavior:dynamic];    collision = [[UICollisionBehavior alloc]init];  collision.translatesReferenceBoundsIntoBoundary = YES;// 边界刚体碰撞  [animator addBehavior:collision];    CGFloat width = self.view.frame.size.width;  CGFloat height = self.view.frame.size.height;    red = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];  red.center = CGPointMake(width/2, height/2+100);  red.backgroundColor = [UIColor redColor];  red.layer.cornerRadius = 15.0;  red.layer.masksToBounds = YES;  [self.view addSubview:red];    [collision addItem:red];    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];  [self.view addGestureRecognizer:tap];}- (void)tap:(UITapGestureRecognizer *)tap{  CGPoint point = [tap locationInView:self.view];    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];  view.center = point;  view.backgroundColor = [UIColor blueColor];  view.layer.cornerRadius = 10.0;  view.layer.masksToBounds = YES;  [self.view addSubview:view];    [collision addItem:view];    UISnapBehavior *snap = [[UISnapBehavior alloc]                          initWithItem:view                          snapToPoint:red.center];  snap.damping = 0.1;  [animator addBehavior:snap];    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{    [animator removeBehavior:snap];  });}

转载于:https://www.cnblogs.com/EverNight/p/7029052.html

你可能感兴趣的文章
nginx 不带www到www域名的重定向
查看>>
记录:Android中StackOverflow的问题
查看>>
导航,头部,CSS基础
查看>>
[草稿]挂载新硬盘
查看>>
[USACO 2017 Feb Gold] Tutorial
查看>>
关于mysql中GROUP_CONCAT函数的使用
查看>>
OD使用教程20 - 调试篇20
查看>>
Java虚拟机(JVM)默认字符集详解
查看>>
Java Servlet 过滤器与 springmvc 拦截器的区别?
查看>>
(tmp >> 8) & 0xff;
查看>>
linux命令之ifconfig详细解释
查看>>
NAT地址转换
查看>>
Nhibernate 过长的字符串报错 dehydration property
查看>>
Deque - leetcode 【双端队列】
查看>>
gulp插件gulp-ruby-sass和livereload插件
查看>>
免费的大数据学习资料,这一份就足够
查看>>
clientWidth、clientHeight、offsetWidth、offsetHeight以及scrollWidth、scrollHeight
查看>>
企业级应用与互联网应用的区别
查看>>
itext jsp页面打印
查看>>
Perl正则表达式匹配
查看>>