好的,以下代码的目的是创建一个带有初始静态图像的UI图像动画,在通过UI按钮动作激活后,它将通过assets.xcassts中的一系列图像进行随机播放,然后在2个周期之后停。此外,当动画停止时,静态UI图像将更改为assets.xcasst中的随机图像。代码首先按照需要工作,但是每次触发UI按钮并且图像随机播放并且静态UI图像发生更改时,动画的repeatcount将增加1个循环。
var myImages = [UIImage]()
@IBAction func playRoundTapped(sender: UIButton) {
for i in 1...3
{
myImages.append(UIImage(named: "\(i)")!)
}
firstDiceImageView.animationImages = myImages
firstDiceImageView.animationRepeatCount = 2
secondDiceImageView.animationImages = myImages
secondDiceImageView.animationRepeatCount = 2
thirdDiceImageView.animationImages = myImages
thirdDiceImageView.animationRepeatCount = 2
forthDiceImageView.animationImages = myImages
forthDiceImageView.animationRepeatCount = 2
firstDiceImageView.startAnimating()
secondDiceImageView.startAnimating()
thirdDiceImageView.startAnimating()
forthDiceImageView.startAnimating()
}
以上是动画的代码。每个动画的animationRepeatCount设置为2个周期。在第一个按钮上单击它会根据需要循环两次,但是在第二个按钮上点击它会循环3次,而在第三个按钮上循环4次。
下面是包含所有代码的完整ViewController.swift文件。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var begButton: UISwitch!
@IBOutlet weak var intButton: UISwitch!
@IBOutlet weak var advButton: UISwitch!
@IBOutlet weak var playRoundButton: UIButton!
@IBOutlet weak var forthDiceImageView: UIImageView!
@IBOutlet weak var thirdDiceImageView: UIImageView!
@IBOutlet weak var secondDiceImageView: UIImageView!
@IBOutlet weak var firstDiceImageView: UIImageView!
var myImages = [UIImage]()
@IBAction func playRoundTapped(sender: UIButton) {
for i in 1...3
{
myImages.append(UIImage(named: "\(i)")!)
}
firstDiceImageView.animationImages = myImages
firstDiceImageView.animationRepeatCount = 2
secondDiceImageView.animationImages = myImages
secondDiceImageView.animationRepeatCount = 2
thirdDiceImageView.animationImages = myImages
thirdDiceImageView.animationRepeatCount = 2
forthDiceImageView.animationImages = myImages
forthDiceImageView.animationRepeatCount = 2
firstDiceImageView.startAnimating()
secondDiceImageView.startAnimating()
thirdDiceImageView.startAnimating()
forthDiceImageView.startAnimating()
}
@IBAction func playRoundTapped2(sender: AnyObject) {
let firstRandomNumber = arc4random_uniform(2) + 1
let firstCardString:String = String(format: "side%i",
firstRandomNumber)
let secondRandomNumber = arc4random_uniform(1) + 1
let secondCardString:String = String(format: "invert%i",
secondRandomNumber)
self.firstDiceImageView.image = UIImage(named: firstCardString)
self.secondDiceImageView.image = UIImage(named: secondCardString)
self.thirdDiceImageView.image = UIImage(named: "card_default")
self.forthDiceImageView.image = UIImage(named: "card_default")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.firstDiceImageView.image = UIImage(named: "card_default")
self.secondDiceImageView.image = UIImage(named: "card_default")
self.thirdDiceImageView.image = UIImage(named: "card_default")
self.forthDiceImageView.image = UIImage(named: "card_default")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:1)
看看下面代码的这一部分......
var myImages = [UIImage]()
@IBAction func playRoundTapped(sender: UIButton) {
for i in 1...3
{
myImages.append(UIImage(named: "\(i)")!)
}
firstDiceImageView.animationImages = myImages
firstDiceImageView.animationRepeatCount = 2
secondDiceImageView.animationImages = myImages
secondDiceImageView.animationRepeatCount = 2
thirdDiceImageView.animationImages = myImages
thirdDiceImageView.animationRepeatCount = 2
forthDiceImageView.animationImages = myImages
forthDiceImageView.animationRepeatCount = 2
firstDiceImageView.startAnimating()
secondDiceImageView.startAnimating()
thirdDiceImageView.startAnimating()
forthDiceImageView.startAnimating()
}
在调用该函数之前,您正在创建myImages
array
。然后,当您调用该函数时,将图像附加到该函数。下次再调用此功能时,请再添加3张图像。你永远不会清空阵列。在@IBAction的开始时,您可以在附加3个图像之前使用myImages.removeAll
清空数组。