如何在iOS swift中获取攻丝点(坐标)
我试图在屏幕上获得触摸的坐标,以显示弹出菜单 如何在页面视图控制器中使用 我做错了什么如何在iOS swift中获取攻丝点(坐标),ios,swift,Ios,Swift,我试图在屏幕上获得触摸的坐标,以显示弹出菜单 如何在页面视图控制器中使用 我做错了什么 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first { let position = touch.locationInView(view) print(position) } } 覆盖函
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let position = touch.locationInView(view)
print(position)
}
}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
如果让触摸=先触摸{
让位置=触摸。位置查看(视图)
印刷(职位)
}
}
在PageViewController中,如果要弹出
在viewDidLoad中:
let tap = UITapGestureRecognizer(target: self, action: "showMoreActions:")
tap.numberOfTapsRequired = 1
view.addGestureRecognizer(tap)
使页面视图控制器继承UIgestureRecognitizerDelegate
,然后添加:
func showMoreActions(touch: UITapGestureRecognizer) {
let touchPoint = touch.locationInView(self.view)
let DynamicView = UIView(frame: CGRectMake(touchPoint.x, touchPoint.y, 100, 100))
DynamicView.backgroundColor=UIColor.greenColor()
DynamicView.layer.cornerRadius=25
DynamicView.layer.borderWidth=2
self.view.addSubview(DynamicView)
}
Swift 5:
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(touchedScreen(touch:)))
view.addGestureRecognizer(tap)
}
@objc func touchedScreen(touch: UITapGestureRecognizer) {
let touchPoint = touch.location(in: self.view)
print(touchPoint)
}
这段代码很好用,我用iOS 9.1 SDK在模拟器中测试过,但它对我不起作用,它有任何deleget或其他工作代码。试试这个方法:我现在试过你的编码。它对我很好用。你用的是哪种xcode?你用的是哪种iOS版本?到底有什么区别?我在我的xcode中应用了上面的编码,它对我来说很好。我放置了断点,每次触摸视图时它都会调用。在页面视图控制器中,如何实现此代码添加到viewDidLoad::无法将view controller类型的值分配给'UIgesterRecognitizerDelegate'类型的值?