Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()
  4.  
  5. @end
  6.  
  7. @implementation ViewController
  8.  
  9. - (void)viewDidLoad
  10. {
  11.     [super viewDidLoad];
  12.    
  13.     aSphere = [[Sphere alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
  14.     [aSphere setBackgroundColor:[UIColor redColor]];
  15.     [aSphere setUserInteractionEnabled:YES];
  16.     [[aSphere layer]setCornerRadius:[aSphere bounds].size.width / 2];
  17.     [[self view]addSubview:aSphere];
  18.    
  19.     bSphere = [[Sphere alloc]initWithFrame:CGRectMake(220, 200, 50, 50)];
  20.     [bSphere setBackgroundColor:[UIColor greenColor]];
  21.     [bSphere setUserInteractionEnabled:YES];
  22.     [[bSphere layer]setCornerRadius:[bSphere bounds].size.width / 2];
  23.     [[self view]addSubview:bSphere];
  24.    
  25.     cSphere = [[Sphere alloc]initWithFrame:CGRectMake(380, 300, 50, 50)];
  26.     [cSphere setBackgroundColor:[UIColor blueColor]];
  27.     [cSphere setUserInteractionEnabled:YES];
  28.     [[cSphere layer]setCornerRadius:[cSphere bounds].size.width / 2];
  29.     [[self view]addSubview:cSphere];
  30.    
  31.     dSphere = [[Sphere alloc]initWithFrame:CGRectMake(self.view.center.x, self.view.center.y, 50, 50)];
  32.     [dSphere setCenter:self.view.center];
  33.     [dSphere setBackgroundColor:[UIColor orangeColor]];
  34.     [dSphere setUserInteractionEnabled:YES];
  35.     [[dSphere layer]setCornerRadius:[dSphere bounds].size.width / 2];
  36.     [[self view]addSubview:dSphere];
  37.    
  38.     UITapGestureRecognizer *aTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
  39.     [aTap setNumberOfTapsRequired:1];
  40.    
  41.     [aSphere addGestureRecognizer:aTap];
  42.    
  43.     animator = [[UIDynamicAnimator alloc]initWithReferenceView:[self view]];
  44.     [animator setDelegate:self];
  45.     gravity = [[UIGravityBehavior alloc]init];
  46.     [animator addBehavior:gravity];
  47.    
  48.     collision = [[UICollisionBehavior alloc]initWithItems:@[aSphere, bSphere, cSphere, dSphere]];
  49.     [collision setTranslatesReferenceBoundsIntoBoundary:YES];
  50.     [animator addBehavior:collision];
  51.    
  52.     UIDynamicItemBehavior *aSphereBehavior = [[UIDynamicItemBehavior alloc]initWithItems:@[aSphere]];
  53.     [aSphereBehavior setElasticity:0.6];
  54.     [animator addBehavior:aSphereBehavior];
  55.    
  56.     UIDynamicItemBehavior *bSphereBehavior = [[UIDynamicItemBehavior alloc]initWithItems:@[bSphere]];
  57.     [bSphereBehavior setElasticity:0.3];
  58.     [animator addBehavior:bSphereBehavior];
  59.    
  60.     UIDynamicItemBehavior *cSphereBehavior = [[UIDynamicItemBehavior alloc]initWithItems:@[cSphere]];
  61.     [cSphereBehavior setElasticity:0.9];
  62.     [animator addBehavior:cSphereBehavior];
  63.    
  64.     UIDynamicItemBehavior *dSphereBehavior = [[UIDynamicItemBehavior alloc]initWithItems:@[dSphere]];
  65.     [dSphereBehavior setElasticity:0.2];
  66.     [animator addBehavior:dSphereBehavior];
  67. }
  68.  
  69. - (void)tap:(UITapGestureRecognizer *)tapRecog
  70. {
  71.     Sphere *view = (Sphere *)[tapRecog view];
  72.    
  73.     if (snap) {
  74.         [animator removeBehavior:snap];
  75.     }
  76.    
  77.     CGPoint point = self.view.center;
  78.    
  79.     UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc]initWithItem:view attachedToAnchor:view.center];
  80.     [animator addBehavior:attachment];
  81.     attachment.frequency = 10.0;
  82.     attachment.damping = 50.0;
  83.     attachment.anchorPoint = point;
  84.    
  85.     UIDynamicItemBehavior *resistance = [[UIDynamicItemBehavior alloc]initWithItems:@[view]];
  86.     resistance.resistance = 50;
  87.     [animator addBehavior:resistance];
  88. }
  89.  
  90. - (void)didReceiveMemoryWarning
  91. {
  92.     [super didReceiveMemoryWarning];
  93. }
  94.  
  95. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement