久久久久久久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)
主站蜘蛛池模板: 欧美一级片在线播放 | 国产高清视频在线观看播放 | 国产超碰人人爽人人做人人爱 | 午夜免费视频 | 久久中文字幕在线 | 国产一区二区三区免费观看视频 | 国产成年人小视频 | 毛片免费在线 | 网站国产 | 久久国产亚洲 | 91人人澡人人爽 | 国产精品视频免费看 | 免费在线成人 | 欧美日韩不卡合集视频 | 午夜激情一区 | 一区二区国产在线观看 | 亚洲va在线va天堂va狼色在线 | 国产精品亚洲视频 | 久草视频在线看 | 最新国产精品 | 久久综合九色综合欧美狠狠 | 日韩高清一区 | 国产精品99久久久久久人 | 最近最新中文字幕 | 国产精品久久久久久久粉嫩 | 99re6热在线精品视频播放 | 中文字幕 在线观看 | 久久久精 | 欧美成人一区二区三区片免费 | 欧美一级在线视频 | 国产日屁 | 久草a√| 欧美黑人体内she精在线观看 | 亚洲一区二区三区免费视频 | 91视频国产精品 | 国产在线精品一区二区三区 | 日韩激情在线 | 久久国产精品视频 | 久久黄色网 | 国产精品午夜电影 | 好姑娘影视在线观看高清 |