UIDynamic

用途:

     从一个点移动到另外一个点;

相关属性:

    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 @end
17 
18 @implementation YFPushBehaviorViewController
19 - (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];  //p2
50     CGPoint squareViewCenterPoint = self.squareView.center;  //p1
51     
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
原文地址:https://www.cnblogs.com/bigshow1949/p/5806176.html