為app實現漸變的遮罩效果

weixin_34007291發表於2016-04-07

效果圖如下

1714221-2b46e850b22bbe93.png
exampleImg1.png

實現程式碼如下

//blackView為你想加上遮罩的檢視


     CGColorRef opaqueBlackColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor;
    CGColorRef transparentBalckColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4].CGColor;
    
     //遮罩效果由CAGradientLayer實現
    CAGradientLayer * layer = [[CAGradientLayer alloc]init];
    layer.frame = CGRectMake(0, 0, WIDTH, self.blackView.bounds.size.height);
    
    //設定漸變的方向
    layer.startPoint = CGPointMake(0, 1);
    layer.endPoint = CGPointMake(0, 0);
    
    //設定漸變得顏色範圍
    layer.colors = @[(__bridge id)opaqueBlackColor,(__bridge id)transparentBalckColor];
    
    [self.blackView.layer insertSublayer:layer atIndex:0];

相關文章