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游戏开发经典实例
74
2
2.8
Core Animation
移动图片
问题
如何让图片在屏幕上移动,并实时、流畅地改变它的位置。
解决方案
让图片移动的步骤如下:
1.
新建
Single View Application
项目,命名为
ImageAnimation
2.
向项目中拖入一张小球图片(在源代码中已经提供):
打开
Main.storyboard
拖入一个
Image View
对象,将它的
Image
属性设置为小球图片。
Image View
创建一个
Outlet
连接,命名为
ball
,并将
Image View
连接
到这个
Outlet
(参考本章
2.2
节)。
打开
ViewController.swift
viewWillAppear
方法中加入代码:
UIView.animate(withDuration: 2.0, animations: { () -> Void in
self.ball.center = CGPoint(x: 0, y: 0)
})
3.
运行应用程序。
当程序启动后,小球将慢慢移动到屏幕的左上角。
讨论
这个示例程序演示了如何让一个
UIImageView
对象在
2
秒内移动到指定位置。
对于
Image View
而言,它只会被动地显示一张图片,不会(也没有必要)自己动
来动去,这样就需要别的东西来使它们动起来。这里的“别的东西”也就是
View
Controller
,它负责管理显示在屏幕上的一切。
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