如何准确拖放 swift 的图片?
How to drag and drop images with swift exactly?
我的屏幕上有一张图片,您可以拖放它。那已经有效了。当我把手指放在中间时。
就像我将手指放在任何角落(或任何其他不在中间的地方)一样,图像的中间就在我的手指下。但我还是想要角球。
这是我的代码:
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)
对象视图:
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")
}
}
有解决办法吗?
你的问题在这里self.center = (touch?.location(in: self.superview))!
。
你应该在touchesBegan
中计算出中心的偏移量,然后在移动图像时加上它。
我现在无法测试代码,但它应该可以让您了解如何进行测试。
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)
}
我的屏幕上有一张图片,您可以拖放它。那已经有效了。当我把手指放在中间时。 就像我将手指放在任何角落(或任何其他不在中间的地方)一样,图像的中间就在我的手指下。但我还是想要角球。
这是我的代码:
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)
对象视图:
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")
}
}
有解决办法吗?
你的问题在这里self.center = (touch?.location(in: self.superview))!
。
你应该在touchesBegan
中计算出中心的偏移量,然后在移动图像时加上它。
我现在无法测试代码,但它应该可以让您了解如何进行测试。
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)
}