UIDynamic,iosuidynamic

來源:互聯網
上載者:User

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.