gpt4 book ai didi

For 循环中的 IOS 动画。调整了每个延迟但仍然有问题

转载 作者:行者123 更新时间:2023-11-29 01:37:32 25 4
gpt4 key购买 nike

我正在创建一个模式匹配游戏。基本上我在屏幕上有 4 个不同颜色的方形 UIView。我希望一个方 block 随机闪烁,然后另一个方 block 随机闪烁。例如,当游戏开始时,假设随机选择一个红色框来闪烁。现在,在第二次迭代中,红色框应该再次闪烁,然后是另一个随机选择的框,可以说它是蓝色的。所以现在我们有红色和蓝色。现在循环再次开始,现在是红色、蓝色、绿色。这就是我想要完成的。如果这没有意义,请下载 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
}
通过这种方法,如果我从我认为你想要的角度正确地思考这个问题,你就可以在开始下一个 View 的动画之前完成集合中给定 UIView 的调光和取消调光的执行。让我知道这是否适合您。

编辑:

我意识到我取消了动画之间的暂停,但您可以将其放入延迟区域以根据您的需要进行调整。

哦,我意识到我的第一篇文章没有检查敌人数组中的值,所以让我改变一些东西。

关于For 循环中的 IOS 动画。调整了每个延迟但仍然有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32808826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com