iOS 物理方面

來源:互聯網
上載者:User

標籤:

一,重力與碰撞 

//建立物理模擬器

         UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

         self.animator = animator;  //強引用animator,否則代碼塊執行完成後,將被釋放

    

         //建立重力行為

         UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.view1]];

    

         //設定一些屬性(可以不設定,不設則都為預設)

         gravityBehavior.gravityDirection = CGVectorMake(0, 1);  //重力方向

         gravityBehavior.angle = M_PI*0.5;  //重力方向

         gravityBehavior.magnitude = 0.5; //重力加速度,1代表加速度是(每秒100個點)

    

         //把重力行為行為添加到模擬器

         [animator addBehavior:gravityBehavior];

    

    

         //建立碰撞行為

         UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.view1,self.view2]];

    

         //設定碰撞邊界,不設定就會飛出螢幕,設定就會在螢幕邊框處產生碰撞效果

         collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;

    

         //將碰撞行為加入物理模擬器

         [animator addBehavior:collisionBehavior];

 

二,捕捉行為

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  {      //擷取觸摸點      UITouch *touch = [touches anyObject];      CGPoint point = [touch locationInView:touch.view];            //建立模擬模擬器      UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];      self.animator = animator;  //模擬器          //建立捕捉行為     UISnapBehavior *snapBehavior = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:point];          //設定反彈係數(反彈幅度越大,值越大,反彈幅度越小)     snapBehavior.damping = 0.5;          //將行為添加到模擬器     [animator addBehavior:snapBehavior];      }

iOS 物理方面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.