Why is UIKit Dynamics not working?

I am studying iOS development now. When I add a direct view and want it to be done by gravity, create and run my test application, I find that it does not have gravity. This is the viewDidLoad code in my viewController.m:

[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]; 
+6
source share
1 answer

Your UIDynamicAnimator freed when returning viewDidLoad . He cannot revive anything if he is free. Make a property:

 @property (nonatomic) UIDynamicAnimator *animator; 

And install it:

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

Source: https://habr.com/ru/post/978596/


All Articles