Xcode amp; Swift - Detecting user touch of UIView inside of UIScrollView(Xcode amp;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
,视图控制器将开始接收触摸事件UIViewController
是 UIResponder
的子类.您可以在视图控制器中覆盖这些方法中的一个或多个以响应这些触摸:
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 的用户触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Xcode &Swift - 在 UIScrollView 内检测 UIView 的用
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01