I'm trying to learn UIKit Dynamics.
I have created a box view and added the UIGravityBehavior on it, but nothing happens when I run the Project!
Viewcontroller.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* box = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 100, 100)];
box.backgroundColor = [UIColor blackColor];
[self.view addSubview:box];
UIGravityBehavior* gravityBehaviour = [[UIGravityBehavior alloc]init];
[gravityBehaviour addItem:box];
UIDynamicAnimator* myAnimator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
[myAnimator addBehavior:gravityBehaviour];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
This is my view controller.m file.
What is wrong in the program?
If you want to use it in ViewDidLoad, you need to announce UIDynamicAnimator and UIGravityBehavior as properties of the current VC. The Automatic Reference Counting will deallocate those objects after ViewDidLoad finishing.
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With