- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// target
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[redView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:redView];
// blocker
UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(10, 350, 300, 10)];
[greenView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:greenView];
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[redView]];
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[redView,greenView]];
[self.animator addBehavior:gravityBeahvior];
[self.animator addBehavior:collisionBehavior];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// target
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[redView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:redView];
// blocker
UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(10, 350, 300, 10)];
[greenView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:greenView];
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[redView]];
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[redView,greenView]];
UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:greenView attachedToAnchor:CGPointMake(160, 355)];
[self.animator addBehavior:gravityBeahvior];
[self.animator addBehavior:collisionBehavior];
[self.animator addBehavior:attachmentBehavior];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// target
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[redView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:redView];
// blocker
UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(10, 350, 300, 10)];
[greenView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:greenView];
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(0, 360, 30, 10)];
[subview1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:subview1];
UIView *subview2 = [[UIView alloc] initWithFrame:CGRectMake(280, 360, 30, 10)];
[subview2 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:subview2];
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[redView]];
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[redView,greenView,subview1,subview2]];
UIAttachmentBehavior *leftAttachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:subview1 attachedToAnchor:subview1.center];
UIAttachmentBehavior *rightAttachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:subview2 attachedToAnchor:subview2.center];
[self.animator addBehavior:gravityBeahvior];
[self.animator addBehavior:collisionBehavior];
[self.animator addBehavior:leftAttachmentBehavior];
[self.animator addBehavior:rightAttachmentBehavior];
}