Fading out any content which approaches edges of UIScollView

后端 未结 3 1596
有刺的猬
有刺的猬 2021-02-06 00:25

As the title says, I am trying to give to some UIImageViews a fading out effect as they get closer and closer to any of the four edges of my UIScrollView. Since the user can dra

3条回答
  •  野的像风
    2021-02-06 01:04

    Swift version of Nate answer, but only for top and bottom:

            let innerColor = UIColor(white: 1.0, alpha: 0.0).CGColor
            let outerColor = UIColor(white: 1.0, alpha: 1.0).CGColor;
    
            // define a vertical gradient (up/bottom edges)
            let colors = [outerColor, innerColor,innerColor,outerColor]
            let locations = [0.0, 0.15,0.85,1.0]
    
            var vMaskLayer : CAGradientLayer = CAGradientLayer()// layer];
            // without specifying startPoint and endPoint, we get a vertical gradient
            vMaskLayer.opacity = 0.7
            vMaskLayer.colors = colors;
            vMaskLayer.locations = locations;
            vMaskLayer.bounds = self.scrollView.bounds;
            vMaskLayer.anchorPoint = CGPointZero;
    
            // you must add the mask to the root view, not the scrollView, otherwise
            //  the masks will move as the user scrolls!
            self.view.layer.addSublayer(vMaskLayer)
    

提交回复
热议问题