当我按下按钮时,有一个没有动画的动画项目。我究竟做错了什么。以下是视图控制器中的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var startStopAnimating: UIButton!
@IBOutlet weak var imageView: UIImageView!
// image arrays
let frames: [String] = ["frame1.png", "frame2.png", "frame3.png", "frame4.png", "frame5.png", "frame6.png", "frame7.png"]
let swan: [String] = ["swanFrame1", "swanFrame2", "swanFrame3"]
let swirl: [String] = ["swirlFrame1", "swirlFrame2", "swirlFrame3"]
let starryNight: [String] = ["starryNight1", "starryNight2", "starryNight3"]
let doveWithBranch: [String] = ["doveWithBranch1", "doveWithBranch2", "doveWithBranch3"]
let tanCandles: [String] = ["tanCandles1", "tanCandles2", "tanCandles3", "tanCandles4"]
let buddhaFloat: [String] = ["buddhaFloat1", "buddhaFloat2", "buddhaFloat3", "buddhaFloat4", "buddhaFloat5", "buddhaFloat6"]
// timer vars
var timer = NSTimer()
var isAnimating = true
var counter = 1
var arrayIndex = 0
var pauseCheck = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
randomizedFuncs()
}
@IBAction func startStopAnimateButton(sender: AnyObject) {
if isAnimating == true {
timer.invalidate()
startStopAnimating.setTitle("Animate", forState: UIControlState.Normal)
} else {
startStopAnimating.setTitle("Stop Animating", forState: UIControlState.Normal)
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "pauseCheck", userInfo: nil, repeats: true)
}
}
func framesFunc() {
if arrayIndex == swan.count-1 {
arrayIndex = 0
} else {
arrayIndex++
}
imageView.image = UIImage(named: frames[arrayIndex])
counter++
pauseCheck = 1
if counter == 50 {
arrayIndex = 0
timer.invalidate()
isAnimating = false
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "swanFunc", userInfo: nil, repeats: true)
isAnimating = true
counter = 0
arrayIndex = 0
}
}
func swanFunc() {
if arrayIndex == frames.count-1 {
arrayIndex = 0
} else {
arrayIndex++
}
imageView.image = UIImage(named: frames[arrayIndex])
counter++
pauseCheck = 2
if counter == 50 {
arrayIndex = 0
timer.invalidate()
isAnimating = false
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "swirlFunc", userInfo: nil, repeats: true)
isAnimating = true
counter = 0
arrayIndex = 0
}
}
func swirlFunc() {
if arrayIndex == swirl.count-1 {
arrayIndex = 0
} else {
arrayIndex++
}
imageView.image = UIImage(named: frames[arrayIndex])
counter++
pauseCheck = 3
if counter == 50 {
arrayIndex = 0
timer.invalidate()
isAnimating = false
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "starryNightFunc", userInfo: nil, repeats: true)
isAnimating = true
counter = 0
arrayIndex = 0
}
}
func starryNightFunc() {
if arrayIndex == starryNight.count-1 {
arrayIndex = 0
} else {
arrayIndex++
}
imageView.image = UIImage(named: frames[arrayIndex])
counter++
pauseCheck = 4
if counter == 50 {
arrayIndex = 0
timer.invalidate()
isAnimating = false
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "doveWithBranchFunc", userInfo: nil, repeats: true)
isAnimating = true
counter = 0
arrayIndex = 0
}
}
func doveWithBranchFunc() {
if arrayIndex == doveWithBranch.count-1 {
arrayIndex = 0
} else {
arrayIndex++
}
imageView.image = UIImage(named: frames[arrayIndex])
counter++
pauseCheck = 5
if counter == 50 {
arrayIndex = 0
timer.invalidate()
isAnimating = false
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "tanCandlesFunc", userInfo: nil, repeats: true)
isAnimating = true
counter = 0
arrayIndex = 0
}
}
func tanCandlesFunc() {
if arrayIndex == tanCandles.count-1 {
arrayIndex = 0
} else {
arrayIndex++
}
imageView.image = UIImage(named: frames[arrayIndex])
counter++
pauseCheck = 6
if counter == 50 {
arrayIndex = 0
timer.invalidate()
isAnimating = false
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "buddhaFloatFunc", userInfo: nil, repeats: true)
isAnimating = true
counter = 0
arrayIndex = 0
}
}
func buddhaFloatFunc() {
if arrayIndex == buddhaFloat.count-1 {
arrayIndex = 0
} else {
arrayIndex++
}
imageView.image = UIImage(named: frames[arrayIndex])
counter++
pauseCheck = 7
if counter == 50 {
arrayIndex = 0
timer.invalidate()
isAnimating = false
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "frames Func", userInfo: nil, repeats: true)
isAnimating = true
counter = 0
arrayIndex = 0
}
}
// pauses the images and restarts them where they left off, its called in the start/stop action
func pauseCheckFunc() {
if pauseCheck == 1 {
timer.invalidate()
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "framesFunc", userInfo: nil, repeats: true)
} else if pauseCheck == 2 {
timer.invalidate()
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "swanFunc", userInfo: nil, repeats: true)
} else if pauseCheck == 3 {
timer.invalidate()
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "swirlFunc", userInfo: nil, repeats: true)
} else if pauseCheck == 4 {
timer.invalidate()
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "starryNightFunc", userInfo: nil, repeats: true)
} else if pauseCheck == 5 {
timer.invalidate()
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "doveWithBranchFunc", userInfo: nil, repeats: true)
} else if pauseCheck == 6 {
timer.invalidate()
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "tanCandlesFunc", userInfo: nil, repeats: true)
} else if pauseCheck == 7 {
timer.invalidate()
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "buddhaFloatFunc", userInfo: nil, repeats: true)
}
}
func randomizedFuncs() {
let randomNumber = Int(arc4random_uniform(UInt32(7)))
if randomNumber == 0 {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "swanFunc", userInfo: "nil", repeats: true)
} else if randomNumber == 1 {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "framesFunc", userInfo: "nil", repeats: true)
} else if randomNumber == 2 {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "swirlFunc", userInfo: "nil", repeats: true)
} else if randomNumber == 3 {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "starryNightFunc", userInfo: "nil", repeats: true)
} else if randomNumber == 4 {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "doveWithBranchFunc", userInfo: "nil", repeats: true)
} else if randomNumber == 5 {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "tanCandlesFunc", userInfo: "nil", repeats: true)
} else if randomNumber == 6 {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "buddhaFloatFunc", userInfo: "nil", repeats: true)
}
}
}
答案 0 :(得分:0)
您使用pauseCheck
作为选择器,但这不是代码中的功能。我认为你的意思是pauseCheckFunc
?
另外,我在代码中的任何地方都没有看到实际开始制作动画的命令。您也没有在UIImages上正确设置动画帧。目前还不清楚你为什么要使用计时器为图像制作动画。您只需将图像数组设置为UIImages的animationImages
属性,然后使用startAnimating()
和stopAnimating()
来启动和停止动画。