UIPangestureRecognizer

UIPangestureRecognizer

我是swift / xcode的新手,而且UIPanGestureRecognizer有问题。
我正在尝试创建一个滑动菜单,以便在您“平移”他时滑动它。
滑动菜单运行良好,我可以通过平移菜单(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.

就像所说的评论一样,我认为问题出在最后两行。
有人知道怎么了吗?

最佳答案

您应该将UIPanGestureRecognizer附加到mysubView:

panGestureRecognizerArrow = UIPanGestureRecognizer(target: self, action: "handlePan:")
mysubView.addGestureRecognizer(panGestureRecognizerArrow)

对于在屏幕边缘附近开始的平移手势,您也可以使用UIScreenEdgePanGestureRecognizer

关于ios - iOS Swift:2种 View 上的UIPanGestureRecognizer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31850770/

10-11 14:49