代码之家  ›  专栏  ›  技术社区  ›  Scaraux

uiview动画阴影路径不跟随

  •  1
  • Scaraux  · 技术社区  · 6 年前

    我有一个 UIView 用一个 expand 我做的功能。 当我调用这个函数时,uiview保持居中,我扩展它的宽度。

    问题是我的 UIVIEW 有一个 shadow ,带有 shadowPath . 每当我展开它时,阴影都会立即展开到视图在动画结束时得到的最大宽度,而不是跟随边界。

    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
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   rob mayoff    6 年前

    UIView shadowPath