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

Xcode &Swift - 在 UIScrollView 內檢測 UIView 的用

Xcode amp; Swift - Detecting user touch of UIView inside of UIScrollView(Xcode amp;Swift - 在 UIScrollView 內檢測 UIView 的用戶觸摸)
本文介紹了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 的用戶觸摸的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

iOS - Using storyboard and autolayout to center the UIScrollView(iOS - 使用故事板和自動布局使 UIScrollView 居中)
get index or tag value from imageview tap gesture(從 imageview 點擊手勢獲取索引或標簽值)
UIScrollView not scrolling regardless of large contentSize(無論內容大小如何,UIScrollView 都不會滾動)
UIScrollView zooming with Auto Layout(UIScrollView 使用自動布局縮放)
How to create an image from a UIView / UIScrollView(如何從 UIView/UIScrollView 創建圖像)
iOS/Swift - Hide/Show UITabBarController when scrolling down/up(iOS/Swift - 向下/向上滾動時隱藏/顯示 UITabBarController)
主站蜘蛛池模板: 国产日韩久久久久69影院 | 欧美黄色一区 | 欧美精品三区 | 日韩精品一区二区三区在线观看 | 伊人网站视频 | 亚洲国产高清高潮精品美女 | 91大神在线资源观看无广告 | 国产精品美女在线观看 | 国产精品99999999 | 成人在线观看免费 | 99tv| 99久久久久久久 | 日韩午夜网站 | 高清一区二区三区 | h在线播放 | 亚洲精品区 | 视频一区二区中文字幕 | 91av在线视频观看 | 日韩欧美网 | 人人亚洲| 色免费视频| 一区二区三区四区在线视频 | 精品国产乱码久久久久久牛牛 | 97成人精品 | 日韩高清一区二区 | 精品国产31久久久久久 | 成人在线精品视频 | 一本色道精品久久一区二区三区 | 欧美一级黄色免费看 | 国产成人亚洲精品自产在线 | 夜夜草视频| 日韩和的一区二在线 | 国产免费一区二区 | 999精品视频 | 国产精品久久国产精品99 gif | 久久99深爱久久99精品 | 中文字幕亚洲精品在线观看 | 天天看天天操 | 在线一区观看 | 午夜午夜精品一区二区三区文 | 视频一区二区中文字幕 |