我正在Swift中構建一個應用程序。我寫了一個UIGravityBehavior
,一個拖放功能,以及3個按鈕和地板UIImageView
之間的碰撞檢測,但是當按下其中一個按鈕時,該按鈕的UIGravityBehavior
不再存在。有一點幫助會很棒。拖放功能使問題Swift
import UIKit
class ViewController: UIViewController {
var location = CGPoint(x: 0, y: 0)
var animator: UIDynamicAnimator?
var gravity: UIGravityBehavior?
var isRotating = false
var collision: UICollisionBehavior!
@IBOutlet var Ball1: UIButton!
@IBOutlet var Ball2: UIButton!
@IBOutlet var Ball3: UIButton!
@IBOutlet var Floor: UIImageView!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first as UITouch!
location = touch.locationInView(self.view)
Ball1.center = location
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first as UITouch!
location = touch.locationInView(self.view)
Ball1.center = location
}
override func viewDidLoad() {
super.viewDidLoad()
self.animator = UIDynamicAnimator(referenceView: self.view)
let gravity = UIGravityBehavior (items: [self.Ball1!, self.Ball2!, self.Ball3!])
let direction = CGVectorMake(0.0, 1.0)
gravity.gravityDirection = direction
self.animator?.addBehavior(gravity)
collision = UICollisionBehavior(items: [ Ball1!, Ball2!, Ball3!])
collision.translatesReferenceBoundsIntoBoundary = true
collision.addBoundaryWithIdentifier("Floor", fromPoint: CGPointMake(self.view.frame.origin.x, -7), toPoint: CGPointMake(self.view.frame.origin.x + self.view.frame.width, -7))
animator!.addBehavior(collision)
}
是否有按鈕的任何IBActions?不建議設置UIDynamicItem的任何位置或框架屬性,因爲它將與UIDynamicAnimator對抗。當你說:Ball1.center = location時,你應該使用UIAttachmentBehavior而不是直接設置中心。 – beyowulf
我是編程新手,如何使用UIAttachmentBehavior? – Xcoder555