1

我正在尝试在 scrollView 的两端添加渐变渐变,这在模拟器中效果很好,但是当我在 Device 上运行它时,它给了我糟糕的访问错误。

这是我从这个链接获得的在 scrollView 中添加渐变渐变的代码。在第 13 行的 .m 文件中,它给出了错误

.h 文件

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface ViewController : UIViewController
{
    CAGradientLayer *maskLayer;
}

.m 文件

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (!maskLayer)
    {
        maskLayer = [CAGradientLayer layer];

        CGColorRef outerColor = [UIColor colorWithWhite:1.0 alpha:0.5].CGColor;
        CGColorRef innerColor = [UIColor colorWithWhite:1.0 alpha:0.0].CGColor;

       //here it gives bad access error
        maskLayer.colors = [NSArray arrayWithObjects:(__bridge id)(outerColor),(__bridge id)(innerColor),(__bridge id)(innerColor), (__bridge id)(outerColor), nil];


        maskLayer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.05],[NSNumber numberWithFloat:0.95],[NSNumber numberWithFloat:1.0], nil];

        maskLayer.bounds = CGRectMake(0, 0,scrollView.frame.size.width,scrollView.frame.size.height);

           maskLayer.frame = CGRectMake(0, scrollView.frame.origin.y,scrollView.frame.size.width,scrollView.frame.size.height);

        [self.view.layer addSublayer:maskLayer];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)ascrollView
{
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    maskLayer.position = CGPointMake(0, ascrollView.contentOffset.y);
    [CATransaction commit];
}
4

0 回答 0