【问题标题】:Shadow at the top of a Cell UITableCellView单元格 UITableCellView 顶部的阴影
【发布时间】:2011-12-29 23:43:50
【问题描述】:

我想知道是否有人知道如何根据屏幕截图获得类似事物中的阴影效果。在我看来,阴影效果是通过在上方单元格底部和下方单元格顶部创建阴影来创建的。

我可以使用下面的代码从上面的单元格创建一个,但无法弄清楚如何在下面的单元格上执行它,因为它没有显示出来,就好像单元格以相反的顺序呈现z 索引。

self.layer.shadowOffset = CGSizeMake(0, 2);
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowRadius = 2;
self.layer.shadowOpacity = 0.5;

CGFloat top = self.layer.bounds.origin.y;
CGFloat left = self.layer.bounds.origin.x;
CGFloat width = self.layer.bounds.size.width;

CGRect shadowFrame = CGRectMake(left, top, width, 44);
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;
self.layer.shadowPath = shadowPath;

非常感谢任何建议。

Things http://www.purplesmash.com/images/Things.jpg

【问题讨论】:

    标签: iphone uitableview shadow


    【解决方案1】:

    只需在使用 CoreGraphics 绘图时创建空心矩形(将空心部分与单元格可见视图完全对齐)并为上下文设置阴影 - 这将为您节省性能。

    更新:
    我已经在我的单元格中尝试过这个drawRect: 方法

    // Creating path which will hold our hollow rectangle
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(-8.0f, -8.0f, 336.0f, 96.0f));
    CGPathAddRect(path, NULL, CGRectMake(-5.0f, 0.0f, 330.0f, 80.0f));
    
    // Saving current graphics context state
    CGContextSaveGState(context);
    
    // Configuring shadow
    CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 0.0f), 6.0f, 
                                [[UIColor blackColor] CGColor]);
    
    // Adding our path
    CGContextAddPath(context, path);  
    
    // Configure hollow rectangle fill color
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    
    // Fill rectangle and keep hollow part transparent
    CGContextEOFillPath(context);
    
    // Restore graphics context
    CGContextRestoreGState(context);
    

    当然,额外的改进将是在初始化的某处或在此方法中通过条件if(path == NULL) 预先计算CGMutablePathRef path。颜色也可以保留。如果这应该是单元格更新的最后一行,那么您可能不需要保存上下文状态。

    【讨论】:

    • 您能否举个例子,从性能的角度来看,我的方法似乎运行良好,但如果您能详细说明并提供一个快速示例,那就太好了。
    • 非常感谢。稍后我会在我的开发 PC 上尝试并更新。
    • 我想我对这种方法的唯一担心是我不确定是否可以在绘制单元格后动态更改其外观。在我的应用程序中,如果用户点击最左边的按钮,则会应用阴影。如果他们再次点击它,它就会被删除。我不必重新加载它只作用于单元格的表格。我想我需要再玩一会。
    • 当用户点击单元格时,你可以只重绘它就可以了。
    • 请参阅上面的编辑了解我如何实施您的建议,非常感谢它和我以前的代码一样有效,但希望更有效。
    【解决方案2】:

    您可能需要通过将阴影绘制为选中单元格的一部分来完成此操作,而不是在周围单元格上设置阴影属性。

    我会通过添加一两个 CAGradientLayers 作为选中单元格的 contentView.layer 的子级来做到这一点。

    【讨论】:

    • 非常感谢您的建议,您有任何示例代码可以指导我吗?
    • 好的,我已经完成并让它工作了。稍后我会发布代码:)
    【解决方案3】:

    我根据月光的建议编辑了这个。当单元格需要调用该方法时,我将该代码放入方法 addInnerTopShadow 中。我添加了另一个方法 addInnerBottomShadow,它在底部放置阴影。

    我在 drawRect 方法中调用这些方法,当我想在状态之间切换时,我调用该方法:

    [cell setNeedsDisplay];
    

    这允许我切换单元格的状态而无需执行类似的操作

    [self.tableView reloadData];
    

    正如方法所建议的那样,我有外部阴影方法,可以在第一个和最后一个单元格上放置阴影,但这是另一个问题,已经回答了很多次。我现在的应用程序完全按照我的意愿运行。感谢 Rob 和 Moonlight。

    -(void)addInnerBottomShadow
    {   
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        // Creating path which will hold our hollow rectangle
        CGMutablePathRef path = CGPathCreateMutable();
    
        CGPathAddRect(path, NULL, CGRectMake(0, 44, 480, 80));
        CGPathAddRect(path, NULL, CGRectMake(0, 54, 480, 96));
    
        // Saving current graphics context state
        CGContextSaveGState(context);
    
        // Configuring shadow
        CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 0.0f), 6.0f, [[UIColor blackColor] CGColor]);
    
        // Adding our path
        CGContextAddPath(context, path);  
    
        // Configure hollow rectangle fill color
        CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    
        // Fill rectangle and keep hollow part transparent
        CGContextEOFillPath(context);
        CGContextClipToRect(context, self.bounds);
    
        // Restore graphics context
        CGContextRestoreGState(context);
    }
    
    -(void)addInnerTopShadow
    {
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddRect(path, NULL, CGRectMake(-8.0f, -8.0f, 336.0f, 96.0f));
        CGPathAddRect(path, NULL, CGRectMake(-5.0f, 0.0f, 330.0f, 80.0f));
    
        // Saving current graphics context state
        CGContextSaveGState(context);
    
        // Configuring shadow
        CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 0.0f), 7.0f, [[UIColor blackColor] CGColor]);
    
        // Adding our path
        CGContextAddPath(context, path);  
    
        // Configure hollow rectangle fill color
        CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    
        // Fill rectangle and keep hollow part transparent
        CGContextEOFillPath(context);
        CGContextClipToRect(context, self.bounds);
    
        // Restore graphics context
        CGContextRestoreGState(context);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多