IOS基础之UIDynamicAnimator动力学入门-02
生活随笔
收集整理的這篇文章主要介紹了
IOS基础之UIDynamicAnimator动力学入门-02
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS基礎之UIDynamicAnimator動力學入門-02
10-彈性附著
// // ViewController.m // 10-彈性附著 // // 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.damping = 0.5;//頻率self.attach.frequency = 2;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.7; }@end11-附著行為-Item和Item之間
// // ViewController.m // 11-附著行為-Item和Item之間 // // 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,weak)UIView *blueView; @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 attachedToItem:self.blueView];//減幅// self.attach.damping = 0.5;//頻率// self.attach.frequency = 2;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.7;UIView * blueView =[[UIView alloc] init ];blueView.backgroundColor = [UIColor blueColor];blueView.frame = CGRectMake(250, 100, 100, 100);[self.view addSubview:blueView];self.blueView = blueView;}@end12-附著行為-Item和Item之間偏移量
// // ViewController.m // 12-附著行為-Item和Item之間偏移量 // // 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 offsetFromCenter:UIOffsetMake(20, 20) attachedToAnchor:p];//減幅// self.attach.damping = 0.5;//頻率self.attach.frequency = 2;UIGravityBehavior * gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];//3把行為添加到動畫者當中[self.animator addBehavior:gravity];[self.animator addBehavior:self.attach];UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView]];collision.translatesReferenceBoundsIntoBoundary = YES;[self.animator addBehavior:collision];}- (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.7; }@end13-推行為
// // ViewController.m // 13-推行為 // // 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 *touch = touches.anyObject;CGPoint p = [touch locationInView:touch.view];//1動畫者對象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2創建行為// UIPushBehaviorModeContinuous, 持續的推力越來越快 // UIPushBehaviorModeInstantaneous。 瞬時的推力,越來越慢UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[self.redView] mode:UIPushBehaviorModeContinuous];//Vector 向量//計算偏移量CGFloat offsetX = (p.x - self.redView.center.x)/50;CGFloat offsetY = (p.y - self.redView.center.y)/50;push.pushDirection = CGVectorMake(-offsetX, -offsetY); // push.angle = M_PI; //9點鐘方向。 左邊方向。或者 坐標負半軸//量級 推力push.magnitude = 1;//3把行為添加到動畫者當中[self.animator addBehavior:push];} - (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;}@endVector 是向量
14-動力學元素自身屬性
// // ViewController.m // 14-動力學元素自身屬性 // // 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{//1動畫者對象self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//2創建行為UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView,self.blueView]];UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];collision.translatesReferenceBoundsIntoBoundary = YES;//動力學元素 自身屬性UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.blueView]];//彈力 // itemBehavior.elasticity = 1;//密度 // itemBehavior.density = 0.5;//摩擦力itemBehavior.friction = 0.5;//3把行為添加到動畫者當中[self.animator addBehavior:itemBehavior];[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(100, H-50, 50, 50);[self.view addSubview:blueView];self.blueView = blueView;}@end 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的IOS基础之UIDynamicAnimator动力学入门-02的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS基础之UIDynamicAnima
- 下一篇: 成大事必备9种能力 9种手段 9种心态(