反正有没有把它变成for循环或改进它?

时间:2015-11-06 17:23:10

标签: ios swift loops

我有基本上想要的代码:将按钮图像更改一段时间然后重置它,然后更改另一个按钮等...

 buttonList[total[0]].setImage(imagePlay, forState: .Normal);
        let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(speed * Double(NSEC_PER_SEC)))
        dispatch_after(time, dispatch_get_main_queue()) {
            self.buttonList[total[0]].setImage(self.og, forState: .Normal);
            self.buttonList[total[1]].setImage(self.imagePlay, forState: .Normal);
            let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(speed * Double(NSEC_PER_SEC)))
            dispatch_after(time, dispatch_get_main_queue()) {
                self.buttonList[total[1]].setImage(self.og, forState: .Normal);
                self.buttonList[total[2]].setImage(self.imagePlay, forState: .Normal);
                let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(speed * Double(NSEC_PER_SEC)))
                dispatch_after(time, dispatch_get_main_queue()) {
                    self.buttonList[total[2]].setImage(self.og, forState: .Normal);
                    self.buttonList[total[3]].setImage(self.imagePlay, forState: .Normal);
                    let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(speed * Double(NSEC_PER_SEC)))
                    dispatch_after(time, dispatch_get_main_queue()) {
                        self.buttonList[total[3]].setImage(self.og, forState: .Normal);
                        self.buttonList[total[4]].setImage(self.imagePlay, forState: .Normal);
                        let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(speed * Double(NSEC_PER_SEC)))
                        dispatch_after(time, dispatch_get_main_queue()) {
                            self.buttonList[total[4]].setImage(self.og, forState: .Normal);
                            self.buttonList[total[5]].setImage(self.imagePlay, forState: .Normal);
                            let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(speed * Double(NSEC_PER_SEC)))
                            dispatch_after(time, dispatch_get_main_queue()) {
                                self.buttonList[total[5]].setImage(self.og, forState: .Normal);
                                self.buttonList[total[6]].setImage(self.imagePlay, forState: .Normal);
                                let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(speed * Double(NSEC_PER_SEC)))
                                dispatch_after(time, dispatch_get_main_queue()) {
                                    self.buttonList[total[6]].setImage(self.og, forState: .Normal);
                                    self.buttonList[total[7]].setImage(self.imagePlay, forState: .Normal);
                                    let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(speed * Double(NSEC_PER_SEC)))
                                    dispatch_after(time, dispatch_get_main_queue()) {
                                            self.buttonList[total[7]].setImage(self.og, forState: .Normal);
                                            self.buttonList[total[8]].setImage(self.imagePlay, forState: .Normal);
                                            let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(speed * Double(NSEC_PER_SEC)))
                                            dispatch_after(time, dispatch_get_main_queue()) {
                                                self.buttonList[total[8]].setImage(self.og, forState: .Normal);
                                                self.replayButton.enabled = true;
                                                for(var i = 0; i < self.buttonList.count; i++){
                                                    self.buttonList[i].enabled = true;

正如您所看到的,这是一个漫长的过程,因为最终我将拥有大小为30+的数组,有人可以提出更好的方法吗?谢谢。

3 个答案:

答案 0 :(得分:1)

除非我误解了你的代码,否则递归可能会有所帮助

buttonList[total[0]].setImage(imagePlay, forState: .Normal);
delayedImageChangeForIndex(0)

然后

func delayedImageChangeForIndex(index: Int) {
    let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(speed * Double(NSEC_PER_SEC)))
    dispatch_after(time, dispatch_get_main_queue()) {
        self.buttonList[total[index]].setImage(self.og, forState: .Normal);
        self.buttonList[total[index + 1]].setImage(self.imagePlay, forState: .Normal);

        index++
        if (index == 30) {
           // when reached max index, break the recursion
           return
        }           

        //recursion here
        delayedImageChangeForIndex(index)
    }   
}

不确定total数组的来源。如果它是类属性,请使用self,否则您需要将其作为delayedImageChangeForIndex

的参数发送

答案 1 :(得分:1)

你可以使用一个只提前安排一切的功能,如下所示:

func updateButtonImages() {
    let buttons = total.map { buttonList[$0] }
    buttons[0].setImage(imagePlay, forState: .Normal)

    var time = dispatch_time(DISPATCH_TIME_NOW, 0)
    for (priorButton, currentButton) in zip(buttons[0..<buttons.count - 1], buttons[1..<buttons.count]) {
        time = dispatch_time(time, Int64(speed * Double(NSEC_PER_SEC)))
        dispatch_after(time, dispatch_get_main_queue()) {
            priorButton.setImage(og, forState: .Normal)
            currentButton.setImage(imagePlay, forState: .Normal)
        }
    }

    time = dispatch_time(time, Int64(speed * Double(NSEC_PER_SEC)))
    dispatch_after(time, dispatch_get_main_queue()) {
        buttons.last?.setImage(og, forState: .Normal)
        replayButton.enabled = true
        buttonList.forEach { $0.enabled = true }
    }
}

或者您可以使用重新安排自己的功能,如下所示:

func updateButtonImages(currentIndex currentIndex: Int = 0, priorIndex: Int? = nil) {
    if let priorIndex = priorIndex {
        buttonList[total[priorIndex]].setImage(og, forState: .Normal)
    }

    if currentIndex == total.count {
        replayButton.enabled = true
        buttonList.forEach {
            $0.enabled = true
        }
        return
    }

    buttonList[total[currentIndex]].setImage(imagePlay, forState: .Normal)

    let nextUpdateTime = dispatch_time(DISPATCH_TIME_NOW, Int64(speed * Double(NSEC_PER_SEC)))
    dispatch_after(nextUpdateTime, dispatch_get_main_queue()) {
        updateButtonImages(currentIndex: currentIndex + 1, priorIndex: currentIndex)
    }
}

无论哪种方式,只需致电updateButtonImages()即可开始此过程。

答案 2 :(得分:-1)

是的,您可以使用递归函数。它可以帮助你很好。