在iOS7中,layer.mask渐变为Solid

时间:2013-10-15 15:18:06

标签: ios ios7 mask cagradientlayer

下一个代码在ios6中正常运行。它应该将顶部和底部内部alpha渐变应用于视图:

CAGradientLayer * gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor],
                   (id)[[UIColor whiteColor]CGColor],
                   (id)[[UIColor whiteColor]CGColor],
                   (id)[[UIColor clearColor] CGColor],
                   nil];

gradient.startPoint = CGPointMake(0.5, 0.0);
gradient.endPoint = CGPointMake(0.5, 1.0);
gradient.locations = [NSArray arrayWithObjects:@0,@(val),@(1.-val),@1,nil];

self.layer.mask = gradient;

BUT!如果我在iOS7中运行此代码,而不是一个很好的混合alpha渐变,渐变的“透明”部分是纯白色。

1 个答案:

答案 0 :(得分:2)

我会将其放在评论中,但格式化将会关闭。这段代码在iOS 7中适用于我。您可以根据自己的需要进行调整。如果它还没有工作,那么我会说渐变不是问题。

+(void)addScrollingGradientToView:(UIView*)view
{
    //add in the gradient to show scrolling
    CAGradientLayer *nextImageFade = [CAGradientLayer layer];
    nextImageFade.frame = CGRectInset(view.bounds,-10,-10);

    nextImageFade.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:0. green:0. blue:0. alpha:0.].CGColor,
                            [UIColor colorWithRed:0. green:0. blue:0. alpha:1.0].CGColor,
                            [UIColor colorWithRed:0. green:0. blue:0. alpha:1.0].CGColor,
                            [UIColor colorWithRed:0. green:0. blue:0. alpha:0.0].CGColor,nil];
    nextImageFade.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],
                               [NSNumber numberWithFloat:20 / view.frame.size.height],
                               [NSNumber numberWithFloat:(view.frame.size.height - 20) / view.frame.size.height],
                               [NSNumber numberWithFloat:1.], nil];
    nextImageFade.startPoint = CGPointMake(.5, 0);
    nextImageFade.endPoint = CGPointMake(.5, 1);

    //Put in the fading last so that it is above everything else
    [view.layer setMask:nextImageFade];
}