Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. var speed : Double! = 0;
  2. var lastTouchPoint : CGPoint!;
  3. var attach : UIAttachmentBehavior! = nil;
  4. var itemBehavior : UIDynamicItemBehavior!;
  5. var snapping : UISnapBehavior! = nil;
  6. var currentBall : Ball! = nil;
  7.  
  8.  
  9. override init(frame: CGRect) {
  10. super.init(frame: frame);
  11. println( "-- initiated view --" );
  12.  
  13. self.backgroundColor = UIColor.orangeColor();
  14.  
  15. ball = Ball( frame: CGRect(x: 200, y: 200, width: 50, height: 50 ) );
  16. self.addSubview(ball);
  17.  
  18. ball2 = Ball( frame: CGRect(x: 100, y: 100, width: 50, height: 50 ) );
  19. self.addSubview(ball2);
  20.  
  21. setup()
  22.  
  23. self.addGestureRecognizer( UIPanGestureRecognizer(target: self, action: "panHandler:") );
  24.  
  25. self.attach = UIAttachmentBehavior(item: self, attachedToAnchor: CGPoint(x: 0, y: 0) );
  26.  
  27. }
  28.  
  29.  
  30.  
  31. func setup() {
  32. animator = UIDynamicAnimator(referenceView:self);
  33.  
  34. var collision: UICollisionBehavior!
  35. collision = UICollisionBehavior(items: [ball, ball2])
  36. collision.translatesReferenceBoundsIntoBoundary = false;
  37. collision.collisionDelegate = self;
  38. animator?.addBehavior(collision)
  39.  
  40. var dynamicItem : UIDynamicItemBehavior = UIDynamicItemBehavior(items: [ball, ball2] );
  41. dynamicItem.allowsRotation = false;
  42. self.animator!.addBehavior(dynamicItem);
  43. }
  44.  
  45.  
  46.  
  47.  
  48. func panHandler( recognizr : UIPanGestureRecognizer ) {
  49.  
  50.  
  51. switch( recognizr.state ) {
  52. case UIGestureRecognizerState.Began:
  53.  
  54. let location = recognizr.locationInView(self);
  55. let view : AnyObject? = self.hitTest(location, withEvent: nil);
  56.  
  57. if( view is Ball ) {
  58.  
  59. currentBall = view as! Ball;
  60.  
  61. if( self.attach != nil ){ self.animator!.removeBehavior(attach); self.attach = nil; }
  62.  
  63.  
  64. self.attach = UIAttachmentBehavior(item: currentBall, attachedToAnchor: location );
  65. self.animator!.addBehavior(self.attach);
  66. self.attach.damping = 1;
  67. self.attach.length = 1;
  68.  
  69. let dynamicItem : UIDynamicItemBehavior = UIDynamicItemBehavior(items: [currentBall] );
  70. dynamicItem.allowsRotation = false;
  71.  
  72. self.animator!.addBehavior(dynamicItem);
  73. }
  74.  
  75.  
  76. break;
  77.  
  78. case UIGestureRecognizerState.Changed :
  79.  
  80. if( currentBall != nil ) {
  81. let location = recognizr.locationInView(self);
  82. self.attach.anchorPoint = location;
  83. }
  84.  
  85.  
  86. break;
  87.  
  88.  
  89. default :
  90. println("--- snap ---");
  91.  
  92. if( self.currentBall == nil ) {return;}
  93.  
  94. self.animator!.removeBehavior(attach);
  95. self.attach = nil;
  96.  
  97.  
  98. var snap : UISnapBehavior = UISnapBehavior(item: self.currentBall, snapToPoint: self.currentBall.startingPoint );
  99. snap.damping = 1;
  100.  
  101.  
  102. self.animator!.addBehavior(snap);
  103.  
  104. break;
  105.  
  106. }
  107.  
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement