Xcode和Swift - 在UIScrollView中检测UIView的用户触摸

use*_*282 27 xcode uiscrollview uiview ios swift

我正在创建一个类似于Flappy Bird的游戏,但是用户将他们的手指放在屏幕上并躲避障碍物,而不是轻拍以使鸟儿飞翔.

我通过使用UIScrollView来实现这一点,其中UIView被用作障碍物.当用户触摸UIView时,游戏结束.

如何在UIScrollView中检测用户对UIView的触摸?我正在使用Swift和Xcode Beta 4.

编辑:这是游戏的截图

这是游戏的截图

如您所见,用户在向上滚动时在灰色块(UIViews)之间移动手指.

s1m*_*m0n 51

通过设置userInteractionEnabledNO您的滚动视图,视图控制器将开始接收触摸事件,因为UIViewController是的子类UIResponder.您可以在视图控制器中覆盖这些方法中的一个或多个以响应这些触摸:

  • touchesBegan:withEvent:
  • touchesMoved:withEvent:
  • touchesEnded:withEvent:
  • touchesCancelled:withEvent:

我创建了一些示例代码来演示如何执行此操作:

class ViewController: UIViewController {
    @IBOutlet weak var scrollView: UIScrollView!

    // This array keeps track of all obstacle views
    var obstacleViews : [UIView] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Create an obstacle view and add it to the scroll view for testing purposes
        let obstacleView = UIView(frame: CGRectMake(100,100,100,100))
        obstacleView.backgroundColor = UIColor.redColor()
        scrollView.addSubview(obstacleView)

        // Add the obstacle view to the array
        obstacleViews += obstacleView
    }

    override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
        testTouches(touches)
    }

    override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
        testTouches(touches)
    }

    func testTouches(touches: NSSet!) {
        // Get the first touch and its location in this view controller's view coordinate system
        let touch = touches.allObjects[0] as UITouch
        let touchLocation = touch.locationInView(self.view)

        for obstacleView in obstacleViews {
            // Convert the location of the obstacle view to this view controller's view coordinate system
            let obstacleViewFrame = self.view.convertRect(obstacleView.frame, fromView: obstacleView.superview)

            // Check if the touch is inside the obstacle view
            if CGRectContainsPoint(obstacleViewFrame, touchLocation) {
                println("Game over!")
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)


tru*_*201 15

您可以通过编程方式添加手势识别器,如下所示

var touch = UITapGestureRecognizer(target:self, action:"action")
scrollView.addGestureRecognizer(touch)
Run Code Online (Sandbox Code Playgroud)

但是,此手势识别器不适合您.UITapGestureRecognizer仅返回点击,而不是点按并保持,UILongPressGestureRecognizer不提供有关位置的信息,因此您想要使用UIPanGestureRecognizer.它会不断告诉你触摸的移动距离.

var touch = UIPanGestureRecognizer(target:self, action:"handlePan")
scrollView.addGestureRecognizer(touch)

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
    let translation = recognizer.translationInView(self.view)
    recognizer.setTranslation(CGPointZero, inView: self.view)
}
Run Code Online (Sandbox Code Playgroud)

您可以使用常量"平移"来移动对象,它表示人滑动手指的距离.使用它加上你的鸟的位置将鸟移动到一个新的点.调用此函数后,必须将转换重置为零.

编辑:使用您的游戏格式,此代码应该是最好的方法.

所以,总之,找到手指位置的代码应如下所示.

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInView(yourScrollView)
    }
}


@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
    let translation = recognizer.translationInView(self.view)
    var currentLocation : CGPoint = CGPointMake(location.x+translation.x, location.y+translation.y)
    recognizer.setTranslation(CGPointZero, inView: self.view)
}
Run Code Online (Sandbox Code Playgroud)

currentLocation是一个CGPoint,包含当前触摸的位置,无论手指滑动到何处.由于我不知道如何创建要避免的视图,因此必须使用currentLocation的y坐标来确定在该y处要避免的视图的x边界,并使用<或>比较器来确定如果触摸的x边界位于这些视图中的任何一个内.

注意:您必须声明位置,以便可以在handlePan中访问它

var location : CGPoint = CGPointZero
Run Code Online (Sandbox Code Playgroud)