gpt4 book ai didi

ios - 如何准确地快速拖放图像?

转载 作者:行者123 更新时间:2023-11-28 06:32:45 26 4
gpt4 key购买 nike

我的屏幕上有一张图片,您可以拖放它。那已经有效了。当我 Handlebars 指放在中间时。就像我将手指放在任何角落(或任何其他不在中间的地方)一样,图像的中间就在我的手指下。但我还是想要角球。

这是我的代码:

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)

对象 View :

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)
}

关于ios - 如何准确地快速拖放图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39877296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com