swift 3转换后,class无法正常工作

时间:2016-11-19 05:13:47

标签: ios swift3 xcode8.1

我的班级(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)

仍然没有显示任何进展,任何人都有任何线索在这里错了然后让我知道

1 个答案:

答案 0 :(得分:1)

你没有指明究竟什么不起作用,但是当我测试你的代码时它显示进度唯一不起作用的是它没有显示innerCirclePathLayer,因为你没有划分你的{{1} } RGB的颜色,因为255接受init(red:green:blue:alpha:)0.0之间的值和1.0之上的值被解释为1.0。因此,尝试将RGB分割为1.0

255