Skip to Content
Swift游戏开发经典实例
book

Swift游戏开发经典实例

by Jonathon Manning, Paris Buttfield-Addison
January 2020
Intermediate to advanced
356 pages
6h 4m
Chinese
China Electric Power Press Ltd.
Content preview from Swift游戏开发经典实例
输入
101
解决方案
要告诉
iOS
某个点属于某个视图的
bounds
边界范围之内,可以覆
point(inside:
, with:)
方法。
在一个
UIView
子类中,覆盖这个方法:
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
//
如果点的位置落在视图
bounds
之外
40pt
范围内,
//
都可以认为它是位于视图内
return self.bounds.insetBy(dx: -40, dy: -40).contains(point)
}
讨论
当屏幕上发生一个触摸,
iOS
开始会判断到底是哪个视图被触摸。它从顶层视图开
始调用
point(inside:,with:)
方法,以此来判断触摸是否是在该视图之内发生。
然后它挨个询问顶层视图下面的子视图,看触摸还位于谁的范围之内,一直到最下
一层视图。
默认情况下,只有触摸位于视图的
bounds
矩形范围内,才会认为这个触摸与该视
图有关。但我们也可以通过覆盖
point(inside:,with:)
法,以实现不同的行为。
point(inside:,with:)
方法的
point
数表示屏幕上的一个点,使用的是视图的坐
标空间,如果要让这个点被判定为“视图之内”,则让该方法返回
true
;要让这
个点被判定为“视图之外”,则让该方法返回
false
3.8
识别摇晃手势
问题
如何识别用户正在摇晃设备?
解决方案
在视图控制器中加入如下代码:
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

精通機器學習

精通機器學習

Aurélien Géron
FreeBSD® Unleashed

FreeBSD® Unleashed

Brian Tiemann, Michael C. Urban
深入淺出代數

深入淺出代數

Tracey Pilone, Dan Pilone
Swift学习手册

Swift学习手册

Jon Manning, Paris Buttfield-Addison, Tim Nugent

Publisher Resources

ISBN: 9787519840129