UIDynamic-附著行為:UIAttachmentBehavior,

來源:互聯網
上載者:User

UIDynamic-附著行為:UIAttachmentBehavior,

直接上代碼:

 1 // 2 //  YFAttachmentBehaviorViewController.m 3 //  BigShow1949 4 // 5 //  Created by apple on 16/8/25. 6 //  Copyright © 2016年 BigShowCompany. All rights reserved. 7 // 8  9 #import "YFAttachmentBehaviorViewController.h"10 11 @interface YFAttachmentBehaviorViewController ()12 13 @property (nonatomic, strong) UIView *square1;14 @property(nonatomic,strong)UIDynamicAnimator *animator;15 @property (nonatomic, strong) UIAttachmentBehavior *attachmentBehavior;16 17 18 19 @end20 21 @implementation YFAttachmentBehaviorViewController22 23 - (void)viewDidLoad24 {25     [super viewDidLoad];26     27     self.view.backgroundColor = [UIColor whiteColor];28     29     // 建立一個正方形30     self.square1 =[[UIView alloc] initWithFrame: CGRectMake(0.0f, 568-80, 80.0f, 80.0f)];31     self.square1.backgroundColor = [UIColor greenColor];32     self.square1.center = self.view.center;33     [self.view addSubview:self.square1];34     35     36     self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];37 38     UICollisionBehavior* collision = [[UICollisionBehavior alloc] initWithItems:@[self.square1]];39     collision.translatesReferenceBoundsIntoBoundary = YES;40     41     UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.square1]];42     43     44     [self.animator addBehavior:collision];45     [self.animator addBehavior:gravity];46     47     48     // 視圖手勢49     [self createGestureRecognizer];50 }51 52 53 - (void)createGestureRecognizer{54     UIPanGestureRecognizer *tapGestureRecognizer =55     [[UIPanGestureRecognizer alloc] initWithTarget:self  action:@selector(handleTap:)];56     [self.view addGestureRecognizer:tapGestureRecognizer];57 }58 59 - (void)handleTap:(UIPanGestureRecognizer *)gesture{60     61     if (gesture.state == UIGestureRecognizerStateBegan){62         NSLog(@"----Began");63         64         CGPoint squareCenterPoint = CGPointMake(self.square1.center.x, self.square1.center.y - 100.0);65         66         UIAttachmentBehavior* attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.square1 attachedToAnchor:squareCenterPoint];67         68         self.attachmentBehavior = attachmentBehavior;69         [self.animator addBehavior:attachmentBehavior];70         71     } else if ( gesture.state == UIGestureRecognizerStateChanged) {72         NSLog(@"----Changed");73         [self.attachmentBehavior setAnchorPoint:[gesture locationInView:self.view]];74         75     } else if (gesture.state == UIGestureRecognizerStateEnded) {76         NSLog(@"----Ended");77         [self.animator removeBehavior:self.attachmentBehavior];78     }79 }80 81 82 83 @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.