Xcode& Swift-在UIScrollView内部检测用户对UIView的触摸 [英] Xcode & Swift - Detecting user touch of UIView inside of UIScrollView

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

问题描述

我正在创建一个类似于《飞扬的小鸟》的游戏,但是用户将手指放在屏幕上并躲开障碍物,而不是轻拍以使小鸟飞起来.

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.

这是游戏的屏幕截图

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

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的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