Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 基于时间快慢减少单元格背景_Swift_Iphone_Uitableview_Colors_Cell - Fatal编程技术网

Swift 基于时间快慢减少单元格背景

Swift 基于时间快慢减少单元格背景,swift,iphone,uitableview,colors,cell,Swift,Iphone,Uitableview,Colors,Cell,我想确保我的手机有与剩余时间相关的背景。在这个意义上,我越接近0,我就越希望它被减少,这样我们就知道计时器即将过期 根据经过的时间,它会自动从右向左减少。 这是我在管理单元格时使用的代码 }我想你想要的是这样的东西: 这不是一件小事。我通过在视图中添加一个CAGradientLayer并设置其位置的动画来实现这一点。同时,我运行计时器来更改标签值 所以你可以这样做;当然,您可能需要调整这些值。这只是一个概念验证演示。问题是什么?@matt我想把基于时间缝合的颜色作为单元格的背景。我不知道“基

我想确保我的手机有与剩余时间相关的背景。在这个意义上,我越接近0,我就越希望它被减少,这样我们就知道计时器即将过期

根据经过的时间,它会自动从右向左减少。 这是我在管理单元格时使用的代码


}我想你想要的是这样的东西:

这不是一件小事。我通过在视图中添加一个CAGradientLayer并设置其
位置的动画来实现这一点。同时,我运行计时器来更改标签值


所以你可以这样做;当然,您可能需要调整这些值。这只是一个概念验证演示。

问题是什么?@matt我想把基于时间缝合的颜色作为单元格的背景。我不知道“基于时间缝合的颜色”是什么意思。如果要更改单元格背景色,请更改它。代码在哪里?“我没看到它。@马特抱歉,说到底我想写……问题仍然存在。我没有看到你的代码做任何会改变背景颜色的事情。
class TimerCell: UITableViewCell {

@IBInspectable var defaultBackgroundColor: UIColor = .white
@IBInspectable var runningBackgroundColor: UIColor = .white
@IBInspectable var pausedBackgroundColor: UIColor = .white

@IBInspectable var animationDuration: Double = 0

@IBOutlet var timeLabel: UILabel!
@IBOutlet var nameLabel: UILabel!
@IBOutlet var startButton: UIButton!
@IBOutlet var pauseButton: UIButton!
@IBOutlet var stopButton: UIButton!

weak var timer: Timer? {
    didSet {
        guard let timer = timer else {
            updater?.invalidate()
            return
        }
        
        if case .running = timer.state {
            startUpdater()
        }
        
        configure(animated: false)
    }
}

private weak var updater: Foundation.Timer?

override func awakeFromNib() {
    super.awakeFromNib()
}

override func setEditing(_ editing: Bool, animated: Bool) {
    print("*** \(Date()) setEditing(\(editing), animated: \(animated)) (timer?.name: \(String(describing: timer?.name)))")
    super.setEditing(editing, animated: animated)
    configure(animated: animated)
}

func configure(animated: Bool = true) {
    guard let timer = timer else {
        return
    }
    
    UIView.animate(withDuration: animated ? animationDuration : 0) {
        guard !self.isEditing else {
            self.timeLabel.text = timer.initialTime.hmsString
            
            self.startButton.safelySetIsHidden(true)
            self.pauseButton.safelySetIsHidden(true)
            self.stopButton.safelySetIsHidden(true)
            
            self.backgroundColor = self.defaultBackgroundColor
            
            return
        }
        
        self.timeLabel.text = ceil(timer.timeForState).hmsString
        self.nameLabel.text = timer.name
        
        switch timer.state {
        case .stopped:
            self.stopButton.safelySetIsHidden(true)
            self.pauseButton.safelySetIsHidden(true)
            
            self.startButton.safelySetIsHidden(false)
            
            self.backgroundColor = self.defaultBackgroundColor
        case .running:
            self.startButton.safelySetIsHidden(true)
            
            self.stopButton.safelySetIsHidden( ceil(timer.timeForState) == 0 ? true : false )
            self.pauseButton.safelySetIsHidden( ceil(timer.timeForState) == 0 ? true : false )
            self.backgroundColor = self.runningBackgroundColor
            
        case .paused:
            self.pauseButton.safelySetIsHidden(true)
            
            self.startButton.safelySetIsHidden(false)
            self.stopButton.safelySetIsHidden(false)
            
            self.backgroundColor = self.pausedBackgroundColor
        }
    }
}

@IBAction private func startTimer() {
    timer?.state = .running
    configure()
    startUpdater()
}

@IBAction private func pauseTimer() {
    timer?.state = .paused
    configure()
}

@IBAction private func stopTimer() {
    timer?.state = .stopped
    configure()
}

private func startUpdater() {
    guard let timer = timer else {
        return
    }
    let date = Date(timeIntervalSinceNow: timer.timeForState.truncatingRemainder(dividingBy: 1))
    let updater = Foundation.Timer(fire: date, interval: 1, repeats: true) {
        [weak timer] updater in
        self.configure()
        if timer?.state != .running {
            updater.invalidate()
        }
    }
    self.updater = updater
    RunLoop.main.add(updater, forMode: .common)
}