gpt4 book ai didi

ios - UIImage 去我点击的地方(但我需要它所以你只能拖动它)

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

我有一个 UIImage 并将其链接到我的代码中并将其命名为“Person”在我点击屏幕的那一刻,UIImage 跳转到那个位置,但我想要它,所以图像只能在我拖动它时移动。这是我的代码:

var location = CGPointMake(0, 0)

@IBOutlet var Person: UIImageView!

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

let touch : UITouch = touches.first as UITouch!

location = touch.locationInView(self.view)

Person.center = location

}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

let touch : UITouch = touches.first as UITouch!

location = touch.locationInView(self.view)

Person.center = location

}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

Person.center = CGPointMake(160, 330)


}

最佳答案

我会使用手势识别器——更不用说编码了。此外,如果要移动的元素不止一个,我会使用“三步”手势。

第 1 步:用户点击 subview ,虚线边框表示 subview 处于编辑模式。

第 2 步:用户平移 subview 到父 View 中的新位置。

第 3 步:用户点击父 View 中的任何其他位置(包括接下来要编辑的新 subview )以使第一个 subview 退出编辑模式。

subview 代码:

var _editMode = false
var editMode:Bool {
return _editMode
}
var borderPath = UIBezierPath()
var dashedBorder = CAShapeLayer()

func drawDashedBorder() {
borderPath = UIBezierPath()
borderPath.move(to: CGPoint(x: 3, y: 3))
borderPath.addLine(to: CGPoint(x: self.frame.width-3, y: 3))
borderPath.addLine(to: CGPoint(x: self.frame.width-3, y: self.frame.height-3))
borderPath.addLine(to: CGPoint(x: 3, y: self.frame.height-3))
borderPath.close()
dashedBorder = CAShapeLayer()
dashedBorder.strokeColor = UIColor.white.cgColor
dashedBorder.fillColor = UIColor.clear.cgColor
dashedBorder.lineDashPattern = [2, 2]
dashedBorder.lineDashPhase = 0.0
dashedBorder.lineJoin = kCALineJoinMiter
dashedBorder.lineWidth = 1.0
dashedBorder.miterLimit = 10.0
dashedBorder.path = borderPath.cgPath
let animation = CABasicAnimation(keyPath: "lineDashPhase")
animation.fromValue = 0.0
animation.toValue = 15.0
animation.duration = 0.75
animation.repeatCount = 10000
dashedBorder.add(animation, forKey: "linePhase")
self.addSublayer(dashedBorder)
_editMode = true
}

func removeDashedBorder() {
dashedBorder.removeFromSuperlayer()
_editMode = false
}

这不是生产代码,所以我从来没有完全弄清楚如何让“移动虚线”动画永远运行(这可能很好,因为它可能是一个性能问题)。但是 10000 秒似乎相当慷慨用于编辑。

这是 super View 代码,假设您有 imageView1 和 imageView2 作为 subview :

var editMode = false

override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer()
let panGesture = UIPanGestureRecognizer()
tapGesture.numberOfTapsRequired = 1
tapGesture.addTarget(self, action: #selector(editImage))
self.addGestureRecognizer(tapGesture)
panGesture.addTarget(self, action: #selector(moveImage))
self.addGestureRecognizer(panGesture)
}

func editEye(_ recognizer:UITapGestureRecognizer) {
let p = recognizer.location(in: self)
if !editMode {
if (imageView1.layer.hitTest(p) != nil) {
imageView1.drawDashedBorder()
self.editMode = true
} else if (imageView2.layer.hitTest(p) != nil) {
imageView2.drawDashedBorder()
self.editMode = true
}
} else {
if imageView1.editMode {
imageView1.removeDashedBorder()
self.editMode = false
} else if imageView2.editMode {
imageView2.removeDashedBorder()
self.editMode = false
}
}
}

func moveEye(_ recognizer:UIPanGestureRecognizer) {
let p = recognizer.location(in: self)
if imageView1.editMode {
imageView1.position = p
}
if imageView2.editMode {
imageView2.position = p
}
}

这可能不符合您的确切需求,但点击和平移点击的组合似乎更自然。我敢肯定,至少,做一个“无声”的点击和平移(在用户看来他们只是在四处拖动一些东西,但他们仍然必须先点击它)更合适。

关于ios - UIImage 去我点击的地方(但我需要它所以你只能拖动它),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40209317/

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