IOS基础之UIDynamicAnimator动力学入门

01-重力

//
//  ViewController.m
//  01-重力
//
//  Created by 鲁军 on 2021/4/15.
//#import "ViewController.h"@interface ViewController()@property(nonatomic,weak)UIView *redView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end@implementation ViewController- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根据某一个范围 创建动画者对象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根据某一个动力学元素,创建行为UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];//3把行为添加到动画者当中[self.animator addBehavior:gravity];
//    gravity.angle = M_PI; //用角度的形式设置方向
//    gravity.gravityDirection = CGVectorMake(1, 1);  //右下角跑//    gravity.magnitude = 100;//重力的量级。即重力加速度}- (void)viewDidLoad {[super viewDidLoad];UIView * redView =[[UIView alloc] init ];redView.backgroundColor = [UIColor redColor];redView.frame = CGRectMake(100, 100, 100, 100);[self.view addSubview:redView];self.redView = redView;}@end

02-碰撞

//
//  ViewController.m
//  02-碰撞
//
//  Created by 鲁军 on 2021/4/15.
//#import "ViewController.h"@interface ViewController()@property(nonatomic,weak)UIView *redView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end@implementation ViewController- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根据某一个范围 创建动画者对象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根据某一个动力学元素,创建行为UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];// 3碰撞行为UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView]];//把引用的view 的Bounds 变成边界collision.translatesReferenceBoundsIntoBoundary = YES;//4把行为添加到动画者当中[self.animator addBehavior:gravity];[self.animator addBehavior:collision];}- (void)viewDidLoad {[super viewDidLoad];UIView * redView =[[UIView alloc] init ];redView.backgroundColor = [UIColor redColor];redView.frame = CGRectMake(100, 100, 100, 100);[self.view addSubview:redView];self.redView = redView;}@end

03-碰撞与另外一个Item碰撞

//
//  ViewController.m
//  03-碰撞与另外一个Item碰撞
//
//  Created by 鲁军 on 2021/4/17.
//#import "ViewController.h"#define H [UIScreen mainScreen].bounds.size.height@interface ViewController()@property(nonatomic,weak)UIView *redView;
@property(nonatomic,weak)UIView *blueView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end@implementation ViewController- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根据某一个范围 创建动画者对象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根据某一个动力学元素,创建行为UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];// 3碰撞行为UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];//把引用的view 的Bounds 变成边界collision.translatesReferenceBoundsIntoBoundary = YES;//4把行为添加到动画者当中[self.animator addBehavior:gravity];[self.animator addBehavior:collision];}- (void)viewDidLoad {[super viewDidLoad];UIView * redView =[[UIView alloc] init ];redView.backgroundColor = [UIColor redColor];redView.frame = CGRectMake(100, 100, 100, 100);[self.view addSubview:redView];self.redView = redView;UIView * blueView =[[UIView alloc] init ];blueView.backgroundColor = [UIColor blueColor];blueView.frame = CGRectMake(170,H - 50, 50, 50);[self.view addSubview:blueView];self.blueView = blueView;}@end

04-碰撞模式

//
//  ViewController.m
//  04-碰撞模式
//
//  Created by 鲁军 on 2021/4/17.
//#import "ViewController.h"#define H [UIScreen mainScreen].bounds.size.height@interface ViewController()@property(nonatomic,weak)UIView *redView;
@property(nonatomic,weak)UIView *blueView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end@implementation ViewController- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根据某一个范围 创建动画者对象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根据某一个动力学元素,创建行为UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];// 3碰撞行为UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];//把引用的view 的Bounds 变成边界collision.translatesReferenceBoundsIntoBoundary = YES;//碰撞模式//UICollisionBehaviorModeItems 仅仅Item 和 Item 之间发生的碰撞//UICollisionBehaviorModeBoundaries 仅仅是和边界发生碰撞//UICollisionBehaviorModeEverything Item 和边界都发生碰撞   默认是everythingcollision.collisionMode = UICollisionBehaviorModeEverything;//4把行为添加到动画者当中[self.animator addBehavior:gravity];[self.animator addBehavior:collision];}- (void)viewDidLoad {[super viewDidLoad];UIView * redView =[[UIView alloc] init ];redView.backgroundColor = [UIColor redColor];redView.frame = CGRectMake(100, 100, 100, 100);[self.view addSubview:redView];self.redView = redView;UIView * blueView =[[UIView alloc] init ];blueView.backgroundColor = [UIColor blueColor];blueView.frame = CGRectMake(170,H - 50, 50, 50);[self.view addSubview:blueView];self.blueView = blueView;}@end

