繁体   English   中英

如何快速从图像创建循环加载器

[英]How to create a circular loader from an image in swift

我进行了一些研究,但不幸的是,仅发现了一些允许使用彩色圆形装载机且未使用图像的库。

所以这是我的问题,我有一个循环倒计时,在我要实现的设计中,它使用了一个复杂的辉光,我发现它很难再现,如图所示。 发光持续约3秒钟绕圈显示此倒数的进度。

我最初的想法是尝试修改UIView + Glow,以使辉光不会发生变化,但即使到那时,当我将UIView径向隐藏时,我也会停下来。

因此,我现在正在考虑简单地导出使进度条作为图像的外部辉光,并径向隐藏该辉光(最终这样做会更快,更简单,而不是尝试手动创建完全相同的辉光)。

有谁知道我应该从哪里开始看或者我应该怎么做才能使用角度/弧度隐藏圆形图像的一部分?

编辑:这是发光的样子,上面覆盖着圆形标签(黑色圆圈),用于显示倒计时值。

在此处输入图片说明

首先,我要感谢@ Carpsen90的帮助,尽管他的回答不是我想要的,但它帮助我了解了如何使用CABasicAnimation并结合了该答案的灵感,我找到了解决问题的方法,使用图像和CAShapeLayer

对于那些想知道我在做什么的人,这是我使用的代码

// countdownGlow is the glow (white and red part of my image)
func animateMask() {
        let arcPath = CGMutablePath()
        arcPath.move(to: CGPoint(x: self.countdownGlow.frame.width / 2, y: 0))
        arcPath.addArc(center: CGPoint(x: self.countdownGlow.frame.width / 2, y: self.countdownGlow.frame.width / 2), radius: self.countdownGlow.frame.width / 2, startAngle: CGFloat(-1 * Double.pi / 2), endAngle: CGFloat(-3 * Double.pi / 2), clockwise: false)
        arcPath.addArc(center: CGPoint(x: self.countdownGlow.frame.width / 2, y: self.countdownGlow.frame.width / 2), radius: self.countdownGlow.frame.width / 2, startAngle: CGFloat(-3 * Double.pi / 2), endAngle: CGFloat(-5 * Double.pi / 2), clockwise: false)
        let ringLayer = CAShapeLayer(layer: self.countdownGlow.layer)
        ringLayer.path = arcPath

        ringLayer.strokeEnd = 0.0
        ringLayer.fillColor = UIColor.clear.cgColor
        ringLayer.strokeColor = UIColor.black.cgColor
        ringLayer.lineWidth = self.countdownGlow.frame.width / 2

        self.countdownGlow.layer.mask = ringLayer
        self.countdownGlow.layer.mask?.frame = self.countdownGlow.layer.bounds

        let swipe = CABasicAnimation(keyPath: "strokeEnd")
        swipe.duration = 3
        swipe.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        swipe.fillMode = kCAFillModeForwards
        swipe.isRemovedOnCompletion = true
        swipe.toValue = 1.0
        ringLayer.add(swipe, forKey: "strokeEnd")
}

我似乎无法简单地告诉我圆弧是一个完整的圆,所以我走了半个圆,然后又跑了一个半圆

如果您有任何改进,我很乐意尝试一下,因为我确信我的解决方案根本没有优化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM