HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何为倒计时器创建循环进度指示器大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试添加倒计时到我的应用程序,我有动画这个问题.我想要的外观类似于下面显示的iPad倒计时,红色条随着时钟倒计时而增加.

最初我创建了一个图像地图集,每个倒计时的半秒都有一个单独的图像,但这似乎需要大量内存才能运行,因此崩溃了应用程序,所以我现在必须找到替代方案.

我一直在寻找着色在这里https://developer.apple.com/library/prerelease/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Sprites/Sprites.html#//apple_ref/doc/uid/TP40013043-CH9,但看不到一种方法来着色精灵的一部分,似乎整个精灵将被改变.

有没有人知道在这里是否可以选择着色,还是有办法减少图像地图集使用的内存?

谢谢

解决方法

你可以使用CAShapeLayer和动画结束动画如下:

更新Xcode 9•Swift 4

定义剩下的时间

let timeLeftShapeLayer = CAShapeLayer()
let bgShapeLayer = CAShapeLayer()
var timeLeft: TimeInterval = 60
var endTime: Date?
var timeLabel =  UILabel()
var timer = Timer()
// here you create your basic animation object to animate the strokeEnd
let strokeIt = CABasicAnimation(keyPath: "strokeEnd")

定义一个创建de UIBezierPath的方法

startAngle在-90˚和endAngle 270

func drawBgShape() {
    bgShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX,y: view.frame.midY),radius:
        100,startAngle: -90.degreesToradians,endAngle: 270.degreesToradians,clockwise: truE).cgPath
    bgShapeLayer.strokeColor = UIColor.white.cgColor
    bgShapeLayer.fillColor = UIColor.clear.cgColor
    bgShapeLayer.lineWidth = 15
    view.layer.addSublayer(bgShapeLayer)
}
func drawTimeLeftShape() {
    timeLeftShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX,clockwise: truE).cgPath
    timeLeftShapeLayer.strokeColor = UIColor.red.cgColor
    timeLeftShapeLayer.fillColor = UIColor.clear.cgColor
    timeLeftShapeLayer.lineWidth = 15
    view.layer.addSublayer(timeLeftShapeLayer)
}

添加您的标签

func addTimeLabel() {
    timeLabel = UILabel(frame: CGRect(x: view.frame.midX-50,y: view.frame.midY-25,width: 100,height: 50))
    timeLabel.textAlignment = .center
    timeLabel.text = timeLeft.time
    view.addSubview(timeLabel)
}

在viewDidload中设置endTime并将CAShapeLayer添加到视图中:

override func viewDidLoad() {
    super.viewDidLoad()
    view.BACkgroundColor = UIColor(white: 0.94,alpha: 1.0)
    drawBgShape()
    drawTimeLeftShape()
    addTimeLabel()
    // here you define the fromValue,toValue and duration of your animation
    strokeIt.fromValue = 0
    strokeIt.toValue = 1
    strokeIt.duration = timeLeft
    // add the animation to your timeLeftShapeLayer
    timeLeftShapeLayer.add(strokeIt,forKey: nil)
    // define the future end time by adding the timeLeft to Now Date()
    endTime = Date().addingTimeInterval(timeLeft)
    timer = Timer.scheduledTimer(timeInterval: 0.1,target: self,SELEctor: #SELEctor(updatetiR_107_11845@E),userInfo: nil,repeats: truE)
}

更新时间

@objc func updatetiR_107_11845@e() {
    if timeLeft > 0 {
        timeLeft = endTime?.timeIntervalSinceNow ?? 0
        timeLabel.text = timeLeft.time
    } else {
        timeLabel.text = "00:00"
        timer.invalidate()
    }
}

您可以使用此扩展名将度数转换为弧度并显示时间

extension TimeInterval {
    var time: String {
        return String(format:"%02d:%02d",Int(self/60),Int(ceil(truncaTingRemainder(dividingBy: 60))) )
    }
}
extension Int {
    var degreesToradians : CGFloat {
        return CGFloat(self) * .pi / 180
    }
}

Sample project

大佬总结

以上是大佬教程为你收集整理的ios – 如何为倒计时器创建循环进度指示器全部内容,希望文章能够帮你解决ios – 如何为倒计时器创建循环进度指示器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。