Xcode &Swift - 在 UIScrollView 内检测 UIView 的用户触摸

时间:2022-10-27
本文介绍了Xcode &Swift - 在 UIScrollView 内检测 UIView 的用户触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在创建一个类似于 Flappy Bird 的游戏,但用户将手指放在屏幕上并躲避障碍物,而不是点击让小鸟飞起来.

I am creating a game, similar to Flappy Bird but the user holds their finger on the screen and dodges the obstacles, rather than tapping to make the bird fly.

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

I am doing this by having a UIScrollView, in which UIView's are used as obstacles. When the user touches a UIView, the game is over.

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

How do I detect the users touch of a UIView from within a UIScrollView? I am using Swift with Xcode Beta 4.

这是游戏截图

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

As you can see, the user moves their finger between the grey blocks (UIViews) as they scroll up.

推荐答案

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

By setting userInteractionEnabled to NO for your scroll view, the view controller will start receiving touch events since UIViewController is a subclass of UIResponder. You can override one or more of these methods in your view controller to respond to these touches:

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

我创建了一些示例代码来演示如何做到这一点:

I created some example code to demonstrate how you could do this:

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!")
            }
        }
    }

}

这篇关于Xcode &Swift - 在 UIScrollView 内检测 UIView 的用户触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:UIScrollView 中的 contentOffset 有什么作用? 下一条:以编程方式强制 UIScrollView 停止滚动,以便与多个数据源共享表格视图

相关文章

最新文章