24

在我的应用程序中,我使用 aCAGradientLayer来设置单元格的背景,以这种方式:

retValue = [tableView dequeueReusableCellWithIdentifier:@"Cells" forIndexPath:indexPath];
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor blueColor];
bgColorView.layer.masksToBounds = YES;
id startColor = (id)[[UIColor colorWithWhite:0.75 alpha:1] CGColor];
id endColor = (id)[[UIColor blueColor] CGColor];
CAGradientLayer* gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = retValue.contentView.bounds;
gradientLayer.colors = @[startColor,startColor,endColor,endColor];
gradientLayer.locations = @[[NSNumber numberWithFloat:0],
    [NSNumber numberWithFloat:0.95],
    [NSNumber numberWithFloat:0.95],
    [NSNumber numberWithFloat:1]];
[gradientLayer setStartPoint:CGPointMake(0,0.5)];
[gradientLayer setEndPoint:CGPointMake(1,0.5)];
[bgColorView.layer addSublayer:gradientLayer];
retValue.selectedBackgroundView = bgColorView;

(这段代码在里面- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

它在 iOS 6 上工作正常,但在 iOS 7 上不起作用,在新的 iOS 中,渐变始终是垂直的(忽略 startPoint 和 endPoint)

有没有人遇到过同样的问题?

谢谢,

佩里

4

3 回答 3

43

这个问题相当古老,但我遇到了它,所以其他人也可能会。以下代码将生成在 7.0.3 版本的 iPhone 模拟器上运行的水平渐变

+ (void)drawGradientOverContainer:(UIView *)container
{
    UIColor *transBgColor = [UIColor colorWithWhite:1.0 alpha:0.0];
    UIColor *black = [UIColor blackColor];
    CAGradientLayer *maskLayer = [CAGradientLayer layer];
    maskLayer.opacity = 0.8;
    maskLayer.colors = [NSArray arrayWithObjects:(id)black.CGColor, 
    (id)transBgColor.CGColor, (id)transBgColor.CGColor, (id)black.CGColor, nil];

    // Hoizontal - commenting these two lines will make the gradient veritcal
    maskLayer.startPoint = CGPointMake(0.0, 0.5);
    maskLayer.endPoint = CGPointMake(1.0, 0.5);

    NSNumber *gradTopStart = [NSNumber numberWithFloat:0.0];
    NSNumber *gradTopEnd = [NSNumber numberWithFloat:0.4];
    NSNumber *gradBottomStart = [NSNumber numberWithFloat:0.6];
    NSNumber *gradBottomEnd = [NSNumber numberWithFloat:1.0];
    maskLayer.locations = @[gradTopStart, gradTopEnd, gradBottomStart, gradBottomEnd];

    maskLayer.bounds = container.bounds;
    maskLayer.anchorPoint = CGPointZero;
    [container.layer addSublayer:maskLayer];
}

我不确定为什么您的代码不起作用,但如果我不设置锚点,我会得到奇怪的行为 - 虽然渐变仍然是水平的。也许它与作为单元格背景视图有关 - 您可以尝试将渐变应用到基础表。

于 2014-03-09T21:34:28.000 回答
12

Swift 4中,我实现了一个很好的工作扩展:

extension UIView {

    enum GradientColorDirection {
        case vertical
        case horizontal
    }

    func showGradientColors(_ colors: [UIColor], opacity: Float = 1, direction: GradientColorDirection = .vertical) {
        let gradientLayer = CAGradientLayer()
        gradientLayer.opacity = opacity
        gradientLayer.colors = colors.map { $0.cgColor }

        if case .horizontal = direction {
            gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
            gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)
        }

        gradientLayer.bounds = self.bounds
        gradientLayer.anchorPoint = CGPoint.zero
        self.layer.addSublayer(gradientLayer)
    }

}
于 2018-04-23T10:59:26.450 回答
4

3年后,现在才发现。感谢 Gord 提供的解决方案。这是在 Swift 3.0 中:

func drawGradientOver(container: UIView) {
    let transBgColor = UIColor.clear
    let black = UIColor.black
    let maskLayer = CAGradientLayer()
    maskLayer.opacity = 0.8
    maskLayer.colors = [black.cgColor, transBgColor.cgColor, transBgColor.cgColor, black.cgColor]

    // Hoizontal - commenting these two lines will make the gradient veritcal
    maskLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
    maskLayer.endPoint = CGPoint(x: 1.0, y: 0.5)

    let gradTopStart = NSNumber(value: 0.0)
    let gradTopEnd = NSNumber(value: 0.4)
    let gradBottomStart = NSNumber(value: 0.6)
    let gradBottomEnd = NSNumber(value: 1.0)
    maskLayer.locations = [gradTopStart, gradTopEnd, gradBottomStart, gradBottomEnd]

    maskLayer.bounds = container.bounds
    maskLayer.anchorPoint = CGPoint.zero
    container.layer.addSublayer(maskLayer)
}
于 2017-03-10T21:23:56.667 回答