【问题标题】:Gradient effect to left half portion of UITableViewCell in iOSiOS中UITableViewCell左半部分的渐变效果
【发布时间】:2013-05-23 08:16:02
【问题描述】:

是否可以在iOS中只制作一半或一半的UITableViewCell渐变?

我正在使用以下代码。

-(void) addGradient:(UITableViewCell *) _button {
    //  [self removeGradient:_button];
    // Add Border
    CALayer *layer = _cell.layer;
    layer.cornerRadius = 4.0f;
    layer.masksToBounds = YES;
    layer.borderWidth = 1.0f;
    layer.borderColor = [UIColor colorWithWhite:0.5f alpha:0.2f].CGColor;

    // Add Shine
    CAGradientLayer *shineLayer = [CAGradientLayer layer];
    shineLayer.frame = layer.bounds;
    shineLayer.colors = [NSArray arrayWithObjects:
                         (id)[UIColor colorWithWhite:1.0f alpha:0.0f].CGColor,
                         (id)[UIColor colorWithWhite:1.0f alpha:0.0f].CGColor,
                         (id)[UIColor colorWithWhite:0.75f alpha:0.0f].CGColor,
                         (id)[UIColor colorWithWhite:0.4f alpha:0.0f].CGColor,
                         (id)[UIColor colorWithWhite:1.0f alpha:1.0f].CGColor,
                         nil];
    shineLayer.locations = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat:0.0f],
                            [NSNumber numberWithFloat:0.5f],
                            [NSNumber numberWithFloat:0.5f],
                            [NSNumber numberWithFloat:0.8f],
                            [NSNumber numberWithFloat:1.0f],
                            nil];
    [layer addSublayer:shineLayer];

}

我在 cellForRowAtIndexPath 方法中调用此方法。在这里,我将渐变效果完全设置为单元格的底部。但我只需要具有渐变效果的 UITableViewCell 的左半部分。有可能吗?

谢谢

【问题讨论】:

  • 设置另一个更小的框架有什么问题?

标签: iphone ios uitableview gradient


【解决方案1】:

创建另一个 UIView,其框架占据 UITableViewCell 的一半,使用您拥有的 sn-p 将渐变设置为该视图。

将 UIView 作为子视图添加到 UITableViewCell。

//Considering cell is the tableviewcell
UIView *leftHalf = [[UIView alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width/2,cell.frame.size.height)];
[self addGradient:leftHalf]; //Assuming the method can take UIView as param.
[cell addSubView: leftHalf];

【讨论】:

    猜你喜欢
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 2016-11-18
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    相关资源
    最近更新 更多