IOS基础之UIDynamicAnimator动力学入门-01
生活随笔
收集整理的這篇文章主要介紹了
IOS基础之UIDynamicAnimator动力学入门-01
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
IOS基礎(chǔ)之UIDynamicAnimator動(dòng)力學(xué)入門(mén)
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;//使用強(qiáng)引用,不會(huì)立刻消失 @end@implementation ViewController- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根據(jù)某一個(gè)范圍 創(chuàng)建動(dòng)畫(huà)者對(duì)象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根據(jù)某一個(gè)動(dòng)力學(xué)元素,創(chuàng)建行為UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];//3把行為添加到動(dòng)畫(huà)者當(dāng)中[self.animator addBehavior:gravity]; // gravity.angle = M_PI; //用角度的形式設(shè)置方向 // gravity.gravityDirection = CGVectorMake(1, 1); //右下角跑// gravity.magnitude = 100;//重力的量級(jí)。即重力加速度}- (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;}@end02-碰撞
// // ViewController.m // 02-碰撞 // // Created by 魯軍 on 2021/4/15. //#import "ViewController.h"@interface ViewController()@property(nonatomic,weak)UIView *redView; @property(nonatomic,strong)UIDynamicAnimator *animator;//使用強(qiáng)引用,不會(huì)立刻消失 @end@implementation ViewController- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根據(jù)某一個(gè)范圍 創(chuàng)建動(dòng)畫(huà)者對(duì)象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根據(jù)某一個(gè)動(dòng)力學(xué)元素,創(chuàng)建行為UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];// 3碰撞行為UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView]];//把引用的view 的Bounds 變成邊界collision.translatesReferenceBoundsIntoBoundary = YES;//4把行為添加到動(dòng)畫(huà)者當(dāng)中[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;}@end03-碰撞與另外一個(gè)Item碰撞
// // ViewController.m // 03-碰撞與另外一個(gè)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;//使用強(qiáng)引用,不會(huì)立刻消失 @end@implementation ViewController- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根據(jù)某一個(gè)范圍 創(chuàng)建動(dòng)畫(huà)者對(duì)象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根據(jù)某一個(gè)動(dòng)力學(xué)元素,創(chuàng)建行為UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];// 3碰撞行為UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];//把引用的view 的Bounds 變成邊界collision.translatesReferenceBoundsIntoBoundary = YES;//4把行為添加到動(dòng)畫(huà)者當(dāng)中[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;}@end04-碰撞模式
// // 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;//使用強(qiáng)引用,不會(huì)立刻消失 @end@implementation ViewController- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根據(jù)某一個(gè)范圍 創(chuàng)建動(dòng)畫(huà)者對(duì)象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根據(jù)某一個(gè)動(dòng)力學(xué)元素,創(chuàng)建行為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 之間發(fā)生的碰撞//UICollisionBehaviorModeBoundaries 僅僅是和邊界發(fā)生碰撞//UICollisionBehaviorModeEverything Item 和邊界都發(fā)生碰撞 默認(rèn)是everythingcollision.collisionMode = UICollisionBehaviorModeEverything;//4把行為添加到動(dòng)畫(huà)者當(dāng)中[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;}@end05- 碰撞行為-創(chuàng)建邊界
// // ViewController.m // 05-碰撞行為-創(chuàng)建邊界 // // 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;//使用強(qiáng)引用,不會(huì)立刻消失 @end@implementation ViewController //優(yōu)先級(jí)是最高的 - (void)loadView{self.view = [[BGView alloc] initWithFrame:[UIScreen mainScreen].bounds];self.view.backgroundColor = [UIColor whiteColor]; }- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根據(jù)某一個(gè)范圍 創(chuàng)建動(dòng)畫(huà)者對(duì)象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根據(jù)某一個(gè)動(dòng)力學(xué)元素,創(chuàng)建行為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)];//以一個(gè)自定義的矩形框 為邊界UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.blueView.frame];[collision addBoundaryWithIdentifier:@"key" forPath:path];//4把行為添加到動(dòng)畫(huà)者當(dāng)中[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;}@end06-碰撞行為-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;//使用強(qiáng)引用,不會(huì)立刻消失 @end@implementation ViewController //優(yōu)先級(jí)是最高的 - (void)loadView{self.view = [[BGView alloc] initWithFrame:[UIScreen mainScreen].bounds];self.view.backgroundColor = [UIColor whiteColor]; }- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根據(jù)某一個(gè)范圍 創(chuàng)建動(dòng)畫(huà)者對(duì)象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根據(jù)某一個(gè)動(dòng)力學(xué)元素,創(chuàng)建行為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)];//以一個(gè)自定義的矩形框 為邊界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把行為添加到動(dòng)畫(huà)者當(dāng)中[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;}@end07-碰撞的行為-代理方法
// // 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;//使用強(qiáng)引用,不會(huì)立刻消失 @end@implementation ViewController //優(yōu)先級(jí)是最高的 - (void)loadView{self.view = [[BGView alloc] initWithFrame:[UIScreen mainScreen].bounds];self.view.backgroundColor = [UIColor whiteColor]; }- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//根據(jù)某一個(gè)范圍 創(chuàng)建動(dòng)畫(huà)者對(duì)象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2根據(jù)某一個(gè)動(dòng)力學(xué)元素,創(chuàng)建行為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)];//以一個(gè)自定義的矩形框 為邊界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]; //重繪};//設(shè)置代理collision.collisionDelegate = self;//4把行為添加到動(dòng)畫(huà)者當(dāng)中[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{//強(qiáng)制轉(zhuǎn)化 NSString *str = (NSString *) identifier;if( [str isEqualToString:@"key1"]){self.redView.backgroundColor = [UIColor orangeColor];}else{self.redView.backgroundColor =[UIColor redColor];} }@end08-甩行為
// // 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{//獲取觸摸對(duì)象UITouch *t = touches.anyObject;CGPoint p = [t locationInView:t.view];//1動(dòng)畫(huà)者對(duì)象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2創(chuàng)建行為UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:p];//damping 阻尼 值越小 晃動(dòng)越大 0 - 2。 減速 屬性 snap.damping = 2;//3把行為添加到動(dòng)畫(huà)者當(dāng)中[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; }@end09-附著行為
// // 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{//獲取觸摸對(duì)象UITouch *t = touches.anyObject;CGPoint p = [t locationInView:t.view];//1動(dòng)畫(huà)者對(duì)象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2創(chuàng)建行為//剛性附著self.attach = [[UIAttachmentBehavior alloc] initWithItem:self.redView attachedToAnchor:p];self.attach.length = 100;UIGravityBehavior * gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];//3把行為添加到動(dòng)畫(huà)者當(dāng)中[self.animator addBehavior:gravity];[self.animator addBehavior:self.attach];}- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//獲取觸摸對(duì)象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總結(jié)
以上是生活随笔為你收集整理的IOS基础之UIDynamicAnimator动力学入门-01的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 苹果面试8大难题及答案
- 下一篇: IOS基础之UIDynamicAnima