[英]Why UIKit Dynamics doesn`t work?
I am studying iOS development now. 我正在研究iOS开发。 When I add a rect view and want it be effected by the gravity, build and run my test app, I find it does`t have the gravity.
当我添加一个rect视图并希望它受重力影响,构建并运行我的测试应用程序时,我发现它确实没有引力。 This is the viewDidLoad code in my viewController.m :
这是我的viewController.m中的viewDidLoad代码:
[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];
Your UIDynamicAnimator
is deallocated when viewDidLoad
returns. 当
viewDidLoad
返回时,您的UIDynamicAnimator
被释放。 It can't animate anything if it's deallocated. 如果它被解除分配,它就无法动画。 Make a property:
建立一个属性:
@property (nonatomic) UIDynamicAnimator *animator;
And set it: 并设置它:
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.