Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var speed : Double! = 0;
- var lastTouchPoint : CGPoint!;
- var attach : UIAttachmentBehavior! = nil;
- var itemBehavior : UIDynamicItemBehavior!;
- var snapping : UISnapBehavior! = nil;
- var currentBall : Ball! = nil;
- override init(frame: CGRect) {
- super.init(frame: frame);
- println( "-- initiated view --" );
- self.backgroundColor = UIColor.orangeColor();
- ball = Ball( frame: CGRect(x: 200, y: 200, width: 50, height: 50 ) );
- self.addSubview(ball);
- ball2 = Ball( frame: CGRect(x: 100, y: 100, width: 50, height: 50 ) );
- self.addSubview(ball2);
- setup()
- self.addGestureRecognizer( UIPanGestureRecognizer(target: self, action: "panHandler:") );
- self.attach = UIAttachmentBehavior(item: self, attachedToAnchor: CGPoint(x: 0, y: 0) );
- }
- func setup() {
- animator = UIDynamicAnimator(referenceView:self);
- var collision: UICollisionBehavior!
- collision = UICollisionBehavior(items: [ball, ball2])
- collision.translatesReferenceBoundsIntoBoundary = false;
- collision.collisionDelegate = self;
- animator?.addBehavior(collision)
- var dynamicItem : UIDynamicItemBehavior = UIDynamicItemBehavior(items: [ball, ball2] );
- dynamicItem.allowsRotation = false;
- self.animator!.addBehavior(dynamicItem);
- }
- func panHandler( recognizr : UIPanGestureRecognizer ) {
- switch( recognizr.state ) {
- case UIGestureRecognizerState.Began:
- let location = recognizr.locationInView(self);
- let view : AnyObject? = self.hitTest(location, withEvent: nil);
- if( view is Ball ) {
- currentBall = view as! Ball;
- if( self.attach != nil ){ self.animator!.removeBehavior(attach); self.attach = nil; }
- self.attach = UIAttachmentBehavior(item: currentBall, attachedToAnchor: location );
- self.animator!.addBehavior(self.attach);
- self.attach.damping = 1;
- self.attach.length = 1;
- let dynamicItem : UIDynamicItemBehavior = UIDynamicItemBehavior(items: [currentBall] );
- dynamicItem.allowsRotation = false;
- self.animator!.addBehavior(dynamicItem);
- }
- break;
- case UIGestureRecognizerState.Changed :
- if( currentBall != nil ) {
- let location = recognizr.locationInView(self);
- self.attach.anchorPoint = location;
- }
- break;
- default :
- println("--- snap ---");
- if( self.currentBall == nil ) {return;}
- self.animator!.removeBehavior(attach);
- self.attach = nil;
- var snap : UISnapBehavior = UISnapBehavior(item: self.currentBall, snapToPoint: self.currentBall.startingPoint );
- snap.damping = 1;
- self.animator!.addBehavior(snap);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement