Scaraux
Scaraux

Reputation: 4152

UIView animation shadowPath doesn't follow

I have a UIView with a expand function that I made. When I call this function, the UIView stays centered, and I expand its width.

The issue is that my UIView has a shadow, with a shadowPath. Whenever I expand it, the shadow is imediatly expanded to the maximum width the view will get at the end of the animation, instead of following the bounds.

func expand() {
    guard self.expanded == false else {
        return
    }

    let originX = self.frame.origin.x
    let originY = self.frame.origin.y
    let originWidth = self.frame.width
    let originHeight = self.frame.height
    let newWidth = self.frame.width * 2

    let newRect = CGRect(x: originX - ((newWidth - originWidth) / 2), y: originY, width: newWidth, height: originHeight)

    self.expanded = true

    UIView.animate(withDuration: 0.4, animations: {
        self.frame = newRect
        self.layoutIfNeeded()
    }) { (completed) in
    }
}


override func layoutSubviews() {
    super.layoutSubviews()
    self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath
}

Upvotes: 1

Views: 192

Answers (1)

rob mayoff
rob mayoff

Reputation: 385500

UIView explicitly (but privately) chooses which of its layer's properties to implicitly animate, and shadowPath is not one of them. You have to animate it yourself.

Upvotes: 1

Related Questions