0
我想显示被触摸和移动的对象的x corrdinate。swift touchesBegan并将其显示在被触摸的对象上
试试看:
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跳转,开始触摸,框正在移动。
但是,如果我激活线来显示坐标(例如,我的意思是任何动态文本),移动和跳跃不再工作。
为什么会出现此问题?
哇...完美的答案。我完全理解它。任何解决方案:我可以通过编程禁用自动布局吗? – isicom
你真的不需要那样做。您可以改变其约束,而不是更改视图的“中心”。但是,如果您真的想完全从自动布局中删除这一个视图,您可以;最简单的方法是在代码中而不是在故事板中创建视图。 – matt
很酷的想法,我会尝试。感谢马特;) – isicom