直接上代码
注意事项
*- (void)addMaskToMenuView:(UIView )menuView 中的menuview一定不能设置透明度为0
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor systemPinkColor];
UIView *bgcView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40)];
// bgcView.backgroundColor = UIColor.orangeColor;
[self.view addSubview:bgcView];
UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 60, 40)];
// titleScrollView.backgroundColor = UIColor.greenColor;
titleScrollView.showsHorizontalScrollIndicator = NO;
[bgcView addSubview:titleScrollView];
CGFloat buttonx = 0;
CGFloat lineWidth = 10;
for (NSInteger i = 0; i < 10; i++) {
UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(buttonx, 0, 50, 40)];
// titleButton.backgroundColor = UIColor.redColor;
[titleButton setTitle:@"123" forState:(UIControlStateNormal)];
[titleButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
[titleScrollView addSubview:titleButton];
buttonx = buttonx + lineWidth + 50;
}
titleScrollView.contentSize = CGSizeMake(50*10+9*10+30, 40);
[self addMaskToMenuView:bgcView];
UIView *aaView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 100, 60, 40)];
aaView.backgroundColor = UIColor.blueColor;
[self.view addSubview:aaView];
}
- (void)addMaskToMenuView:(UIView *)menuView {
NSObject *transparent = (NSObject *)[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0].CGColor;
NSObject *opaque = (NSObject *)[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0].CGColor;
CALayer *maskLayer = [CALayer layer];
maskLayer.frame = menuView.bounds;
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = maskLayer.bounds;
gradientLayer.colors = @[opaque, transparent];
gradientLayer.locations = @[@((menuView.frame.size.width - 110) / menuView.frame.size.width),@((menuView.frame.size.width - 60) / menuView.frame.size.width)];
gradientLayer.startPoint = CGPointMake(0, 0.5);
gradientLayer.endPoint = CGPointMake(1, 0.5);
[maskLayer addSublayer:gradientLayer];
menuView.layer.mask = maskLayer;
}
网友评论