Last active
August 29, 2015 14:23
-
-
Save KatagiriSo/b597c0ca88272d2d8a18 to your computer and use it in GitHub Desktop.
UIDynamicItem Gravity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class GrowView: UIView { | |
var anim : UIDynamicAnimator! | |
var grav : UIGravityBehavior! | |
var coll : UICollisionBehavior! | |
required init(coder aDecoder: NSCoder) { | |
self.anim = nil | |
self.grav = nil | |
self.coll = nil | |
super.init(coder: aDecoder) | |
} | |
override func awakeFromNib() { | |
self.anim = UIDynamicAnimator(referenceView: self) | |
let v = UIView(frame: CGRectMake(10,10,30,30)) | |
v.backgroundColor = UIColor.redColor() | |
self.addSubview(v) | |
self.grav = UIGravityBehavior(items: [v]) | |
self.coll = UICollisionBehavior(items: [v]) | |
self.anim.addBehavior(grav) | |
self.anim.addBehavior(coll) | |
coll.addBoundaryWithIdentifier("bottom", fromPoint: CGPointMake(0, self.frame.height), toPoint: CGPointMake(self.frame.width, self.frame.height)) | |
} | |
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { | |
let t = touches.first | |
let loc = t?.locationInView(self) | |
let v = UIView(frame: CGRectMake(loc!.x,loc!.y,3,10)) | |
let c = [UIColor.redColor(), UIColor.greenColor(), UIColor.grayColor(), UIColor.whiteColor() | |
, UIColor.purpleColor(), UIColor.yellowColor(), UIColor.magentaColor()] | |
let index = Int(arc4random() % (UInt32(c.count))) | |
v.backgroundColor = c[index] | |
v.transform = CGAffineTransformMakeRotation(loc!.x / (loc!.y + 0.01)) | |
self.addSubview(v) | |
self.grav.addItem(v) | |
self.coll.addItem(v) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment