有个需求,视图的右边需要渐隐,如下图:(这里只是随便拿个界面做下测试)
要求整个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;
}
搞定!