iOS7 UIKit動力學-碰撞特性UICollisionBehavior 上

來源:互聯網
上載者:User

標籤:

我們談到了重力上述財產UIGravityBehavior這個類。

非常明確的看法,當我們添加的屬性的嚴重性後,,蘋果UIview像掉進無底洞,地下墜,不斷的加速。而如今呢,我們要在這個手機螢幕上,加入一個地面。使不斷下落的蘋果終於有一個著陸點。那麼我們怎樣為這個視圖加入一個地面呢。例如以下(當前內容承接上文內容,如有問題。請看上文:UIGravityBehavior):

首先在.h檔案裡建立一個UICollisionBehavior對象

.h:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    UIDynamicAnimator * _animator;    UIGravityBehavior * _gravity;    //new    UICollisionBehavior * _ground;}@end

然後在.m檔案裡為UICollisionBehavior對象進行初始化。並為apple對象設定邊框屬性。

- (void)viewDidLoad{    [super viewDidLoad];    UIView * apple = [[UIView alloc] initWithFrame:CGRectMake(40,40, 40, 40)];    apple.backgroundColor = [UIColor redColor];    [self.view addSubview:apple];        _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];    _gravity = [[UIGravityBehavior alloc] initWithItems:@[apple]];    [_animator addBehavior:_gravity];        /*   UICollisionBehavior   */    _ground = [[UICollisionBehavior alloc] initWithItems:@[apple]];    _ground.translatesReferenceBoundsIntoBoundary = YES;    [_animator addBehavior:_ground];}

這裡,以上的代碼建立了一個碰撞的行為。它定義了一個或者多個邊界互動的特性,這裡沒有顯示的為各個空間加入邊界,而是隱式的調用了一個UICollisionBehavior的屬性translatesReferenceBoundsIntoBoundary,將這個屬性設定為YES之後。

會使邊界引用使用視圖提供的UIDynamicAnimator邊界。構建和執行,你就會看到效果,apple落到地上彈起來了,又又一次落到地上。



點擊關注我。很多其它精彩內容!。!

群號:336146073

著作權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。

iOS7 UIKit動力學-碰撞特性UICollisionBehavior 上

聯繫我們

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