久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Xcode &Swift - 在 UIScrollView 內(nèi)檢測 UIView 的用

Xcode amp; Swift - Detecting user touch of UIView inside of UIScrollView(Xcode amp;Swift - 在 UIScrollView 內(nèi)檢測 UIView 的用戶觸摸)
本文介紹了Xcode &Swift - 在 UIScrollView 內(nèi)檢測 UIView 的用戶觸摸的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在創(chuàng)建一個類似于 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 被用作障礙物.當(dāng)用戶觸摸 UIView 時,游戲結(jié)束.

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 設(shè)置為 NO,視圖控制器將開始接收觸摸事件UIViewControllerUIResponder 的子類.您可以在視圖控制器中覆蓋這些方法中的一個或多個以響應(yīng)這些觸摸:

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:

我創(chuàng)建了一些示例代碼來演示如何做到這一點:

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

}

這篇關(guān)于Xcode &Swift - 在 UIScrollView 內(nèi)檢測 UIView 的用戶觸摸的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

iOS - Using storyboard and autolayout to center the UIScrollView(iOS - 使用故事板和自動布局使 UIScrollView 居中)
get index or tag value from imageview tap gesture(從 imageview 點擊手勢獲取索引或標(biāo)簽值)
UIScrollView not scrolling regardless of large contentSize(無論內(nèi)容大小如何,UIScrollView 都不會滾動)
UIScrollView zooming with Auto Layout(UIScrollView 使用自動布局縮放)
How to create an image from a UIView / UIScrollView(如何從 UIView/UIScrollView 創(chuàng)建圖像)
iOS/Swift - Hide/Show UITabBarController when scrolling down/up(iOS/Swift - 向下/向上滾動時隱藏/顯示 UITabBarController)
主站蜘蛛池模板: 日韩精品少妇 | 午夜激情网站 | 国产欧美一区二区三区视频在线观看 | 欧美精品综合 | 四虎影视最新地址 | 国产黄色免费 | 高清视频一区二区 | 99视频网 | 免费av小说 | 伊人国产精品 | 91成人精品一区在线播放 | 久艹在线| 精品一区二区三区四区五区 | 看毛片网站 | 久久av资源 | 三级在线观看 | 国产一区二区视频在线 | 天天做天天爱 | 成年人网站在线免费观看 | 美国黄色一级大片 | 国产精品911 | 欧美色综合天天久久综合精品 | 亚洲在线视频观看 | www.狠狠| 一级片aa | 黄色a毛片| 色综合久久久久 | 一本伊人| 香蕉伊人网 | 久久久精品影院 | 亚洲福利一区 | 国产三级在线播放 | www.黄色网| 色多多视频在线观看 | 韩国精品一区 | 吃奶动态图 | 黄色片在线免费观看 | 欧美日韩在线一区 | 国产精品成人国产乱一区 | 香蕉福利视频 | 日韩中文字幕在线 |