如何添加遮罩层,以遮盖特定颜色

时间:2016-08-21 08:04:54

标签: ios uiview core-animation mask

我在scrollview中有一个UILabel。

我希望屏蔽部分滚动视图,以便当UILabel在该部分下方时,它将改变它的颜色。

无法找到怎么做。

1 个答案:

答案 0 :(得分:0)

试试这段代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
    maskView.backgroundColor = [UIColor greenColor];
    maskView.alpha = 0.5;
    self.maskView = maskView;
    [self.view addSubview:maskView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect rect = [self.label convertRect:CGRectMake(0, 0, 100, 100) toView:self.maskView];
    NSLog(@"%@", NSStringFromCGRect(rect));
    if (rect.origin.y >= self.maskView.frame.size.height || rect.origin.y <= -self.label.frame.size.height) {
        self.label.backgroundColor = [UIColor redColor];
    } else {
        self.label.backgroundColor = [UIColor orangeColor];
    }
}

关键是scrollViewDidScroll:convertRect: toView:

enter image description here

enter image description here