Olg*_*ton 4 iphone cocoa-touch objective-c
任何想法如何在分组的uitableview的边框周围显示阴影?(在顶部,底部,边的所有边界周围以及部分的圆角周围).
我需要与图片完全相同的效果:
alt http://img824.imageshack.us/img824/2826/tableviewwithshadow.png
注意边框附近的小阴影(灰色一个,蓝色是背景)
Nen*_*d M 12
在我看来,我想出了一个相当"hackish"的解决方案,所以我对此并不完全满意,但无论如何!在一个部分中围绕一组单元格绘制阴影的基本功能由此完成.因此,我将UITableView子类化并实现了layoutSubviews方法,如下所示:
- (void) layoutSubviews {
[super layoutSubviews];
const CGFloat PageCellBackgroundRadius = 6.0;
for(int i = 0; i < [self numberOfSections]; i++) {
NSInteger viewTag = i + 123456;
CGRect frameRect = [self shadowFrameForSection: i];
UIView* shadowBackgroundView = [self viewWithTag: viewTag];
if (shadowBackgroundView) {
if (!CGRectEqualToRect(frameRect, shadowBackgroundView.frame)) {
shadowBackgroundView.frame = frameRect;
CGPathRef shadowPath = [UIBezierPath bezierPathWithRoundedRect: shadowBackgroundView.bounds
byRoundingCorners: UIRectCornerAllCorners
cornerRadii: CGSizeMake(PageCellBackgroundRadius, PageCellBackgroundRadius)].CGPath;
shadowBackgroundView.layer.shadowPath = shadowPath;
}
[self sendSubviewToBack: shadowBackgroundView];
} else {
shadowBackgroundView = [[[UIView alloc] initWithFrame: frameRect] autorelease];
shadowBackgroundView.tag = viewTag;
shadowBackgroundView.opaque = YES;
shadowBackgroundView.backgroundColor = [UIColor clearColor];
shadowBackgroundView.layer.shadowOpacity = 0.3;
shadowBackgroundView.layer.shadowRadius = 2;
shadowBackgroundView.layer.shadowColor = [[UIColor blackColor] CGColor];
shadowBackgroundView.layer.shadowOffset = CGSizeMake(0.0, 1.0);
CGPathRef shadowPath = [UIBezierPath bezierPathWithRoundedRect: shadowBackgroundView.bounds
byRoundingCorners: UIRectCornerAllCorners
cornerRadii: CGSizeMake(PageCellBackgroundRadius, PageCellBackgroundRadius)].CGPath;
shadowBackgroundView.layer.shadowPath = shadowPath;
shadowBackgroundView.layer.shouldRasterize = YES;
[self addSubview: shadowBackgroundView];
}
}
}
Run Code Online (Sandbox Code Playgroud)
和一个小帮手方法:
- (CGRect) shadowFrameForSection: (NSInteger) section {
CGRect sectionFrame = [self rectForSection: section];
CGFloat sectionHeaderHeight = CGRectGetHeight([self rectForHeaderInSection: section]);
CGFloat sectionFooterHeight = CGRectGetHeight([self rectForFooterInSection: section]);
UIEdgeInsets contentInsets = UIEdgeInsetsMake(sectionHeaderHeight + 1, 10, sectionFooterHeight + 1, 10);
return UIEdgeInsetsInsetRect(sectionFrame, contentInsets);
}
Run Code Online (Sandbox Code Playgroud)
我认为代码是不言自明的!基本上它围绕将UIViews添加到UITableView作为子视图,为一个部分的计算框架,然后将投影阴影绘制到添加的UIView的图层.UITableView为帧计算提供了一些方法:"rectForSection"等.
我对它不满意,因为在layoutSubviews方法中添加视图感觉不对!(这可以在其他地方完成=>表视图委托?)我也希望更多地使用CALayers,但你不能标记它们!因为我的代码通过缩小/扩展已添加的uiviews来实现性能原因.
在这种形式下,如果部分消失或部分被重新排序等,解决方案将无效.如果您以动画方式向表视图等添加行,它也表现不佳,但它对"静态"分组非常有用我认为表格视图!
我还没有测试性能影响,因此请自担风险使用!
所以也许这是一个很好的起点,可以更好地解决这个问题!:-)
| 归档时间: |
|
| 查看次数: |
7849 次 |
| 最近记录: |