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游戏开发经典实例
视图和菜单
75
为了让
View Controller
能够驱使
Image View
进行运动,它首先得用一个
Outlet
引用
Image View
。一个
Outlet
是一个变量,用于连接到某个
View
。当我们按下
Ctrl
+
鼠标左键,从
Image View
拖一条线到代码窗口时,
Xcode
会认为你想创
建一个连接并弹出一个“
add connection
”(创建连接)对话框。
程序启动后,第一件事情就是将
IB
中创建的所有连接进行初始化。这时,我们在
代码中就可以通过
self.ball
获取到正确的引用,而不需要做任何额外的工作。
一旦连接被建立,就可以在
viewWillAppear
方法中进行移动小球的工作了。这个
方法恰如其名,会在视图(或者屏幕)即将显示给用户的时候调用。这里,我们在
这个方法中添加了如下代码:
UIView.animate(withDuration: 2.0, animations: { () -> Void in
self.ball.center = CGPoint(x: 0, y: 0)
})
方法
animate(withDuration:,animations:)
需要两个参数:动画持续的时间,以
及一个块对象,用于要在动画过程中执行的动作。
这里,动画将需要
2
秒钟来完成,并且动画期间只执行了一个改变:即修
self.
ball
center
属性为坐标(
0,0
)。当然,只要你愿意,你也可以加入对其他属性
的改变。例如,在块(即两个花括号之间)中加入下列代码
:
self.ball.alpha ...
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