UIScrollView上的渐变掩码很难更新

时间:2016-02-22 00:00:21

标签: swift uiscrollview gradient mask

我有一个UIScrollView子类,包含一个非常宽的UIView子类,我希望淡出可见内容的边缘。这是我在UIScrollView子类中得到的内容:

private var gradientMask = CAGradientLayer()
override func layoutSubviews() {
    super.layoutSubviews()

    gradientMask.frame = self.bounds
    gradientMask.colors = [UIColor(white: 0.0, alpha: 0.18).CGColor,
        UIColor(white: 0.0, alpha: 0.95).CGColor,
        UIColor(white: 0.0, alpha: 0.95).CGColor,
        UIColor(white: 0.0, alpha: 0.0).CGColor
    ]

    let fadeStart:CGFloat = 50.0 / self.bounds.width
    gradientMask.startPoint = CGPoint(x:0, y:0.5)
    gradientMask.endPoint = CGPoint(x:1, y:0.5)
    let fadeEnd = (self.bounds.width - 40.0) / self.bounds.width
    gradientMask.locations = [fadeStart,fadeStart+0.1,fadeEnd,1.0]

    self.layer.mask = gradientMask
}   

它确实有效,但存在滞后现象。滚动后,渐变似乎随内容滚动一会儿,然后快速回到预期位置。如何确保滚动视图的边缘淡出 - 滚动时渐变不会移动?

1 个答案:

答案 0 :(得分:4)

修复只是将UIScrollView子类嵌入到已应用渐变的视图中。就是这样!