IOS For 循环中的动画。调整了每个延迟但仍然有问题
IOS Animations in For Loop. Adjusted delay for each but still having issues
我正在创建模式匹配游戏。基本上我在屏幕上有 4 个不同颜色的正方形 UIView。我希望一个方块随机闪烁,然后另一个方块随机闪烁。例如,当游戏开始时,假设一个红色框被随机选择闪烁。现在在第二次迭代中,红色框应该再次闪烁,然后是另一个随机选择的框,假设它是蓝色的。所以现在我们有红色和蓝色。现在循环再次开始,现在是红色、蓝色、绿色。这就是我想要完成的。如果这没有意义,请下载 IOS 游戏 "Mimeo",它应该准确地表达了我正在尝试做的事情。有人可以帮我吗?我运气不好,我曾尝试在其他地方寻求帮助,但没有成功。
// Created by Gary Dorman on 9/26/15.
// Copyright © 2015 Gary Dorman. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var enemyArray = [Int]()
var userArray = [Int]()
var currentScore = 0
var highScore = 0
var randomNumber = 0
@IBOutlet weak var RedBox: UIView!
@IBOutlet weak var GreenBox: UIView!
@IBOutlet weak var BlueBox: UIView!
@IBOutlet weak var YellowBox: UIView!
@IBOutlet weak var ScoreLabel: UILabel!
@IBOutlet weak var TitleImageView: UIImageView!
@IBAction func startGameButton(sender: UIButton) {
self.resetCurrentScoreAndLabel()
self.increaseLevel()
}
func increaseLevel(){
var delayTime = 1.0
randomNumber = Int(arc4random_uniform(4) + 1)
enemyArray.append(randomNumber)
for level in 1...enemyArray.count{
animateColorBox(self.enemyArray[level - 1], delay: delayTime)
delayTime++
print(enemyArray[level - 1])
}
}
override func viewDidLoad() {
super.viewDidLoad()
configureBorderOnColorBoxes()
}
func configureBorderOnColorBoxes(){
RedBox.layer.borderColor = UIColor.blackColor().CGColor
GreenBox.layer.borderColor = UIColor.blackColor().CGColor
BlueBox.layer.borderColor = UIColor.blackColor().CGColor
YellowBox.layer.borderColor = UIColor.blackColor().CGColor
RedBox.layer.borderWidth = 3
GreenBox.layer.borderWidth = 3
BlueBox.layer.borderWidth = 3
YellowBox.layer.borderWidth = 3
}
func resetCurrentScoreAndLabel(){
currentScore = 0
ScoreLabel.text = "Score: \(currentScore)"
}
func animateColorBox(tagNumber: Int, delay: Double){
let pauseBetweenAnimation = 1.25
let originalColor:UIColor = self.view.viewWithTag(tagNumber)!.backgroundColor!
UIView.animateWithDuration(1.0, delay: (delay - 1) * pauseBetweenAnimation, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.view.viewWithTag(tagNumber)!.alpha = 0.25
}, completion: { finished in
UIView.animateWithDuration(0.5, delay: (delay - 1) * pauseBetweenAnimation, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.view.viewWithTag(tagNumber)!.alpha = 1
}, completion: nil)
})
}
}
这里有一个可能适合您的解决方案:
func increaseLevel(){
// Set the delay time
var delayTime = 1.0 // This isn't actually used here
// Find a new random number
randomNumber = Int(arc4random_uniform(4) + 1)
// Append new random number to chained array
enemyArray.append(randomNumber)
// Call the animation method
animateColorBox(counter: 0)
}
func animateColorBox( counter counter: Int){
// If the tag number is within the enemyArray bounds
if( counter < enemyArray.count ){
tagNumber = enemyArray[counter];
// Set pause coefficient
let pauseBetweenAnimation = 1.25
// Save the original color for this view
let originalColor:UIColor = self.view.viewWithTag(tagNumber)!.backgroundColor!
// Do an animation for this view
UIView.animateWithDuration(1.0, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations:
{
self.view.viewWithTag(tagNumber)!.alpha = 0.25
}, completion:
{ finished in
// If finished with previous animation
if( finished ){
// Then set alpha back to normal
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.view.viewWithTag(tagNumber)!.alpha = 1
}, completion:
{
finished in
// If the view has its alpha back to normal
if( finished ){
// Call animateColorBox on next view in array
animateColorBox( counter: counter + 1);
}
})// End UIView animation
}// end if finished
})// end UIView Animation
}// end if counter < enemyArray.count
}
使用这种方法,如果我根据您的想法正确地考虑了这一点,您就可以在开始下一个动画之前完成集合中给定 UIView 的调光和取消调光的执行看法。让我知道这是否适合你。
编辑:
我意识到我去掉了动画之间的暂停,但你可以把它放在延迟区域中,根据你的需要进行调整。
哦,我意识到我的第一个 post 没有检查敌人数组中的值,所以让我改变一些东西。
我正在创建模式匹配游戏。基本上我在屏幕上有 4 个不同颜色的正方形 UIView。我希望一个方块随机闪烁,然后另一个方块随机闪烁。例如,当游戏开始时,假设一个红色框被随机选择闪烁。现在在第二次迭代中,红色框应该再次闪烁,然后是另一个随机选择的框,假设它是蓝色的。所以现在我们有红色和蓝色。现在循环再次开始,现在是红色、蓝色、绿色。这就是我想要完成的。如果这没有意义,请下载 IOS 游戏 "Mimeo",它应该准确地表达了我正在尝试做的事情。有人可以帮我吗?我运气不好,我曾尝试在其他地方寻求帮助,但没有成功。
// Created by Gary Dorman on 9/26/15.
// Copyright © 2015 Gary Dorman. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var enemyArray = [Int]()
var userArray = [Int]()
var currentScore = 0
var highScore = 0
var randomNumber = 0
@IBOutlet weak var RedBox: UIView!
@IBOutlet weak var GreenBox: UIView!
@IBOutlet weak var BlueBox: UIView!
@IBOutlet weak var YellowBox: UIView!
@IBOutlet weak var ScoreLabel: UILabel!
@IBOutlet weak var TitleImageView: UIImageView!
@IBAction func startGameButton(sender: UIButton) {
self.resetCurrentScoreAndLabel()
self.increaseLevel()
}
func increaseLevel(){
var delayTime = 1.0
randomNumber = Int(arc4random_uniform(4) + 1)
enemyArray.append(randomNumber)
for level in 1...enemyArray.count{
animateColorBox(self.enemyArray[level - 1], delay: delayTime)
delayTime++
print(enemyArray[level - 1])
}
}
override func viewDidLoad() {
super.viewDidLoad()
configureBorderOnColorBoxes()
}
func configureBorderOnColorBoxes(){
RedBox.layer.borderColor = UIColor.blackColor().CGColor
GreenBox.layer.borderColor = UIColor.blackColor().CGColor
BlueBox.layer.borderColor = UIColor.blackColor().CGColor
YellowBox.layer.borderColor = UIColor.blackColor().CGColor
RedBox.layer.borderWidth = 3
GreenBox.layer.borderWidth = 3
BlueBox.layer.borderWidth = 3
YellowBox.layer.borderWidth = 3
}
func resetCurrentScoreAndLabel(){
currentScore = 0
ScoreLabel.text = "Score: \(currentScore)"
}
func animateColorBox(tagNumber: Int, delay: Double){
let pauseBetweenAnimation = 1.25
let originalColor:UIColor = self.view.viewWithTag(tagNumber)!.backgroundColor!
UIView.animateWithDuration(1.0, delay: (delay - 1) * pauseBetweenAnimation, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.view.viewWithTag(tagNumber)!.alpha = 0.25
}, completion: { finished in
UIView.animateWithDuration(0.5, delay: (delay - 1) * pauseBetweenAnimation, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.view.viewWithTag(tagNumber)!.alpha = 1
}, completion: nil)
})
}
}
这里有一个可能适合您的解决方案:
func increaseLevel(){
// Set the delay time
var delayTime = 1.0 // This isn't actually used here
// Find a new random number
randomNumber = Int(arc4random_uniform(4) + 1)
// Append new random number to chained array
enemyArray.append(randomNumber)
// Call the animation method
animateColorBox(counter: 0)
}
func animateColorBox( counter counter: Int){
// If the tag number is within the enemyArray bounds
if( counter < enemyArray.count ){
tagNumber = enemyArray[counter];
// Set pause coefficient
let pauseBetweenAnimation = 1.25
// Save the original color for this view
let originalColor:UIColor = self.view.viewWithTag(tagNumber)!.backgroundColor!
// Do an animation for this view
UIView.animateWithDuration(1.0, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations:
{
self.view.viewWithTag(tagNumber)!.alpha = 0.25
}, completion:
{ finished in
// If finished with previous animation
if( finished ){
// Then set alpha back to normal
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.view.viewWithTag(tagNumber)!.alpha = 1
}, completion:
{
finished in
// If the view has its alpha back to normal
if( finished ){
// Call animateColorBox on next view in array
animateColorBox( counter: counter + 1);
}
})// End UIView animation
}// end if finished
})// end UIView Animation
}// end if counter < enemyArray.count
}
使用这种方法,如果我根据您的想法正确地考虑了这一点,您就可以在开始下一个动画之前完成集合中给定 UIView 的调光和取消调光的执行看法。让我知道这是否适合你。
编辑:
我意识到我去掉了动画之间的暂停,但你可以把它放在延迟区域中,根据你的需要进行调整。
哦,我意识到我的第一个 post 没有检查敌人数组中的值,所以让我改变一些东西。