2015-12-18 82 views
1

我正在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) 
} 
+0

是否有按鈕的任何IBActions?不建議設置UIDynamicItem的任何位置或框架屬性,因爲它將與UIDynamicAnimator對抗。當你說:Ball1.center = location時,你應該使用UIAttachmentBehavior而不是直接設置中心。 – beyowulf

+0

我是編程新手,如何使用UIAttachmentBehavior? – Xcoder555

回答

1

您應該添加一個附件屬性,就像您對其他行爲所做的一樣。例如:

var attachment: UIAttachmentBehavior? 

在你的touchesBegan取代Ball1.center = location

if CGRectContainsPoint(Ball1.frame,location) 
{ 
    attachment = UIAttachmentBehavior(item: Ball1, attachedToAnchor: location) 
    animator!.addBehavior(attachment!) 
} 
if CGRectContainsPoint(Ball2.frame,location) 
{ 
    attachment = UIAttachmentBehavior(item: Ball2, attachedToAnchor: location) 
    animator!.addBehavior(attachment!) 
} 
if CGRectContainsPoint(Ball3.frame,location) 
{ 
    attachment = UIAttachmentBehavior(item: Ball3, attachedToAnchor: location) 
    animator!.addBehavior(attachment!) 
} 

然後在你的touchesMoved

if attachment != nil 
{ 
    attachment!.anchorPoint = location 
} 

更換Ball1.center = location然後實現touchesEnded

if animator != nil && attachment != nil 
{ 
    animator.removeBehavior(self.attachment!) 
    self.attachment = nil 
} 

我會添加你寫的代碼,因爲這樣會永遠導致Ball1跟隨用戶的觸摸。不知道這是你的意圖。

+0

它不是,我發佈了一個問題,關於如何讓按鈕可拖動,只有當我先按下它,但我沒有得到我能理解的答案。 – Xcoder555

+0

我認爲,我編輯的代碼更接近你想要的。有可能有更好的方法來實現你的願望,例如,你應該看看手勢識別器。 – beyowulf

+0

嗯。當我運行應用程序時,它崩潰了,並且在animator.removeBehavior(self.attachment!)代碼行中出現錯誤,該代碼行顯示「線程1:EXC_bad_instruction代碼= EXC_1386_INVOP,子代碼= 0x0」,您知道可能導致什麼原因那? – Xcoder555