How can I add a gradient that spans two views?

后端 未结 2 460
盖世英雄少女心
盖世英雄少女心 2020-12-21 16:05

I know how to do (1) but how can I do (2)?

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
CAGradientLayer *gradient = [CAGradientLa         


        
2条回答
  •  天命终不由人
    2020-12-21 16:43

    You can do this by adding a view on top of the view with the gradient, then cutting out the shapes by making a mask out of a UIBezierPath, then adding that to the view on top (let's call it topView):

        let yourPath: UIBezierPath = //create the desired bezier path for your shapes
        let mask = CAShapeLayer()
        mask.path = yourPath.cgPath
        topView.layer.mask = mask
    

提交回复
热议问题