05- 碰撞行为-创建边界

//
//  ViewController.m
//  05-碰撞行为-创建边界
//
//  Created by 鲁军 on 2021/4/17.
//#import "ViewController.h"#define H [UIScreen mainScreen].bounds.size.height@interface BGView : UIView@end@implementation BGView- (void)drawRect:(CGRect)rect{//CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];UIBezierPath *path =  [[UIBezierPath alloc] init];[path moveToPoint:CGPointMake(0, 200)];[path addLineToPoint:CGPointMake(150, 250)];[path stroke];}@end@interface ViewController()@property(nonatomic,weak)UIView *redView;
@property(nonatomic,weak)UIView *blueView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end@implementation ViewController
//优先级是最高的
- (void)loadView{self.view = [[BGView alloc] initWithFrame:[UIScreen mainScreen].bounds];self.view.backgroundColor = [UIColor whiteColor];
}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根据某一个范围 创建动画者对象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根据某一个动力学元素,创建行为UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];// 3碰撞行为UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];//把引用的view 的Bounds 变成边界collision.translatesReferenceBoundsIntoBoundary = YES;//添加边界//以一条线为边界// [collision addBoundaryWithIdentifier:@"key" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];//以一个自定义的矩形框 为边界UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.blueView.frame];[collision addBoundaryWithIdentifier:@"key" forPath:path];//4把行为添加到动画者当中[self.animator addBehavior:gravity];[self.animator addBehavior:collision];}- (void)viewDidLoad {[super viewDidLoad];UIView * redView =[[UIView alloc] init ];redView.backgroundColor = [UIColor redColor];redView.frame = CGRectMake(100, 100, 100, 100);[self.view addSubview:redView];self.redView = redView;UIView * blueView =[[UIView alloc] init ];blueView.backgroundColor = [UIColor blueColor];blueView.frame = CGRectMake(170,H - 150, 50, 50);[self.view addSubview:blueView];self.blueView = blueView;}@end

06-碰撞行为-action

//
//  ViewController.m
//  06-碰撞行为-action
//
//  Created by 鲁军 on 2021/4/17.
//#import "ViewController.h"#define H [UIScreen mainScreen].bounds.size.height@interface BGView : UIView
@property(nonatomic,assign)CGRect redRect;@end@implementation BGView- (void)drawRect:(CGRect)rect{//CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];/* UIBezierPath *path =  [[UIBezierPath alloc] init];[path moveToPoint:CGPointMake(0, 200)];[path addLineToPoint:CGPointMake(150, 250)];[path stroke];*/[[UIBezierPath bezierPathWithRect:self.redRect] stroke];}@end@interface ViewController()@property(nonatomic,weak)UIView *redView;
@property(nonatomic,weak)UIView *blueView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end@implementation ViewController
//优先级是最高的
- (void)loadView{self.view = [[BGView alloc] initWithFrame:[UIScreen mainScreen].bounds];self.view.backgroundColor = [UIColor whiteColor];
}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根据某一个范围 创建动画者对象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根据某一个动力学元素,创建行为UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];// 3碰撞行为UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];//把引用的view 的Bounds 变成边界collision.translatesReferenceBoundsIntoBoundary = YES;//添加边界//以一条线为边界// [collision addBoundaryWithIdentifier:@"key" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];//以一个自定义的矩形框 为边界UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.blueView.frame];[collision addBoundaryWithIdentifier:@"key" forPath:path];collision.action = ^{NSLog(@"%@",NSStringFromCGRect(self.redView.frame));BGView *bgView = (BGView *)self.view;bgView.redRect = self.redView.frame;[self.view setNeedsDisplay]; //重绘if(self.redView.frame.size.width > 105){self.redView.backgroundColor = [UIColor yellowColor];}else {self.redView.backgroundColor = [UIColor redColor];}};//4把行为添加到动画者当中[self.animator addBehavior:gravity];[self.animator addBehavior:collision];}- (void)viewDidLoad {[super viewDidLoad];UIView * redView =[[UIView alloc] init ];redView.backgroundColor = [UIColor redColor];redView.frame = CGRectMake(100, 100, 100, 100);[self.view addSubview:redView];self.redView = redView;UIView * blueView =[[UIView alloc] init ];blueView.backgroundColor = [UIColor blueColor];blueView.frame = CGRectMake(170,H - 150, 50, 50);[self.view addSubview:blueView];self.blueView = blueView;}@end

