CAGradientLayer提供EXC_BAD_ACCESS?

时间:2013-04-25 22:53:19

标签: ios exc-bad-access cagradientlayer

我正试图让我的UIScrollView的结尾淡出 - 就像UITableView一样。我找到了下面的代码。我在一个名为initWithFrame:...内部的方法中的UIScrollView子类中使用它。

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

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

    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.2],
                           [NSNumber numberWithFloat:0.8],
                           [NSNumber numberWithFloat:1.0], nil];

    maskLayer.bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    maskLayer.anchorPoint = CGPointZero;
    CGColorRelease(outerColor);
    CGColorRelease(innerColor);
    self.layer.mask = maskLayer;
}

注意:我已经看过this question,但我的颜色之前已经有__bridge

我确信这是非常基本的东西,但我无法理解。我添加了CGColorRelease行,希望可以修复它,但它没有......

有没有人看到我没有看到的东西?

1 个答案:

答案 0 :(得分:1)

假设您要在XIB中设置scrollview,请删除if语句并将代码放入子类中的awakeFromNib中。如果手动创建滚动视图对象,请在执行inithWithFrame后调用该代码。

另外,翻转outerColor和innerColor变量的alpha值。这是代码。

- (void) setGradient
{
    CAGradientLayer * maskLayer = [CAGradientLayer layer];

    // Flipped your alpha values around.
    CGColorRef outerColor = [UIColor colorWithWhite:0 alpha:0.0].CGColor;
    CGColorRef innerColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;

    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.2],
                           [NSNumber numberWithFloat:0.8],
                           [NSNumber numberWithFloat:1.0], nil];

    maskLayer.bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    maskLayer.anchorPoint = CGPointZero;
    CGColorRelease(outerColor);
    CGColorRelease(innerColor);
    self.layer.mask = maskLayer;

}
- (void)awakeFromNib
{
    [self setGradient];
}