【发布时间】:2016-01-03 06:42:14
【问题描述】:
我正在尝试将 UIPanGestureRecognizer 设置为特定的 UIView
self.halfViewBlue.frame = CGRectMake(0, 0, self.bounds.width / 2, self.bounds.height)
self.halfViewBlue.backgroundColor = UIColor.blueColor()
halfViewBlue.alpha = 1.0
self.addSubview(self.halfViewBlue)
self.halfViewRed.frame = CGRectMake(self.frame.midX, 0, self.bounds.width / 2, self.bounds.height)
self.halfViewRed.backgroundColor = UIColor.redColor()
halfViewRed.alpha = 1.0
self.addSubview(self.halfViewRed)
//Pan Gesture for Red
let panGestureRed = UIPanGestureRecognizer()
panGestureRed.addTarget(self, action: "handlePanRed:")
self.addGestureRecognizer(panGestureRed)
self.userInteractionEnabled = true
//Pan Gesture for Blue
let panGestureBlue = UIPanGestureRecognizer()
panGestureBlue.addTarget(self, action: "handlePanBlue:")
self.addGestureRecognizer(panGestureBlue)
self.userInteractionEnabled = true
}
func handlePanRed(gesture : UIPanGestureRecognizer){
if (gesture.state == UIGestureRecognizerState.Changed || gesture.state == UIGestureRecognizerState.Ended){
let velocity : CGPoint = gesture.velocityInView(halfViewRed)
//Pan Down
if(velocity.y > 0){
print("Panning down")
hideRedBar()
produceBlueBar()
}
//Pan Up
if(velocity.y < 0){
print("panning up")
hideBlueBar()
produceRedBar()
}
}
}
func handlePanBlue(gesture : UIPanGestureRecognizer){
if (gesture.state == UIGestureRecognizerState.Changed || gesture.state == UIGestureRecognizerState.Ended){
let velocity : CGPoint = gesture.velocityInView(halfViewBlue)
//Pan Down
if(velocity.y > 0){
print("Panning down")
hideBlueBar()
produceRedBar()
}
if(velocity.y < 0){
print("panning up")
hideRedBar()
produceBlueBar()
}
}
}
这里我只是简单地创建了两个 UIView,它们都垂直(相等地)在屏幕上分开。 然后我添加了两个 panGestures 和一个函数来跟随它。我的问题是唯一被识别的 panGesture 是“panGestureBlue”,奇怪的是我可以在屏幕的任何部分控制它,而不仅仅是“halfViewBlue”所在的一半。 所以我假设两个 panGestures 都被添加到 self.view 而不是它应该放在的 UIView 中,并且正在读入“panGestureBlue”,因为它是在“panGestureRed”之后添加的。而不是“panGestureBlue.addTarget”中的“self”,我尝试放置“halfViewBlue”,但它崩溃了。
我该如何解决这个问题!?
【问题讨论】:
标签: ios iphone swift uiview uipangesturerecognizer