Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIGravityBehavior isn't working

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?

like image 870
marmikshah Avatar asked Dec 07 '13 09:12

marmikshah


1 Answers

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

like image 150
zeejan Avatar answered Oct 04 '22 15:10

zeejan