我在swift / xcode中非常新,我遇到了UIPanGestureRecognizer的问题。 我试图做一个滑动菜单,当你" pan"他。 滑动菜单运行良好,我可以通过平移菜单(UIViews)来滑动它。 但是这个菜单必须在开头隐藏。 所以我放了一个小箭头。这是问题...我想要一个UIpanGestureRecognizer,他做了前一个相同的动作。当然,当我正在平移箭头时。 所以,这是代码谁工作:
// all is working with this code. handlePan is working well.
var panGestureRecognizer:UIPanGestureRecognizer!
panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
self.addGestureRecognizer(panGestureRecognizer)
这就是我为箭头尝试的内容:
let mysubView = UIView()
let arrowView = UIImageView()
let imageName = "arrow"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
var panGestureRecognizerArrow:UIPanGestureRecognizer!
//put subview bellow the menu
mysubView.frame = CGRect(x: self.bounds.size.width / 2 - 15, y: self.bounds.height, width: 30, height: 30)
//put arrow bellow the menu
imageView.frame = CGRect(x: self.bounds.size.width / 2 - 5, y: self.bounds.height, width: 10, height: 10)
addSubview(imageView)
//put a invisible subview over image, to have a bigger "hit box"
addSubview(mysubView)
//I think the problem is HERE
panGestureRecognizerArrow = UIPanGestureRecognizer(target: mysubView, action: "handlePan:")
self.addGestureRecognizer(panGestureRecognizerArrow)
//it did'nt passed by the handlePan function when I pan mysubView. There is debug console out in it.
像上述评论一样,我认为问题出现在最后两行。 有人知道什么是错的吗?
答案 0 :(得分:1)
您应该将UIPanGestureRecognizer
附加到mysubView
:
panGestureRecognizerArrow = UIPanGestureRecognizer(target: self, action: "handlePan:")
mysubView.addGestureRecognizer(panGestureRecognizerArrow)
对于在屏幕边缘附近开始的平移手势,您也可以使用UIScreenEdgePanGestureRecognizer。