带阴影和角半径的UITableView(附带电影示例)

时间:2012-07-30 21:26:37

标签: ios uitableview calayer

我有一个带UITableView的UIViewController。它位于视图控制器的视图内,四周都有一些填充。我试图从桌面视图和圆角绘制阴影,但我无法同时实现这两个。我已尝试使用以下代码并打开和关闭masksToBounds,但将其设置为NO会创建一个非常奇怪的效果,使用阴影滚动并且调用不会在表内进行剪切。

[self.tableView.layer setShadowColor:[UIColor blackColor].CGColor];
[self.tableView.layer setShadowOffset:CGSizeMake(0, 3)];
[self.tableView.layer setShadowOpacity:0.4];
[self.tableView.layer setShadowRadius:3.0f];
[self.tableView.layer setCornerRadius:5.0f];
[self.tableView.layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.tableView.bounds cornerRadius:5.0f] CGPath]];

我只是用简单的风格绘制UITableView。我试图实现的效果可以在免费的应用程序Transit App中看到,这里是movie,在那里你可以看到阴影停留,桌子甚至有一个上下滚动的面具。

我进行了搜索和搜索,但未能根据其他SO答案或在线文章整理解决方案。帮助赞赏。

6 个答案:

答案 0 :(得分:5)

尝试下面的链接,其中一些是旧的,但你可以得到一些指针。

Custom Table Effects

Dropshadow 2

Cool Table Views

DropShadow

好!!我再次尝试并获得了效果,它再次只是一个工作(而不是使用图像),下面是我试过的

使用与您的tableview相同的框架创建一个CALayer,给出相同的曲线和全部,在该图层的顶部添加表视图,就是这样。

CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [UIColor blueColor].CGColor; // If you dont give this, shadow will not come, dont know why
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 1.0;
sublayer.cornerRadius = 5.0;
sublayer.frame = CGRectMake(myTable.frame.origin.x, myTable.frame.origin.y, myTable.frame.size.width, myTable.frame.size.height);
[self.view.layer addSublayer:sublayer];

[self.view.layer addSublayer:myTable.layer];

阿!!由于某种原因,我无法从我的机器,防火墙上传截图;-)。我会在家里试试。

enter image description here

-anoop

答案 1 :(得分:0)

建议将图像用于具有大型tableviews的阴影,所有绘图都能够降低性能,这可能导致糟糕的用户体验,只需提出建议

答案 2 :(得分:0)

我知道这是一个相当古老的问题,但我最近遇到了同样的问题,这里的解决方案都是很好的例子,但还不够全面。所以这里是我用来创建表视图阴影的方法。它也可以用于任何其他视图。

-(void)makeShadowToView:(UIView*) view withHeight:(CGFloat)shadowHeight
{
    CGRect bounds = view.frame;
    bounds.size.height = shadowHeight;
    view.clipsToBounds = NO; // Without this row it doesn't display any shadow, at least for a table view
    view.layer.shadowColor = [UIColor blackColor].CGColor;
    view.layer.shadowOpacity = 0.9f;
    view.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    view.layer.shadowRadius = 5.0f;
    view.layer.shadowPath = [UIBezierPath bezierPathWithRect:bounds].CGPath;
}

示例用法:[self makeShadowToView:self.view withHeight:height]; 如果要根据表格内容的高度重新计算阴影的高度,则添加height

答案 3 :(得分:0)

试试这个

UITableView * objTableview=[[UITableView alloc]initWithFrame:CGRectMake(x, y, w, h)];

UIView * objViewShadow=[[UIView alloc]initWithFrame:objTableview.bounds];

objTableview.layer.cornerRadius=5;

// add shadow

objViewShadow.layer.shadowOffset = CGSizeMake(0, 3);

objViewShadow.layer.shadowRadius = 5.0;

objViewShadow.layer.shadowColor = [UIColor blackColor].CGColor;

objViewShadow.layer.shadowOpacity = 0.8;

[objViewShadow addSubview:objTableview];

[self.view addSubview:objViewShadow];

答案 4 :(得分:0)

(void)shadowForView{
CALayer *layer = self.viewActionMenu.layer;
layer.shadowOffset = CGSizeMake(-2, 0);
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.shadowRadius = 2.0f;
layer.shadowOpacity = 0.80f;
// [self.view bringSubviewToFront:self.actionView];
                   }

并致电:

[self shadowForView];

答案 5 :(得分:0)

您需要设置这两个属性

self.tableView.clipsToBounds = NO;

self.tableView.layer.masksToBounds = NO;