有個需求蛇摸,視圖的右邊需要漸隱,如下圖:(這里只是隨便拿個界面做下測試)
要求整個view漸漸alpha變透明赶袄,包括view上的子view,需要一起變透明饿肺。
拿起鍵盤就是干:
-(void)testAlpha{
[self.view layoutIfNeeded];
UIColor *color1 = [UIColor colorWithRed:(0) green:(0) blue:(0) alpha:1.0];
UIColor *color2 = [UIColor colorWithRed:(0) green:(0) blue:(0) alpha:0.8];
UIColor *color3 = [UIColor colorWithRed:(0) green:(0) blue:(0) alpha:0.2];
NSArray *colors = [NSArray arrayWithObjects:(id)color1.CGColor, color2.CGColor,color3.CGColor, nil];
NSArray *locations = [NSArray arrayWithObjects:@(0.0), @(0.7),@(1.0), nil];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = colors;
gradientLayer.locations = locations;
gradientLayer.frame = _inputView.bounds;
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1, 0);
_inputView.layer.mask = gradientLayer;
}
搞定!