如何在swift 4中拖动UIImage

如何在swift 4中拖动UIImage,swift,Swift,我目前有代码可以将UIImage移动到我在屏幕上点击的任何位置,但是我的应用程序要求用户能够在屏幕上拖动图像,而我当前的代码不能做到这一点。我对这门语言还不熟悉,所以任何帮助都将不胜感激。以下是获取触摸位置的当前代码: override func-touchsbegind(touch:Set,with-event:UIEvent?{如果让touch=touch.first{让location=touch.location(in:self.view)您可以通过实现touchsbegind()和to

我目前有代码可以将UIImage移动到我在屏幕上点击的任何位置,但是我的应用程序要求用户能够在屏幕上拖动图像,而我当前的代码不能做到这一点。我对这门语言还不熟悉,所以任何帮助都将不胜感激。以下是获取触摸位置的当前代码:


override func-touchsbegind(touch:Set,with-event:UIEvent?{如果让touch=touch.first{让location=touch.location(in:self.view)
您可以通过实现
touchsbegind()
touchsmoved()自己完成
,但是你最好使用
uipangestrerecognzier
。我建议找一个示例项目,让你可以使用
uipangestrerecognzier
拖动视图。这会让你省去一些麻烦。

我制作了一个立方体,它基本上是一个可以拖动的UIView,并且立方体内部的信息会发生变化。我使用了f使用以下函数在视图中拖动多维数据集。查看是否有帮助

   var draggableCube = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))

@objc func panGestureDetected(panGestureRecognizer: UIPanGestureRecognizer) {
        
    let translation = panGestureRecognizer.translation(in: self.view)
    var changeX : CGFloat = 0
    var changeY : CGFloat = 0
    
    if translation.x + self.draggableCube.frame.maxX > self.view.bounds.maxX {
        // prevents it to go outside of the bounds from right side
        changeX = self.view.bounds.maxX - self.draggableCube.frame.maxX
    } else if translation.x + self.draggableCube.frame.minX < self.view.bounds.minX{
        // prevents it to go outside of the bounds from right side
        changeX = self.view.bounds.minX - self.draggableCube.frame.minX
    } else {
        // translation is within limits
        changeX = translation.x
    }
    if translation.y + self.draggableCube.frame.maxY > self.view.bounds.maxY {
        // prevents it to go outside of the bounds from bottom
        changeY = self.view.bounds.maxY - self.draggableCube.frame.maxY
    } else if translation.y + self.draggableCube.frame.minY < self.view.bounds.minY {
        // prevents it to go outside of the bounds from top
        changeY = self.view.bounds.minY - self.draggableCube.frame.minY
    } else {
        // translation is within limits
        changeY = translation.y
    }

    self.draggableCube.center = CGPoint(x: self.draggableCube.center.x + changeX, y: self.draggableCube.center.y + changeY)

    panGestureRecognizer.setTranslation(CGPoint.zero, in: self.view)

    if panGestureRecognizer.state == .ended {
        // implement action what you want to do after the gragging ended
    }
}
var draggableCube=UIView(帧:CGRect(x:0,y:0,宽度:20,高度:20))
@objc func PANGESTURE检测(PANGESTURE识别器:UIPANGESTURE识别器){
let translation=pangestrerecognizer.translation(在:self.view中)
变量changeX:CGFloat=0
变量changeY:CGFloat=0
如果translation.x+self.draggableCube.frame.maxX>self.view.bounds.maxX{
//防止它从右侧超出边界
changeX=self.view.bounds.maxX-self.draggableCube.frame.maxX
}如果translation.x+self.draggableCube.frame.minXself.view.bounds.maxY{
//防止它从底部超出边界
changeY=self.view.bounds.maxY-self.draggableCube.frame.maxY
}否则,如果translation.y+self.draggableCube.frame.minY
使用功能
触摸移动
。它将随着手指移动而不断被调用。ui测试识别器。