日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/18 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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; }@end

11-附著行為-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;}@end

12-附著行為-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; }@end

13-推行為

// // 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;}@end

Vector 是向量

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。