我在使用函数 touchesBegan
时遇到了一些问题。问题是当 touchesBegan
执行时,图像人物跳到 Main.storyboard 开头定义的位置。这是我的代码:
var location = CGPoint(x: 0, y: 0)
@IBOutlet weak var person: UIImageView!
@IBOutlet weak var value: UILabel!
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch: UITouch = touches.first as! UITouch
location = touch.locationInView(self.view)
person.center = location
value.text = "X: \(person.center.x), Y: \(person.center.y)"
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch: UITouch = touches.first as! UITouch
location = touch.locationInView(self.view)
person.center = location
当我添加行时出现问题:
value.text = "X: \(person.center.x), Y: \(person.center.y)"
知道为什么吗?我打印了location和person.center,它们都指向我点击的位置。但是 image person 会转到程序开始时定义的位置。
最佳答案
您需要告诉 ImageView 重绘自身:
person.setNeedsDisplay()
或者您可以将内容模式设置为在 ImageView 边界更改时重新绘制自身:
person.contentMode = UIViewContentModeRedraw // Only once
关于ios - 在 Swift 中使用 touchesBegan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30012729/