2014-11-24 139 views
6

我正在學習iOS開發。當我添加矩形視圖並希望受重力影響時,構建並運行我的測試應用程序,我發現它沒有重力。 這是viewDidLoad中的代碼在我viewController.m:爲什麼UIKit Dynamics不起作用?

[super viewDidLoad]; 

// Do any additional setup after loading the view, typically from a nib. 
UIView *squre = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; 
squre.backgroundColor = [UIColor grayColor]; 
[self.view addSubview:squre]; 

UIDynamicAnimator *_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; 
UIGravityBehavior *_gravity = [[UIGravityBehavior alloc] initWithItems:@[squre]]; 
[_animator addBehavior:_gravity]; 

回答

8

UIDynamicAnimator被釋放時viewDidLoad回報。如果它被釋放,它不能動畫。使屬性:

@property (nonatomic) UIDynamicAnimator *animator; 

,並設置:

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

噢,謝謝〜 – 2014-11-24 02:03:47