Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 快速-将按钮从一个位置拖动到另一个位置_Swift - Fatal编程技术网

Swift 快速-将按钮从一个位置拖动到另一个位置

Swift 快速-将按钮从一个位置拖动到另一个位置,swift,Swift,我正在尝试使用UITouch将按钮从一个位置拖到另一个位置。但我不能拖。我在添加按钮目标时遇到问题 我的代码- override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let btn_swap = UIButton.buttonWithType(UIButtonType.Custom

我正在尝试使用UITouch将按钮从一个位置拖到另一个位置。但我不能拖。我在添加按钮目标时遇到问题

我的代码-

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
     let btn_swap = UIButton.buttonWithType(UIButtonType.Custom) as UIButton!
    btn_swap .setTitle("Drag Me", forState: UIControlState.Normal)
    btn_swap.backgroundColor = UIColor.yellowColor()

    btn_swap.addTarget(self, action: "wasDragged:", forControlEvents: UIControlEvents.TouchDragInside)

    btn_swap.frame = CGRectMake((self.view.bounds.size.width - 100)/2.0,
        (self.view.bounds.size.height - 50)/2.0,
        100, 50)

    self.view.addSubview(btn_swap)
    self.creation_of_btn()
 }

func wasDragged (buttn : UIButton, event :UIEvent)
{
    var touch : UITouch = event.touchesForView(buttn) . anyObject() as UITouch
    var previousLocation : CGPoint = touch .previousLocationInView(buttn)
     var location : CGPoint = touch .locationInView(buttn)
    var delta_x :CGFloat = location.x - previousLocation.x
      var delta_y :CGFloat = location.y - previousLocation.y
    buttn.center = CGPointMake(buttn.center.x + delta_x,
        buttn.center.y + delta_y);

}

您为按钮指定了错误的选择器
被拖动:
。因为你的行动方法看起来像

func wasDragged (buttn : UIButton, event :UIEvent)
{
}
slector应该是
wasdrable:event:

btn_swap.addTarget(self, action: "wasDragged:event:", forControlEvents: UIControlEvents.TouchDragInside)

事件TouchDragInside还需要将事件参数作为第二个参数传递

Swift 1:

Swift 2:

btn_swap.addTarget(self,action: #selector(wasDragged(_:event:)),forControlEvents: .TouchDragInside)

func wasDragged(btnVar : UIButton, evtVar :UIEvent)
{
    let touch : UITouch = (evtVar.touchesForView(btnVar)?.first)! as UITouch
    let previousLocation : CGPoint = touch .previousLocationInView(btnVar)
    let location : CGPoint = touch .locationInView(btnVar)
    let delta_x :CGFloat = location.x - previousLocation.x
    let delta_y :CGFloat = location.y - previousLocation.y
    btnVar.center = CGPointMake(btnVar.center.x + delta_x,
                               btnVar.center.y + delta_y);
}
Swift 4.2

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


btn_swap.addTarget(self,action: #selector(wasDragged(buttn:event:)),for: .touchDragInside)

它正在工作,兄弟。。。谢谢请编辑并删除wasDragged:event:hi之间的空格,我是在swift3中完成的。但我如何检测结束位置,因为我想在他的位置上重新设置按钮?感谢delta的代码,这很好