How to mask UIViews in iOS

前端 未结 3 700
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 23:59

I\'ve seen similar questions, but haven\'t found workable answers.

I want to mask a UIView using a grey image (need to convert to alpha scale for masking). The UIVi

3条回答
  •  有刺的猬
    2020-12-01 00:25

    For apps targeting iOS 8.0+ this worked well (in this case, using a gradient as the mask) It avoids any need to resize or position the mask.

    // Add gradient mask to view
    func addGradientMask(targetView: UIView)
    {
        let gradientMask = CAGradientLayer()
    
        gradientMask.frame = targetView.bounds
        gradientMask.colors = [UIColor.blackColor().CGColor, UIColor.clearColor().CGColor]
        gradientMask.locations = [0.8, 1.0]
    
        let maskView: UIView = UIView()
        maskView.layer.addSublayer(gradientMask)
    
        targetView.maskView = maskView 
    }
    

    In my case, I want to remove the mask once the user starts scrolling. This is done with:

    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    
        exerDetailsTableView.maskView = nil        
    }
    

    where the view is defined as an @IBOutlet:

    @IBOutlet weak var exerDetailsTableView: UITableView!
    

    Result:

提交回复
热议问题