我有一个按钮,每次尝试拖动时都想移动它,然后每次点击时都移动一个segue。对于拖动,我已经放置了该方法:
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);
}
正在调用,但不会移动。我还需要放置或调用什么?
最佳答案
我认为这可能对您有用:
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)
}
关于ios - UIButton touchDragInside不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36506391/