0

我的课程(UIView)在 Xcode 8.1 的 swift 3 转换后无法工作我不知道这里出了什么问题,这个课程是一个进度视图,转换后看起来很好,但我的进度不可见,这是我的课程在转换后:

class CircularLoaderView: UIView, CAAnimationDelegate {
    let circlePathLayer = CAShapeLayer()
    let circleRadius: CGFloat = 60.0
    let innerCirclePathLayer = CAShapeLayer()
    let innerCircleRadius: CGFloat = 60.0
    override init(frame: CGRect) {
        super.init(frame: frame)
        configure()
        innerConfigure()
    }
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        configure()
        innerConfigure()
    }
    func configure() {
        circlePathLayer.frame = bounds
        circlePathLayer.lineWidth = 10
        circlePathLayer.fillColor = UIColor.clear.cgColor
        circlePathLayer.strokeColor = UIColor.darkGray.cgColor
        layer.addSublayer(circlePathLayer)
        backgroundColor = UIColor.clear
        progress = 0
    }
    func innerConfigure() {
        innerCirclePathLayer.frame = bounds
        innerCirclePathLayer.lineWidth = 10
        innerCirclePathLayer.fillColor = UIColor.clear.cgColor
        innerCirclePathLayer.strokeColor = UIColor(red: 100, green: 60, blue: 70, alpha: 0.2).cgColor
        layer.addSublayer(innerCirclePathLayer)
        backgroundColor = UIColor.clear
    }
    func innerCircleFrame() -> CGRect {
        var circleFrame = CGRect(x: 0, y: 0, width: 2*innerCircleRadius, height: 2*innerCircleRadius)
        circleFrame.origin.x = innerCirclePathLayer.bounds.midX - circleFrame.midX
        circleFrame.origin.y = innerCirclePathLayer.bounds.midY - circleFrame.midY
        return circleFrame
    }
    func innerCirclePath() -> UIBezierPath {
        return UIBezierPath(ovalIn: innerCircleFrame())
    }
    func circleFrame() -> CGRect {
        var circleFrame = CGRect(x: 0, y: 0, width: 2*circleRadius, height: 2*circleRadius)
        circleFrame.origin.x = circlePathLayer.bounds.midX - circleFrame.midX
        circleFrame.origin.y = circlePathLayer.bounds.midY - circleFrame.midY
        return circleFrame
    }
    func circlePath() -> UIBezierPath {
        return UIBezierPath(ovalIn: circleFrame())
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        circlePathLayer.frame = bounds
        circlePathLayer.path = circlePath().cgPath
        innerCirclePathLayer.frame = bounds
        innerCirclePathLayer.path = innerCirclePath().cgPath
    }
    var progress: CGFloat {
        get {
            return circlePathLayer.strokeEnd
        }
        set {
            if (newValue > 1) {
                circlePathLayer.strokeEnd = 1
            } else if (newValue < 0) {
                circlePathLayer.strokeEnd = 0
            } else {
                circlePathLayer.strokeEnd = newValue
            }
        }
    }
    func reveal() {
        // 1
        backgroundColor = UIColor.clear
        progress = 1
        // 2
        circlePathLayer.removeAnimation(forKey: "strokeEnd")
        // 3
        circlePathLayer.removeFromSuperlayer()
        superview?.layer.mask = circlePathLayer

        // 1
        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        let finalRadius = sqrt((center.x*center.x) + (center.y*center.y))
        let radiusInset = finalRadius - circleRadius
        let outerRect = circleFrame().insetBy(dx: -radiusInset, dy: -radiusInset)
        let toPath = UIBezierPath(ovalIn: outerRect).cgPath
        // 2
        let fromPath = circlePathLayer.path
        let fromLineWidth = circlePathLayer.lineWidth
        // 3
        CATransaction.begin()
        CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
        circlePathLayer.lineWidth = 2*finalRadius
        circlePathLayer.path = toPath
        CATransaction.commit()
        // 4
        let lineWidthAnimation = CABasicAnimation(keyPath: "lineWidth")
        lineWidthAnimation.fromValue = fromLineWidth
        lineWidthAnimation.toValue = 2*finalRadius
        let pathAnimation = CABasicAnimation(keyPath: "path")
        pathAnimation.fromValue = fromPath
        pathAnimation.toValue = toPath
        // 5
        let groupAnimation = CAAnimationGroup()
        groupAnimation.duration = 1
        groupAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        groupAnimation.animations = [pathAnimation, lineWidthAnimation]
        groupAnimation.delegate = self
        circlePathLayer.add(groupAnimation, forKey: "strokeWidth")
    }
     func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        superview?.layer.mask = nil
    }
}

这就是我设置进度的方式:

cell.loaderView?.progress = CGFloat(receivedSize)/CGFloat(expectedSize)

它仍然没有显示任何进展,任何人都知道这里出了什么问题然后让我知道

4

1 回答 1

1

您还没有指定究竟是什么不起作用,但是当我测试您的代码时,它显示了进度,唯一不起作用的是它没有向我显示,innerCirclePathLayer因为您没有划分RGB颜色,255因为init(red:green:blue:alpha:)接受值在0.0to1.0和上面的值之间1.0被解释作为1.0. 所以尝试一次将 RGB 划分为255.

innerCirclePathLayer.strokeColor = UIColor(red: 100/255.0, green: 60/255.0, blue: 70/255.0, alpha: 0.2).cgColor
于 2016-11-19T05:48:29.370 回答