UIDynamic,iosuidynamic
用途:
從一個點移動到另外一個點;
相關屬性:
mode : UIPushBehaviorModeContinuous //推移模式
angle : setAngle //推移角度
magnitude : setMagnitude //速度 每1個magnigude將會引起100/平方秒的加速度
圖片說明:
p1 : squareView的中心點 p2 : 單擊的點
促使squareView朝著p2移動,因為加了UICollisionBehavior,所以移動時又不會超過邊界
代碼:
1 // 2 // YFPushBehaviorViewController.m 3 // BigShow1949 4 // 5 // Created by apple on 16/8/25. 6 // Copyright © 2016年 BigShowCompany. All rights reserved. 7 // 8 9 #import "YFPushBehaviorViewController.h"10 11 @interface YFPushBehaviorViewController ()12 @property(nonatomic,strong)UIDynamicAnimator *animator;13 @property (nonatomic, strong) UIView *squareView;14 @property (nonatomic, strong) UIPushBehavior *pushBehavior;15 16 @end17 18 @implementation YFPushBehaviorViewController19 - (void)viewDidLoad {20 21 [super viewDidLoad];22 self.view.backgroundColor = [UIColor whiteColor];23 24 }25 26 - (void)viewDidAppear:(BOOL)animated{27 [super viewDidAppear:animated];28 29 // 建立一個正方形30 self.squareView =[[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)];31 self.squareView.backgroundColor = [UIColor greenColor];32 self.squareView.center = self.view.center;33 [self.view addSubview:self.squareView];34 35 // 視圖單機手勢36 [self createGestureRecognizer];37 38 [self createAnimatorAndBehaviors];39 }40 41 - (void) createGestureRecognizer{42 UITapGestureRecognizer *tapGestureRecognizer =43 [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];44 [self.view addGestureRecognizer:tapGestureRecognizer];45 }46 47 - (void) handleTap:(UITapGestureRecognizer *)paramTap{48 49 CGPoint tapPoint = [paramTap locationInView:self.view]; //p250 CGPoint squareViewCenterPoint = self.squareView.center; //p151 52 CGFloat deltaX = tapPoint.x - squareViewCenterPoint.x;53 CGFloat deltaY = tapPoint.y - squareViewCenterPoint.y;54 CGFloat angle = atan2(deltaY, deltaX);55 [self.pushBehavior setAngle:angle]; //推移的角度56 57 //勾股58 CGFloat distanceBetweenPoints =59 sqrt(pow(tapPoint.x - squareViewCenterPoint.x, 2.0) +60 pow(tapPoint.y - squareViewCenterPoint.y, 2.0));61 //double pow(double x, double y);計算以x為底數的y次冪62 //double sqrt (double);開平方63 64 //推力的大小(移動速度)65 [self.pushBehavior setMagnitude:distanceBetweenPoints / 50.0f];66 //每1個magnigude將會引起100/平方秒的加速度,這裡分母越大,速度越小67 68 }69 - (void) createSmallSquareView{70 self.squareView =[[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)];71 72 self.squareView.backgroundColor = [UIColor greenColor];73 self.squareView.center = self.view.center;74 75 [self.view addSubview:self.squareView];76 }77 - (void) createAnimatorAndBehaviors{78 self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];79 80 /* Create collision detection */81 UICollisionBehavior *collision = [[UICollisionBehavior alloc]82 initWithItems:@[self.squareView]];83 collision.translatesReferenceBoundsIntoBoundary = YES;84 85 self.pushBehavior = [[UIPushBehavior alloc]86 initWithItems:@[self.squareView]87 mode:UIPushBehaviorModeContinuous];88 89 [self.animator addBehavior:collision];90 [self.animator addBehavior:self.pushBehavior];91 }92 93 @end