【问题标题】:I have calayer (+animation) hidden behind the parent's layer我有隐藏在父层后面的 calayer (+animation)
【发布时间】:2021-03-05 10:16:13
【问题描述】:

我在父 CALayer 后面隐藏了一个动画

import bvkit
// https://stackoverflow.com/questions/18835915/cashapelayer-animation-arc-from-0-to-final-size
class ProgressLayer: CAShapeLayer
{
    init(ellipseIn: CGRect)
    {
        super.init()
        lineWidth=4
//        let path: UIBezierPath = UIBezierPath()
//        size = ellipseIn.size
//        path.addArc(withCenter: CGPoint(x: size.width / 2, y: size.height / 2),
//                    radius: size.width / 2 - lineWidth * 2,
//                    startAngle: CGFloat(-Double.pi / 2),
//                    endAngle: CGFloat(-Double.pi / 2),
//                    clockwise: true)
        let angle = -CGFloat.pi/2
        let rotation = CGAffineTransform(rotationAngle: angle)
        let translation = CGAffineTransform(translationX: 0,y: 80)
        var concat = rotation.concatenating(translation)
        
//        let unsafe = withUnsafeMutablePointer(&rotation)
        self.path = //path.cgPath//
            CGPath(ellipseIn: ellipseIn, transform: &concat)
        lineCap = CAShapeLayerLineCap.round
        self.strokeEnd=0
    }
    // https://stackoverflow.com/questions/38468515/cabasicanimation-creates-empty-default-value-copy-of-calayer/38468678#38468678
    override init(layer: Any) {
//        if let layer = layer as? ProgressLayer {
//            // no properties to copy over just yet
//        }
        super.init(layer: layer)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func computePath(r: CGRect)
    {
        self.path = CGPath(ellipseIn: r, transform: nil)
    }
    
    func show(progress: CGFloat, duration: TimeInterval)
    {
        if self.strokeEnd>1 {
            self.strokeEnd=0
        }
        let swipe = CABasicAnimation(keyPath:"strokeEnd")
        swipe.duration = duration;
        let old = Float(strokeEnd)
        swipe.fromValue = NSNumber(value: old)
        let newEnd = progress
        swipe.toValue =  NSNumber(value: Float(newEnd))

        swipe.fillMode = CAMediaTimingFillMode.forwards;
        swipe.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        swipe.isRemovedOnCompletion=false
        self.strokeEnd = newEnd
        add(swipe, forKey: "strokeEnd animation")
    }
}

就是这样设置的

    hoster.layer.cornerRadius = 40
    hoster.layer.borderWidth = 4
    hoster.layer.borderColor = UIColor.appv3BackgroundSecondaryColor.cgColor

    pl.strokeColor = UIColor.appv3MainAccentColor.cgColor
    pl.fillColor = nil
    pl.zPosition = -10
    pl.position = CGPoint(x: 40, y: 40)
        //UIColor.appv3BackgroundSecondaryColor.cgColor
    hoster.layer.addSublayer(pl)

无论 ProgressLayer 实例的 zPosition 是正数还是负数,它都是 在宿主视图的图层边框后面绘制(圆圈的光栅化方式不同 因此我在父级后面看到一个“电晕”或我的动画圆形层)

我怎样才能让 ProgressView 在主机层支持之上绘制????

【问题讨论】:

    标签: ios core-animation calayer


    【解决方案1】:

    我冒险走向懦夫的道路,并重新获得了一层视图 解决了问题的食物链更高层

    【讨论】:

    • 另一种解决方案是在视图层下添加您的层(使用insertSublayer(_:below:)。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2011-03-16
    • 2012-04-19
    • 2018-09-08
    相关资源
    最近更新 更多