我正试图在屏幕上获取触摸的坐标,以显示弹出菜单
如何在页面视图控制器中使用
我在这做错了什么?
override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) { if let touch = touches.first { let position = touch.locationInView(view) print(position) } }
解决方法
在PageViewController中,如果要弹出窗口
在viewDidLoad中:
let tap = UITapGestureRecognizer(target: self,action: "showMoreActions:") tap.numberOfTapsrequired = 1 view.addGestureRecognizer(tap)
使页面视图控制器继承UIGestureRecognizerDelegate,然后添加:
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) }