【问题标题】:reduce the cell background based on time swift根据时间迅速减少单元格背景
【发布时间】:2021-03-21 23:43:16
【问题描述】:

我想确保我的单元格具有与剩余时间相关的背景。从某种意义上说,我越接近 0,我就越希望它减少,以便我们了解计时器即将到期。

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

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)
}

}

【问题讨论】:

  • 有什么问题?
  • @matt 我想将根据时间缝合的颜色作为单元格的背景
  • 我不知道“根据时间缝合的颜色”是什么意思。如果要更改单元格背景颜色,请更改它。执行此操作的代码在哪里?我没看到。
  • @matt 抱歉,归根结底我想写..
  • 问题依然存在。我没有看到你的代码做任何会改变背景颜色的事情。

标签: swift iphone uitableview colors cell


【解决方案1】:

我认为你追求的是这样的:

这并非易事。我通过向视图添加 CAGradientLayer 并为其locations 属性设置动画来做到这一点。同时,我运行了计时器来更改标签值。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-21
    • 2016-11-28
    • 1970-01-01
    • 2012-01-29
    • 2013-11-23
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多