我想显示触摸和移动的对象的 x 坐标。
尝试一下:
import UIKit
class ViewController: UIViewController {
var location = CGPoint(x: 0, y: 0)
@IBOutlet weak var viewBox: UIView!
@IBOutlet weak var cntInViewBox: UILabel!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first! as UITouch
location = touch.locationInView(self.view)
viewBox.center = location
/*cntInViewBox.text=String(location.x)*/
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first! as UITouch
location = touch.locationInView(self.view)
viewBox.center = location
/*cntInViewBox.text=String(location.x)*/
}
override func viewDidLoad() {
super.viewDidLoad()
viewBox.center = CGPoint(x:0, y: 0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
没有线路
cntIntViewBox.text=String(location.x)
效果很好。触摸屏幕,viewBox 跳跃,开始触摸,盒子移动。
但是,如果我激活线来显示坐标(例如,我指的是任何动态文本),则移动和跳跃将不再起作用。
为什么会出现这个问题?
最佳答案
问题是您的行更改了标签的文本。这会导致在整个界面中执行自动布局。您的 viewBox
View 由自动布局约束定位,因此这些约束接管并用于定位 View 。约束没有改变,因此 View 不会移动。
关于ios - 快速触摸开始并将其显示在触摸的对象上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33170454/