1。从一点移到另一点
2。相关属性
mode : UIPushBehaviorModeContinuous //推移模式
angle : setAngle //推移角度
magnitude : setMagnitude //速度 每1个magnigude将会引起100/平方秒的加速度
e.g.
p1 : squareView的中心点 p2 : 单击的点
促使squareView朝着p2移动,因为加了UICollisionBehavior,所以移动时又不会超过边界
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self createGestureRecognizer];
[self createSmallSquareView];
[self createAnimatorAndBehaviors];
}
- (void) createGestureRecognizer{ //侦测视图单击
UITapGestureRecognizer *tapGestureRecognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.view addGestureRecognizer:tapGestureRecognizer];
}
- (void) handleTap:(UITapGestureRecognizer *)paramTap{
CGPoint tapPoint = [paramTap locationInView:self.view]; //p2
CGPoint squareViewCenterPoint = self.squareView.center; //p1
CGFloat deltaX = tapPoint.x - squareViewCenterPoint.x;
CGFloat deltaY = tapPoint.y - squareViewCenterPoint.y;
CGFloat angle = atan2(deltaY, deltaX);
[self.pushBehavior setAngle:angle]; //推移的角度
//勾股
CGFloat distanceBetweenPoints =
sqrt(pow(tapPoint.x - squareViewCenterPoint.x, 2.0) +
pow(tapPoint.y - squareViewCenterPoint.y, 2.0));
//double pow(double x, double y);计算以x为底数的y次幂
//double sqrt (double);开平方
[self.pushBehavior setMagnitude:distanceBetweenPoints / 200.0f]; //推力的大小(移动速度)
//每1个magnigude将会引起100/平方秒的加速度,这里分母越大,速度越小
}
- (void) createSmallSquareView{
self.squareView =[[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)];
self.squareView.backgroundColor = [UIColor greenColor];
self.squareView.center = self.view.center;
[self.view addSubview:self.squareView];
}
- (void) createAnimatorAndBehaviors{
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
/* Create collision detection */
UICollisionBehavior *collision = [[UICollisionBehavior alloc]
initWithItems:@[self.squareView]];
collision.translatesReferenceBoundsIntoBoundary = YES;
self.pushBehavior = [[UIPushBehavior alloc]
initWithItems:@[self.squareView]
mode:UIPushBehaviorModeContinuous];
[self.animator addBehavior:collision];
[self.animator addBehavior:self.pushBehavior];
}