2016-04-08 77 views
0

我有一個按鈕,我想每次嘗試拖動時移動,然後在每次點擊時移動一個segue。對於拖我已經把這種方法:UIButton touchDragInside不工作

skillsButtonReal.addTarget(self, action: #selector(ViewController.wasDragged(_:event:)), forControlEvents: UIControlEvents.TouchDragInside) 

func wasDragged (buttn : UIButton, event :UIEvent) 
{ 

    let buttonView = buttn as UIView; 
    let touches : Set<UITouch> = event.touchesForView(buttonView)! 
    let touch = touches.first! 
    let location = touch.locationInView(buttonView) 

    let previousLocation : CGPoint = touch .previousLocationInView(buttonView) 
    let delta_x :CGFloat = location.x - previousLocation.x 
    let delta_y :CGFloat = location.y - previousLocation.y 
    buttn.center = CGPointMake(buttn.center.x + delta_x, 
           buttn.center.y + delta_y); 

} 

哪個被調用,但它不會移動。我還需要放置或調用什麼?

回答

0

我認爲這可能是對你有用:

var panGesture: UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector("recognizePanGesture:")) 
panGesture.minimumNumberOfTouches = 1 
panGesture.maximumNumberOfTouches = 1 
buttonView.addGestureRecognizer(panGesture) 


func recognizePanGesture(sender: UIPanGestureRecognizer) 
{ 
    var translate = sender.translationInView(self.view) 
    sender.view!.center = CGPoint(x:sender.view!.center.x + translate.x, 
    y:sender.view!.center.y + translate.y) 
    sender.setTranslation(CGPointZero, inView: self.view) 
} 
+0

謝謝您的回覆,但現在我有2個問題。首先,一個是該按鈕不會拖動(我應該將視圖的第一部分附加到正確的按鈕上,第二部分放在方法中),另一個問題是該按鈕不會延續到下一個VC。我在那裏做錯了什麼? – Sergio

+0

這個答案是合法的,但他問的是「Touch Drag Inside」,這是一個與UIButtons一起使用的真實事件... –

+0

有什麼我可以做的嗎? – Sergio