我试图弄清楚如何在UIView上添加一个小圆圈/点,但是仅当用户在UIView内点击时才可以。点应放置在用户点击的任何位置。不幸的是,我不知道该怎么做。
任何带有代码的示例都将非常有帮助!我正在使用Swift。
在View Controller中,您应该首先实现touchesBegan
方法,该方法为您提供可以使用的UITouch
对象数组。然后,每个UITouch
对象都具有locationInView
为您提供CGPoint
触摸位置的属性。您可以将这些位置保存在数组中(VC的属性)。或者,您可以立即使用此方法初始化点对象。
func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch in touches{
let location = (touch as! UITouch).locationInView(self.courtView)
var dot = UIView(frame: CGRect(location.x, location.y, 10, 10))
self.courtView.addSubview(dot) //add dot as subview to your main view
}
}
你给作为参数什么locationInView
主要取决于您的视图层次,它可能是self
,self.contentView
或self.view
。同样适用于将点添加为子视图的位置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句