Ios 如何使用swift拖放图像?

Ios 如何使用swift拖放图像?,ios,swift,touch,Ios,Swift,Touch,我的屏幕上有一个你可以拖放的图像。这已经奏效了。当我把手指放在中间时。 就像我把手指放在任何一个角落(或任何不是中间的东西)一样,图像的中间在我的手指下面。但我还是想要一个角落 这是我的密码: let frameDoor = CGRect(x: 100, y: 100, width: 200, height: 400) var doorView = ObjectView(frame: frameDoor) doorView.image = UIImage(named: "door") doorV

我的屏幕上有一个你可以拖放的图像。这已经奏效了。当我把手指放在中间时。 就像我把手指放在任何一个角落(或任何不是中间的东西)一样,图像的中间在我的手指下面。但我还是想要一个角落

这是我的密码:

let frameDoor = CGRect(x: 100, y: 100, width: 200, height: 400)
var doorView = ObjectView(frame: frameDoor)
doorView.image = UIImage(named: "door")
doorView.contentMode = .scaleAspectFit
doorView.isUserInteractionEnabled = true
self.view.addSubview(doorView)
ObjectView:

import UIKit

class ObjectView: UIImageView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        var touch = touches.first
        self.center = (touch?.location(in: self.superview))!
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
导入UIKit
类ObjectView:UIImageView{
重写初始化(帧:CGRect){
super.init(frame:frame)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
var touch=touch.first
self.center=(触摸?.location(在:self.superview中))!
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
}

有什么解决方法吗?

您的问题就在这里
self.center=(touch?.location(in:self.superview))

您应该在
触摸开始
中计算距中心的偏移量,然后在移动图像时添加该偏移量

我现在无法测试代码,但它应该能让您了解如何进行测试

var initialLocation: CGPoint?    
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
     var touch = touches.first
     initialLocation = CGPoint(x: (touch?.location(in: self.superview))!.x - self.center.x, y: (touch?.location(in: self.superview))!.y - self.center.y)
}


override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    var touch = touches.first
    self.center = CGPoint(x: (touch?.location(in: self.superview)).x! - initialLocation.x, y: (touch?.location(in: self.superview)).y! - initialLocation.y)
}
var初始位置:CGPoint?
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
var touch=touch.first
初始位置=CGPoint(x:(触摸?.location(in:self.superview))!.x-self.center.x,y:(触摸?.location(in:self.superview))!.y-self.center.y)
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
var touch=touch.first
self.center=CGPoint(x:(触摸?.location(in:self.superview)).x!-初始位置.x,y:(触摸?.location(in:self.superview)).y!-初始位置.y)
}

谢谢!我试过了,但现在图像不再在我的手指下了。也许我能解决它…@巧克力蛋糕,我已经编辑了答案。
初始位置的计算现在是正确的。当触摸结束或取消时,不要忘记重置它。是的,现在它可以工作了!我只是通过添加一个包含对象初始位置的辅助
initialLocation
来修复它。我这样计算新的位置:
让xPos=((touch?.location(in:self.superview))?.x)!-initialLocationFinger!。x;让yPos=((touch?location(in:self.superview))?.y)!-initialLocationFinger!。Yself.center=CGPoint(x:initialLocationObject!.x+xPos,y:initialLocationObject!.y+yPos)但我认为您的解决方案更聪明;)@巧克力蛋糕,他们都是对的。很抱歉没有立即给出解决方案-我无法测试代码。如果你能接受这个答案,我将不胜感激。谢谢你,祝你好运!没问题!你帮了我很多!非常感谢。