博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIKit动力学(一)
阅读量:6288 次
发布时间:2019-06-22

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

hot3.png

@interface ViewController : UIViewController@end/*物理仿真元素  注意:  不是任何对象都能做物理仿真元素  不是任何对象都能进行物理仿真   物理仿真元素要素:  任何遵守了UIDynamicItem协议的对象  UIView默认已经遵守了UIDynamicItem协议,因此任何UI控件都能做物理仿真  UICollectionViewLayoutAttributes类默认也遵守UIDynamicItem协议  */#import "ViewController.h"@import QuartzCore;const CGPoint kInitialPoint1 = { .x = 33, .y = 250 };const CGPoint kInitialPoint2 = { .x = 150, .y = 250 };@interface ViewController ()@property (weak, nonatomic) IBOutlet UIView *box1;@property (weak, nonatomic) IBOutlet UIView *box2;@property (nonatomic) UIDynamicAnimator *dynamicAnimator;@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];  self.dynamicAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];  [self reset];}- (IBAction)reset {  [self.dynamicAnimator removeAllBehaviors];    self.box1.center = kInitialPoint1;    self.box1.layer.cornerRadius = 10;    self.box1.layer.borderWidth = 2;    self.box1.layer.borderColor = [[UIColor blackColor]CGColor];    self.box1.transform = CGAffineTransformIdentity;        self.box2.center = kInitialPoint2;    self.box2.layer.cornerRadius = 10;    self.box2.layer.borderWidth = 2;    self.box2.layer.borderColor = [[UIColor greenColor]CGColor];    self.box2.transform = CGAffineTransformIdentity;}- (IBAction)snap {//迅速移动行为(捕捉行为)  CGPoint point = [self randomPoint];  UISnapBehavior/*物理仿真行为*/ *snap = [[UISnapBehavior alloc] initWithItem:self.box1//物理仿真元素 要做动画的元素                                                  snapToPoint:point];  snap.damping = 0.25;//阻尼系数   移动后的弹动幅度  [self addTemporaryBehavior:snap];}- (IBAction)attach {//附着行为 ???    UIAttachmentBehavior *attach1 = [[UIAttachmentBehavior alloc] initWithItem:self.box1                                                              offsetFromCenter:UIOffsetMake(50, 50)//旋转点                                                              attachedToAnchor:self.box1.center];    [self.dynamicAnimator addBehavior:attach1];               UIAttachmentBehavior *attach2 = [[UIAttachmentBehavior alloc] initWithItem:self.box2                                                                attachedToItem:self.box1];    [self.dynamicAnimator addBehavior:attach2];          UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[self.box2]                                                              mode:UIPushBehaviorModeInstantaneous];      push.pushDirection = CGVectorMake(0, 2);      [self.dynamicAnimator addBehavior:push];    }- (IBAction)push {//推动行为  UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[self.box1]                                                          mode:UIPushBehaviorModeContinuous];  push.pushDirection = CGVectorMake(1, 1);//    CGVectorMake(0垂直方向 数值越大越往右偏移, 推动速度)  [self.dynamicAnimator addBehavior:push];}- (IBAction)gravity {//重力行为  UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.box1]];  gravity.action = ^{    NSLog(@"%@", NSStringFromCGPoint(self.box1.center));  };  [self.dynamicAnimator addBehavior:gravity];}- (IBAction)collision {//碰撞行为    UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.box1,                                                                                  self.box2]];    [self.dynamicAnimator addBehavior:collision];        UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[self.box1]                                                            mode:UIPushBehaviorModeInstantaneous];    push.pushDirection = CGVectorMake(3, 0);    collision.action = ^{//行为中添加操作        if (self.box2.center.x-self.box2.frame.size.width/2 > self.view.frame.size.width){            [self reset];        }    };    [self addTemporaryBehavior:push];   }- (void)addTemporaryBehavior:(UIDynamicBehavior *)behavior {  [self.dynamicAnimator/*物理仿真器 实施行为的引擎*/ addBehavior:behavior];  [self.dynamicAnimator performSelector:@selector(removeBehavior:)                             withObject:behavior                             afterDelay:1];}- (CGPoint)randomPoint {  CGSize size = self.view.bounds.size;  return CGPointMake(arc4random_uniform(size.width),                     arc4random_uniform(size.height));}@end

转载于:https://my.oschina.net/u/2319073/blog/732937

你可能感兴趣的文章
项目经理笔记一
查看>>
Hibernate一对一外键双向关联
查看>>
mac pro 入手,php环境配置总结
查看>>
MyBatis-Plus | 最简单的查询操作教程(Lambda)
查看>>
rpmfusion 的国内大学 NEU 源配置
查看>>
spring jpa 配置详解
查看>>
IOE,为什么去IOE?
查看>>
java 用反射简单应用,将Object简单转换成map
查看>>
Storm中的Worker
查看>>
dangdang.ddframe.job中页面修改表达式后进行检查
查看>>
Web基础架构:负载均衡和LVS
查看>>
Linux下c/c++相对路径动态库的生成与使用
查看>>
SHELL实现跳板机,只允许用户执行少量允许的命令
查看>>
SpringBoot 整合Redis
查看>>
2014上半年大片早知道
查看>>
Android 6.0指纹识别App开发案例
查看>>
正文提取算法
查看>>
轻松学PHP
查看>>
Linux中的网络监控命令
查看>>
this的用法
查看>>