CABasicAnimation для маски в UIView - PullRequest
       29

CABasicAnimation для маски в UIView

0 голосов
/ 22 февраля 2019

Я добавил маску к своему виду и сейчас пытаюсь ее оживить.Я пытался адаптировать решение от https://stackoverflow.com/a/36461202/642502, но это не для меня.Я попытался поменять местами место добавления анимации и удаления setDisabledActions, но продолжаю получать те же результаты.

extension UIView {
    public func mask(with rect: CGRect?, inverse: Bool = false, animated: Bool = true, duration: TimeInterval = 1.0) {

    guard let rect = rect else {
        self.layer.mask = nil
        return
    }

    let path = UIBezierPath(rect: rect)
    let maskLayer = CAShapeLayer()

    if inverse {
        path.append(UIBezierPath(rect: self.bounds))
        maskLayer.fillRule = .evenOdd
    }

    if animated {
        let animation = CABasicAnimation(keyPath: "path")
        animation.fromValue = maskLayer.path
        animation.toValue = path.cgPath
        animation.duration = duration
        animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        animation.isRemovedOnCompletion = false
        maskLayer.add(animation, forKey: "selectAnimation")
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        maskLayer.path = path.cgPath
        self.layer.mask = maskLayer
        CATransaction.commit()
    } else {
        maskLayer.path = path.cgPath
        self.layer.mask = maskLayer
    }

  }
}

current selection

...