iOS 13奇怪的UIView.animation行为

时间:2019-06-27 14:50:32

标签: ios swift

我有很棒的通用动画,可以在iOS 11和12上正常工作。

Before iOS 13

extension UIImage {

    enum ScalingMode {
        case aspectFill
        case aspectFit

        func aspectRatio(between size: CGSize, and otherSize: CGSize) -> CGFloat {
            let aspectWidth  = size.width/otherSize.width
            let aspectHeight = size.height/otherSize.height

            switch self {
            case .aspectFill:
                return max(aspectWidth, aspectHeight)
            case .aspectFit:
                return min(aspectWidth, aspectHeight)
            }
        }
    }

    func scaled(to newSize: CGSize, scalingMode: UIImage.ScalingMode = .aspectFill) -> UIImage {

        let aspectRatio = scalingMode.aspectRatio(between: newSize, and: size)

        var scaledImageRect = CGRect.zero

        scaledImageRect.size.width  = size.width * aspectRatio
        scaledImageRect.size.height = size.height * aspectRatio
        scaledImageRect.origin.x    = (newSize.width - size.width * aspectRatio) / 2.0
        scaledImageRect.origin.y    = (newSize.height - size.height * aspectRatio) / 2.0

        UIGraphicsBeginImageContextWithOptions(newSize, false, 0)

        draw(in: scaledImageRect)
        let scaledImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return scaledImage!
    }

    func overlayImage(color: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size, false, 0)
        let context = UIGraphicsGetCurrentContext()

        color.setFill()

        context!.translateBy(x: 0, y: self.size.height)
        context!.scaleBy(x: 1.0, y: -1.0)

        context!.setBlendMode(CGBlendMode.colorBurn)
        let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        context!.draw(self.cgImage!, in: rect)

        context!.setBlendMode(CGBlendMode.sourceIn)
        context!.addRect(rect)
        context!.drawPath(using: CGPathDrawingMode.fill)

        let coloredImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return coloredImage!
    }
}

open class CustomActivityIndicator: NSObject {

    private var downLogo : UILabel? = nil
    private var topLogo : UILabel? = nil

    private let window = UIApplication.shared.keyWindow!

    open func start() {

        let image = UIImage(named: "1.png")

        let scaledimage = image!.scaled(to: CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), scalingMode: .aspectFit)

        downLogo = UILabel(frame: UIScreen.main.bounds)
        downLogo!.backgroundColor = UIColor(patternImage: scaledimage.overlayImage(color: UIColor.lightGray))

        topLogo = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: UIScreen.main.bounds.height))
        topLogo!.lineBreakMode = NSLineBreakMode.byClipping
        topLogo!.backgroundColor = UIColor(patternImage: scaledimage)

        window.addSubview(downLogo!)

        window.addSubview(topLogo!)

        UIView.animate(withDuration: 1.5, delay: 0, options: [.curveLinear, .repeat, .autoreverse], animations: { [unowned self] () -> Void in
            self.topLogo!.frame = self.downLogo!.frame

        }, completion: nil)
    }
}

但是在iOS 13上我们有这种行为。

iOS 13 behavior

您可以看到彩色图像的变化坐标,但是Xcode对两个图像显示相同的坐标。

我进行了一些测试,发现是否可以对两个图像进行动画处理。

UIView.animate(withDuration: 1.5, delay: 0, options: [.curveLinear, .repeat, .autoreverse], animations: { [unowned self] () -> Void in
        self.topLogo!.frame = self.downLogo!.frame
        self.downLogo!.frame.size.width = UIScreen.main.bounds.width - 1
    }, completion: nil)

1 个答案:

答案 0 :(得分:0)

看起来Apple改变了UILabel类中的某些内容。因此,将我的标签更改为imageView,现在可以正常使用了。