swift 基础之UIDynamicAnimation
来源:互联网 发布:大鹏教育怎么样知乎 编辑:程序博客网 时间:2024/06/07 03:37
class ViewController: UIViewController, UICollisionBehaviorDelegate { @IBOutlet weak var gravity: UIButton! @IBOutlet weak var push: UIButton! @IBOutlet weak var attachment: UIButton! var collision: UICollisionBehavior! @IBAction func gravity(sender: UIButton) { animator.removeAllBehaviors() //gravity, push and attachment let gravity = UIGravityBehavior(items: [self.gravity,self.push,self.attachment]) animator.addBehavior(gravity) collision = UICollisionBehavior(items: [self.push, self.gravity, self.attachment]) collision.translatesReferenceBoundsIntoBoundary = true animator.addBehavior(collision) } @IBAction func push(sender: AnyObject) { animator.removeAllBehaviors() let push = UIPushBehavior(items: [self.gravity,self.push,self.attachment], mode:UIPushBehaviorMode.Instantaneous) push.magnitude = 2 animator.addBehavior(push) collision = UICollisionBehavior(items: [self.push, self.gravity, self.attachment]) collision.translatesReferenceBoundsIntoBoundary = true animator.addBehavior(collision) } var attachmentBehavior: UIAttachmentBehavior? = nil @IBAction func attachment(sender: AnyObject) { animator.removeAllBehaviors() let anchorPoint = CGPointMake(self.attachment.center.x, self.attachment.center.y) attachmentBehavior = UIAttachmentBehavior(item: self.attachment, attachedToAnchor: anchorPoint) attachmentBehavior!.frequency = 0.5 attachmentBehavior!.damping = 2 attachmentBehavior!.length = 20 animator.addBehavior(attachmentBehavior!) collision = UICollisionBehavior(items: [self.push, self.gravity, self.attachment]) collision.translatesReferenceBoundsIntoBoundary = true animator.addBehavior(collision) } @IBAction func handleAttachment(sender: UIPanGestureRecognizer) { if((attachmentBehavior) != nil){ attachmentBehavior!.anchorPoint = sender.locationInView(self.view) } } var animator = UIDynamicAnimator() override func viewDidLoad() { super.viewDidLoad() animator = UIDynamicAnimator(referenceView: self.view) } override func viewDidAppear(animated: Bool) { let max: CGRect = UIScreen.mainScreen().bounds let snap1 = UISnapBehavior(item: self.gravity, snapToPoint: CGPointMake(max.size.width/2, max.size.height/2 - 50)) let snap2 = UISnapBehavior(item: self.push, snapToPoint: CGPointMake(max.size.width/2, max.size.height/2 )) let snap3 = UISnapBehavior(item: self.attachment, snapToPoint: CGPointMake(max.size.width/2, max.size.height/2 + 50)) snap1.damping = 1 snap2.damping = 2 snap3.damping = 4 animator.addBehavior(snap1) animator.addBehavior(snap2) animator.addBehavior(snap3) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
点击MOB
动画静止
点击IRON,落到最下面
点击HACK随意撞击
0 0
- swift 基础之UIDynamicAnimation
- swift基础之<二>
- swift之Button基础
- swift基础之协议
- Swift基础之UIButton
- Swift基础之UITextField
- swift基础之?和!
- Swift 学习笔记之基础
- Swift基础常识之变量
- Swift基础常识之变量
- Swift基础之对话框UIAlertController
- swift学习之基础语法
- swift之基础语法笔记
- swift基础之错误异常
- swift基础UI之 UITableView
- swift基础之json解析
- 猫猫学swift之基础(一)
- swift详解之二 ------- swift中的几种基础类型
- 小玩文件3
- Hello, Python!
- Android项目中查找一个词,Eclipse 使用技巧之 ---- 查看本类调用和被调用列表
- android中Webview与javascript的交互(互相调用)
- 第一份csdn博客
- swift 基础之UIDynamicAnimation
- Android和IOS录制mp3语音文件的方法
- MFC多线程的创建
- 第15周 程序阅读五
- Android返回不退出
- Stdio快捷键
- 第十四周项目一排序函数模板
- MySQL设计规约
- NYOJ-769乘数密码,逆元解法;