07-碰撞的行为-代理方法

//
//  ViewController.m
//  07-碰撞的行为-代理方法
//
//  Created by 鲁军 on 2021/4/17.
//#import "ViewController.h"#define H [UIScreen mainScreen].bounds.size.height@interface BGView : UIView
@property(nonatomic,assign)CGRect redRect;@end@implementation BGView- (void)drawRect:(CGRect)rect{//CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];UIBezierPath *path =  [[UIBezierPath alloc] init];[path moveToPoint:CGPointMake(0, 200)];[path addLineToPoint:CGPointMake(150, 250)];[path stroke];[[UIBezierPath bezierPathWithRect:self.redRect] stroke];}@end@interface ViewController() <UICollisionBehaviorDelegate>@property(nonatomic,weak)UIView *redView;
@property(nonatomic,weak)UIView *blueView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end@implementation ViewController
//优先级是最高的
- (void)loadView{self.view = [[BGView alloc] initWithFrame:[UIScreen mainScreen].bounds];self.view.backgroundColor = [UIColor whiteColor];
}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根据某一个范围 创建动画者对象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根据某一个动力学元素,创建行为UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];// 3碰撞行为UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];//把引用的view 的Bounds 变成边界collision.translatesReferenceBoundsIntoBoundary = YES;//添加边界//以一条线为边界[collision addBoundaryWithIdentifier:@"key1" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];//以一个自定义的矩形框 为边界UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.blueView.frame];[collision addBoundaryWithIdentifier:@"key2" forPath:path];collision.action = ^{//  NSLog(@"%@",NSStringFromCGRect(self.redView.frame));BGView *bgView = (BGView *)self.view;bgView.redRect = self.redView.frame;[self.view setNeedsDisplay]; //重绘};//设置代理collision.collisionDelegate = self;//4把行为添加到动画者当中[self.animator addBehavior:gravity];[self.animator addBehavior:collision];}- (void)viewDidLoad {[super viewDidLoad];UIView * redView =[[UIView alloc] init ];redView.backgroundColor = [UIColor redColor];redView.frame = CGRectMake(100, 100, 100, 100);[self.view addSubview:redView];self.redView = redView;UIView * blueView =[[UIView alloc] init ];blueView.backgroundColor = [UIColor blueColor];blueView.frame = CGRectMake(170,H - 150, 50, 50);[self.view addSubview:blueView];self.blueView = blueView;}- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item1 withItem:(id<UIDynamicItem>)item2 atPoint:(CGPoint)p{}
//代理方法
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p{//强制转化 NSString *str = (NSString *) identifier;if( [str isEqualToString:@"key1"]){self.redView.backgroundColor = [UIColor orangeColor];}else{self.redView.backgroundColor =[UIColor redColor];}
}@end

08-甩行为

//
//  ViewController.m
//  08-甩行为
//
//  Created by 鲁军 on 2021/4/17.
//#import "ViewController.h"@interface ViewController ()
@property(nonatomic,weak)UIView *redView;
@property(nonatomic,strong)UIDynamicAnimator *animator;
@end@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//获取触摸对象UITouch *t = touches.anyObject;CGPoint p = [t locationInView:t.view];//1动画者对象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2创建行为UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:p];//damping 阻尼 值越小 晃动越大 0 - 2。 减速 属性 snap.damping = 2;//3把行为添加到动画者当中[self.animator addBehavior:snap];}
- (void)viewDidLoad {[super viewDidLoad];UIView * redView =[[UIView alloc] init ];redView.backgroundColor = [UIColor redColor];redView.frame = CGRectMake(100, 100, 100, 100);[self.view addSubview:redView];self.redView = redView;
}@end

09-附着行为

//
//  ViewController.m
//  09-附着行为
//
//  Created by 鲁军 on 2021/4/17.
//#import "ViewController.h"@interface BGView : UIView
@property(nonatomic,assign)CGPoint startPoint;
@property(nonatomic,assign)CGPoint endPoint;
@end@implementation BGView- (void)drawRect:(CGRect)rect{UIBezierPath *path =   [UIBezierPath bezierPath];[path moveToPoint:self.startPoint];[path addLineToPoint:self.endPoint];[path stroke];
}@end@interface ViewController ()
@property(nonatomic,weak)UIView *redView;
@property(nonatomic,strong)UIDynamicAnimator *animator;
@property(nonatomic,strong)UIAttachmentBehavior *attach;
@end@implementation ViewController
- (void)loadView{self.view = [[BGView alloc] initWithFrame:[UIScreen mainScreen].bounds];self.view.backgroundColor = [UIColor whiteColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//获取触摸对象UITouch *t = touches.anyObject;CGPoint p = [t locationInView:t.view];//1动画者对象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2创建行为//刚性附着self.attach = [[UIAttachmentBehavior alloc] initWithItem:self.redView attachedToAnchor:p];self.attach.length = 100;UIGravityBehavior * gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];//3把行为添加到动画者当中[self.animator addBehavior:gravity];[self.animator addBehavior:self.attach];}- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//获取触摸对象UITouch *t = touches.anyObject;CGPoint p = [t locationInView:t.view];self.attach.anchorPoint = p;__weak ViewController *weakView = self;self.attach.action= ^{BGView *bgView = (BGView *) weakView.view;bgView.startPoint = weakView.redView.center;bgView.endPoint = p;[weakView.view setNeedsDisplay];};}- (void)viewDidLoad {[super viewDidLoad];UIView * redView =[[UIView alloc] init ];redView.backgroundColor = [UIColor redColor];redView.frame = CGRectMake(100, 100, 100, 100);[self.view addSubview:redView];self.redView = redView;self.redView.alpha = 0.5;
}@end

IOS基础之UIDynamicAnimator动力学入门-01相关推荐

  1. IOS基础之UIDynamicAnimator动力学入门-02

    IOS基础之UIDynamicAnimator动力学入门-02 10-弹性附着 // // ViewController.m // 10-弹性附着 // // Created by 鲁军 on 202 ...

  2. python基础入门01

    python 基础入门01 前言 python 是一种计算机程序设计语言, 由创始人吉多·范罗苏姆(Guido van Rossum) 在1989年阿姆斯特丹的圣诞节期间为了打发无聊的时间,决定开发一 ...

  3. PLC编程入门-01基础知识介绍

    PLC编程入门-01基础知识介绍 PLC的组成结构 PLC编程语言: PLC输入输出的特点 输入 输出 PLC的组成结构 简图 明细图 CPU:控制器和运算器本身就是CPU主要组成部分,和PC的CPU ...

  4. 数据结构和算法_零基础入门01

    数据结构和算法_零基础入门01 一.数据结构是什么? 逻辑结构.物理结构 二.算法 算法的五个基本特征 算法设计的要求 b站学习小甲鱼的数据结构与算法,自留笔记. 程序设计=数据结构+算法 一.数据结 ...

  5. IOS基础之愤怒的小方块

    IOS基础之愤怒的小方块 // // ViewController.m // 18-愤怒的小方块 // // Created by 鲁军 on 2021/4/17. //#import "V ...

  6. IOS基础之毛毛虫案例-重力

    IOS基础之毛毛虫案例-重力 // // ViewController.m // 17-毛毛虫案例-重力 // // Created by 鲁军 on 2021/4/17. //#import &qu ...

  7. 【网络爬虫入门01】应用Requests和BeautifulSoup联手打造的第一条网络爬虫

    [网络爬虫入门01]应用Requests和BeautifulSoup联手打造的第一条网络爬虫 广东职业技术学院 欧浩源 2017-10-14  1.引言 在数据量爆发式增长的大数据时代,网络与用户的沟 ...

  8. ❤️《Vue前端基础框架集合从入门到高级》(小白也可学,建议收藏)❤️

    <Vue前端基础框架集合从入门到高级>,小白也可学 文章目录 <Vue前端基础框架集合从入门到高级>,小白也可学 ❤️一.前端核心分析 ❤️1.1.概述 ❤️1.2.前端三要素 ...

  9. IOS开发百度地图API入门到精通-用点生成路线,导航,气泡响应

    (转)IOS开发百度地图API入门到精通-用点生成路线,导航,气泡响应 IOS百度地图API开发自定义气泡,点击气泡自动生成路线,以及拖拽 IOS百度地图开发POISearch搜索附近停车场,附近加油 ...

最新文章

  1. Linux qgis 编译,QGIS简介与源代码编译
  2. 拥抱开源,Office 365开发迎来新时代
  3. 有趣又好玩的glm库
  4. 四、Dynamic-programming algorithm Dynamic--LCS
  5. 循环序列模型 —— 1.7 对新序列采样
  6. LeetCode Binary Tree Right Side View (DFS/BFS)
  7. Liunx 项目部署及域名访问
  8. pandas 数据分析 相关性_Pandas库学习笔记2-Pandas数据特征分析
  9. 一个简单的python爬虫,以豆瓣妹子“http://www.dbmeizi.com/category/2?p= ”为例
  10. 常用的项目管理工具有哪些?
  11. 互联网和人工智能之间,主要是什么关系?
  12. 360安全卫士企业版本 跳过卸载保护密码
  13. win10无线网卡无法连接网络
  14. 爱德泰科普 | 一文了解如何整理机柜内的光纤跳线
  15. java csrf_java – 如何在使用CSRF登录后启用Spring Secu...
  16. 淘宝/天猫API ,获取sku详细信息 OneBound数据
  17. 个人品牌的三项基本功
  18. STM32F103系列芯片原理学习以及LED灯的点亮
  19. c语言计算时间差的程序小时和分钟,C语言输入两个时间(同一天的两个时和分),计算其时间差,输出相差几小时几分钟?...
  20. ctf秀misc【5-23】

热门文章

  1. 一维有限元法matlab,一维有限元法解常微分方程
  2. 虚拟机架云服务器,云服务器 虚拟机架设
  3. python读取串口数据保存到mysql数据库_Python3读取Excel数据存入MySQL的方法
  4. hive把字符串转换为时间_关于hive的时间转换
  5. 戴尔win10插耳机还外放_通州附近戴尔笔记本电脑维修哪家强-北京信维佳业科技有限公司...
  6. 研究所月入两万,是一种什么体验?
  7. 2020 职场,哪类程序员会是王者?!
  8. 电脑没有声音一键修复_电脑上有没有好用点的办公提醒小软件?有带声音提醒的桌面便签软件吗...
  9. upload-labs_pass12_文件名截断_URL要编码为%00_pass13_文件名截断_Hex修改为00
  10. rabbitmq python_Python操作RabbitMQ服务器实现消息队列的路由功能