Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "ViewController.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- aSphere = [[Sphere alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
- [aSphere setBackgroundColor:[UIColor redColor]];
- [aSphere setUserInteractionEnabled:YES];
- [[aSphere layer]setCornerRadius:[aSphere bounds].size.width / 2];
- [[self view]addSubview:aSphere];
- bSphere = [[Sphere alloc]initWithFrame:CGRectMake(220, 200, 50, 50)];
- [bSphere setBackgroundColor:[UIColor greenColor]];
- [bSphere setUserInteractionEnabled:YES];
- [[bSphere layer]setCornerRadius:[bSphere bounds].size.width / 2];
- [[self view]addSubview:bSphere];
- cSphere = [[Sphere alloc]initWithFrame:CGRectMake(380, 300, 50, 50)];
- [cSphere setBackgroundColor:[UIColor blueColor]];
- [cSphere setUserInteractionEnabled:YES];
- [[cSphere layer]setCornerRadius:[cSphere bounds].size.width / 2];
- [[self view]addSubview:cSphere];
- dSphere = [[Sphere alloc]initWithFrame:CGRectMake(self.view.center.x, self.view.center.y, 50, 50)];
- [dSphere setCenter:self.view.center];
- [dSphere setBackgroundColor:[UIColor orangeColor]];
- [dSphere setUserInteractionEnabled:YES];
- [[dSphere layer]setCornerRadius:[dSphere bounds].size.width / 2];
- [[self view]addSubview:dSphere];
- UITapGestureRecognizer *aTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
- [aTap setNumberOfTapsRequired:1];
- [aSphere addGestureRecognizer:aTap];
- animator = [[UIDynamicAnimator alloc]initWithReferenceView:[self view]];
- [animator setDelegate:self];
- gravity = [[UIGravityBehavior alloc]init];
- [animator addBehavior:gravity];
- collision = [[UICollisionBehavior alloc]initWithItems:@[aSphere, bSphere, cSphere, dSphere]];
- [collision setTranslatesReferenceBoundsIntoBoundary:YES];
- [animator addBehavior:collision];
- UIDynamicItemBehavior *aSphereBehavior = [[UIDynamicItemBehavior alloc]initWithItems:@[aSphere]];
- [aSphereBehavior setElasticity:0.6];
- [animator addBehavior:aSphereBehavior];
- UIDynamicItemBehavior *bSphereBehavior = [[UIDynamicItemBehavior alloc]initWithItems:@[bSphere]];
- [bSphereBehavior setElasticity:0.3];
- [animator addBehavior:bSphereBehavior];
- UIDynamicItemBehavior *cSphereBehavior = [[UIDynamicItemBehavior alloc]initWithItems:@[cSphere]];
- [cSphereBehavior setElasticity:0.9];
- [animator addBehavior:cSphereBehavior];
- UIDynamicItemBehavior *dSphereBehavior = [[UIDynamicItemBehavior alloc]initWithItems:@[dSphere]];
- [dSphereBehavior setElasticity:0.2];
- [animator addBehavior:dSphereBehavior];
- }
- - (void)tap:(UITapGestureRecognizer *)tapRecog
- {
- Sphere *view = (Sphere *)[tapRecog view];
- if (snap) {
- [animator removeBehavior:snap];
- }
- CGPoint point = self.view.center;
- UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc]initWithItem:view attachedToAnchor:view.center];
- [animator addBehavior:attachment];
- attachment.frequency = 10.0;
- attachment.damping = 50.0;
- attachment.anchorPoint = point;
- UIDynamicItemBehavior *resistance = [[UIDynamicItemBehavior alloc]initWithItems:@[view]];
- resistance.resistance = 50;
- [animator addBehavior:resistance];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement