0

下一个代码在 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;

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

4

1 回答 1

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];
}
于 2013-10-15T17:03:26.157 回答