在 Swift 中使用按钮启动和停止动画
Starting and Stopping Animation with Button in Swift
我正在尝试使用 UIButton 来启动和停止已放入图像视图中的动画图像序列。我正在使用 swift 3.0
这是我当前的代码:
import UIKit
class ViewController: UIViewController {
var selectedProgression : Int = 0
var loading_1: UIImage!
var loading_2: UIImage!
var loading_3: UIImage!
var animatedImage: UIImage!
@IBOutlet weak var progressionAnimation: UIImageView!
override func viewDidLoad() {
loading_1 = UIImage(named: "blues-1")
loading_2 = UIImage(named: "blues-2")
loading_3 = UIImage(named: "blues-3")
var images: [UIImage]!
images = [loading_1, loading_2, loading_3]
animatedImage = UIImage.animatedImage(with: images, duration: 3.0)
progressionAnimation.image = animatedImage
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
我已经看过并且对如何执行此操作有了一些想法,但是在将解决方案实际实施到我的代码中时一直卡住了:(
根据我的发现,我需要为按钮创建一个 outlet 来测量它是否被按下,并在按下时将按钮动作连接到 startAnimating 或 stopAnimating,具体取决于它的当前状态。
我在 Apple 文档中找到了 startAnimating 和 stopAnimating 函数 https://developer.apple.com/documentation/uikit/uiimageview/1621061-startanimating 但不确定如何将它们实际实现到代码中。
任何帮助将不胜感激。谢谢!
可以找到如何创建出口 here。总而言之,打开助手编辑器(右上角的两个圆圈),然后按住控件将按钮从故事板拖到 Swift 文件中。
您必须为此插座编写的 IBAction 看起来像这样:
@IBAction func starStopButtonPressed(_ sender: Any) {
//Check if the progressionAnimation UIImageView is already animating
if progressionAnimation.isAnimating {
progressionAnimation.stopAnimating() //If animating: stop animating
else {
progressionAnimation.startAnimating() //If not animating: start animating
}
}
创建新变量时要小心!
。如果您尝试在变量具有值之前使用该变量,这可能会导致崩溃。
所以这个:
var images: [UIImage]!
images = [loading_1, loading_2, loading_3]
应该这样写:
var image: [UIImage] = [loading_1, loading_2, loading_3]
行数更少,看起来更干净,也更安全。对那些 loading_1-3 UIImages 做同样的事情,你可以将它们的声明移动到 viewDidLoad
函数内部。
就像这样:
var animatedImage: UIImage!
animatedImage = UIImage.animatedImage(with: images, duration: 3.0)
progressionAnimation.image = animatedImage
这样写会更干净,也更安全:
progressionAnimation.image = UIImage.animatedImage(with: images, duration: 3.0)
如果要使用开始或停止动画,请不要使用progreesionAnimation的图片。
progressionAnimation.animationImages = images
progressionAnimation.animationDuration = 1.0
progressionAnimation.animationRepeatCount = 100
progressionAnimation.startAnimating()
您应该使用您创建的图像数组中的动画图像。
我正在尝试使用 UIButton 来启动和停止已放入图像视图中的动画图像序列。我正在使用 swift 3.0
这是我当前的代码:
import UIKit
class ViewController: UIViewController {
var selectedProgression : Int = 0
var loading_1: UIImage!
var loading_2: UIImage!
var loading_3: UIImage!
var animatedImage: UIImage!
@IBOutlet weak var progressionAnimation: UIImageView!
override func viewDidLoad() {
loading_1 = UIImage(named: "blues-1")
loading_2 = UIImage(named: "blues-2")
loading_3 = UIImage(named: "blues-3")
var images: [UIImage]!
images = [loading_1, loading_2, loading_3]
animatedImage = UIImage.animatedImage(with: images, duration: 3.0)
progressionAnimation.image = animatedImage
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
我已经看过并且对如何执行此操作有了一些想法,但是在将解决方案实际实施到我的代码中时一直卡住了:(
根据我的发现,我需要为按钮创建一个 outlet 来测量它是否被按下,并在按下时将按钮动作连接到 startAnimating 或 stopAnimating,具体取决于它的当前状态。
我在 Apple 文档中找到了 startAnimating 和 stopAnimating 函数 https://developer.apple.com/documentation/uikit/uiimageview/1621061-startanimating 但不确定如何将它们实际实现到代码中。
任何帮助将不胜感激。谢谢!
可以找到如何创建出口 here。总而言之,打开助手编辑器(右上角的两个圆圈),然后按住控件将按钮从故事板拖到 Swift 文件中。
您必须为此插座编写的 IBAction 看起来像这样:
@IBAction func starStopButtonPressed(_ sender: Any) {
//Check if the progressionAnimation UIImageView is already animating
if progressionAnimation.isAnimating {
progressionAnimation.stopAnimating() //If animating: stop animating
else {
progressionAnimation.startAnimating() //If not animating: start animating
}
}
创建新变量时要小心!
。如果您尝试在变量具有值之前使用该变量,这可能会导致崩溃。
所以这个:
var images: [UIImage]!
images = [loading_1, loading_2, loading_3]
应该这样写:
var image: [UIImage] = [loading_1, loading_2, loading_3]
行数更少,看起来更干净,也更安全。对那些 loading_1-3 UIImages 做同样的事情,你可以将它们的声明移动到 viewDidLoad
函数内部。
就像这样:
var animatedImage: UIImage!
animatedImage = UIImage.animatedImage(with: images, duration: 3.0)
progressionAnimation.image = animatedImage
这样写会更干净,也更安全:
progressionAnimation.image = UIImage.animatedImage(with: images, duration: 3.0)
如果要使用开始或停止动画,请不要使用progreesionAnimation的图片。
progressionAnimation.animationImages = images
progressionAnimation.animationDuration = 1.0
progressionAnimation.animationRepeatCount = 100
progressionAnimation.startAnimating()
您应该使用您创建的图像数组中的动画图像。