標籤:
我們談到了重力上述財產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 上