gpt4 book ai didi

ios - 快速动画循环进度 View (计时器)

转载 作者:搜寻专家 更新时间:2023-10-31 23:08:22 25 4
gpt4 key购买 nike

<分区>

我有这个循环进度 View 的代码,它显示了你通过计时器的进度,例如您将计时器设置为 60 秒,当您达到 30 秒时进度条就在中间。代码如下所示:

import UIKit

class TimerCircularProgressView: UIView {

@IBInspectable var timeSeconds:CGFloat = CGFloat(timers.seconds[timers.timerID]) {
didSet {
setNeedsDisplay()
}
}

@IBInspectable var timeLabelString:String = "" {
didSet {
setNeedsDisplay()
}
}


private var startAngle = CGFloat(-90 * Double.pi / 180)
private var endAngle = CGFloat(270 * Double.pi / 180)

override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

private func setup() {
backgroundColor = .clear
}

override func draw(_ rect: CGRect) {
// General Declarations
guard let context = UIGraphicsGetCurrentContext() else {
return
}

// Colour Declarations
let progressColour = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
let progressBackgroundColour = UIColor(red: 1, green: 1, blue: 1, alpha: 0.4)
// let titleColour = UIColor(red: 1, green: 1, blue: 1, alpha: 0.8) // let titleColour = UIColor(red: 1, green: 1, blue: 1, alpha: 0.8)

// Shadow Declarations
let innerShadow = UIColor.black.withAlphaComponent(0.22)
let innerShadowOffset = CGSize(width: 3.1, height: 3.1)
let innerShadowBlurRadius = CGFloat(4)

// Background Drawing
let backgroundPath = UIBezierPath(ovalIn: CGRect(x: rect.minX, y: rect.minY, width: rect.width, height: rect.height))
backgroundColor?.setFill()
backgroundPath.fill()

// ProgressBackground Drawing
let progressPadding = CGFloat(timers.deviceWidth / 32 * 1.5) // 15
print(progressPadding)
print(timers.deviceWidth)

let progressBackgroundPath = UIBezierPath(ovalIn: CGRect(x: rect.minX + progressPadding/2, y: rect.minY + progressPadding/2, width: rect.size.width - progressPadding, height: rect.size.height - progressPadding))
progressBackgroundColour.setStroke()
progressBackgroundPath.lineWidth = timers.deviceWidth / 32 // 5, 10
progressBackgroundPath.stroke()

// Progress Drawing
let progressRect = CGRect(x: rect.minX + progressPadding/2, y: rect.minY + progressPadding/2, width: rect.size.width - progressPadding, height: rect.size.height - progressPadding)
let progressPath = UIBezierPath()
progressPath.addArc(withCenter: CGPoint(x: progressRect.midX, y: progressRect.midY), radius: progressRect.width / 2, startAngle: startAngle, endAngle: (endAngle - startAngle) * (CGFloat(timers.seconds[timers.timerID]/timers.seconds[timers.timerID]) - (timeSeconds / CGFloat(timers.seconds[timers.timerID]))) + startAngle, clockwise: true)
progressColour.setStroke()
progressPath.lineWidth = timers.deviceWidth / 32 * 0.8 // Thickness of the progress line
progressPath.lineCapStyle = CGLineCap.round
progressPath.stroke()

// Text Drawing
let textRect = CGRect(x: rect.minX, y: rect.minY, width: rect.size.width, height: rect.size.height)
let textContent = NSString(string: timeLabelString)
let textStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
textStyle.alignment = .center

let textFontAttributes = [
NSAttributedStringKey.font: UIFont(name: "Helvetica", size: rect.width / timers.timerFontSize)!, // 3, HelveticaNeue-Light
NSAttributedStringKey.foregroundColor: UIColor.black, // NSAttributedStringKey.foregroundColor: titleColor,
NSAttributedStringKey.paragraphStyle: textStyle]

let textHeight = textContent.boundingRect(with: CGSize(width: textRect.width, height: textRect.height), options: .usesLineFragmentOrigin, attributes: textFontAttributes, context: nil).height

context.saveGState()
context.clip(to: textRect)
textContent.draw(in: CGRect(x: textRect.minX, y: textRect.minY + (textRect.height - textHeight) / 2, width: textRect.width, height: textHeight), withAttributes: textFontAttributes)
context.restoreGState();
}

}

目前,圆形进度条每秒更新一次,这意味着在短时间内,进度条移动的动画会跳动,不流畅。

我已经尝试设置一个大约 0.05 秒间隔的计时器,但这可能会导致计时不准确,所以我尝试了 0.1 和 0.2,它们仍然显得相当不稳定。

@objc func processSlider() {

if secondsDDecimal > 0 {
secondsDDecimal -= 0.2
}

circularProgressView.timeSeconds = CGFloat(secondsDDecimal)

}

有没有一种方法可以让循环进度 View 的过渡动画一秒一秒,使其平滑而不跳跃?

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