UITableView section 圆角 阴影
在UITableView实现图片上面的效果,百度一下看了别人的实现方案有下面2种:
1.UITableView section里面嵌套UITableView然后在上面实现圆角和阴影, 弊端代码超多我看了下就不想看了立马放弃.
2.UICollectionView 实现, 但是我原来的UI是UITableView写的就懒得重写.
找来找去都没一种简单的实现方案,自己有事了几个绘图API,没有达到图片的效果.想了两天灵光一闪,一个超简单的方法就能实现section+圆角+阴影 . 分享出来给有需要的人
UITableViewCell 是系统的没有自定义,直接在上面插入一个UIView做阴影图层就能达到效果,原理就是利用clipsToBounds属性UIView的layer图层超出view的部分不显示
贴代码
//============阴影层=========== @interface SubCellShadowView :UIView @property (nonatomic, strong) CAShapeLayer *shadowLayer; @property (nonatomic, strong) CALayer *separatorLine; @end @implementation SubCellShadowView @end
//===============Cell==================
@interface SubCell : UITableViewCell
@property (nonatomic, strong) SubCellShadowView *bgView;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic) NSInteger rowInSection;//每一组的行数
@end
@implementation SubCell
- (void)awakeFromNib {
[super awakeFromNib];
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.clipsToBounds = NO;
SubCellShadowView *bgView = [[SubCellShadowView alloc] init];
[self insertSubview:bgView atIndex:];
self.bgView= bgView;
CAShapeLayer *shadow = [CAShapeLayer layer];
shadow.shadowColor = [UIColor blackColor].CGColor;
shadow.shadowOffset=CGSizeMake(,);
shadow.shadowOpacity=0.15;
[bgView.layeraddSublayer:shadow];
bgView.shadowLayer= shadow;
CALayer*line = [CALayerlayer];
line.backgroundColor = [UIColor groupTableViewBackgroundColor].CGColor;
[bgView.layeraddSublayer:line];
bgView.separatorLine= line;
}
-(void)layoutSubviews{
[super layoutSubviews];
UIBezierPath*bgBezierPath =nil;
CGFloat cornerRaduis =7.0;//觉得阴影大的可以把半径调小,半径大的话阴影面积会变大
if(self.indexPath.row== && self.rowInSection==) {//单组单行
self.bgView.clipsToBounds=NO;
self.bgView.frame=self.bounds;
CGRect rect = UIEdgeInsetsInsetRect(self.bgView.bounds, UIEdgeInsetsMake(, , , ));
bgBezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRaduis, cornerRaduis)];
}elseif(self.indexPath.row==) {// 第一行
self.bgView.clipsToBounds=YES;
self.bgView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(-, , , ));
CGRect rect = UIEdgeInsetsInsetRect(self.bgView.bounds, UIEdgeInsetsMake(, , , ));
bgBezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(cornerRaduis, cornerRaduis)];
}elseif(self.indexPath.row==self.rowInSection-) {// 最后一行
self.bgView.clipsToBounds=YES;
self.bgView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(, , -, ));
CGRect rect = UIEdgeInsetsInsetRect(self.bgView.bounds, UIEdgeInsetsMake(, , , ));
bgBezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(cornerRaduis, cornerRaduis)];
}else{// 中间行
self.bgView.clipsToBounds=YES;
self.bgView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(, , , ));
CGRect rect = UIEdgeInsetsInsetRect(self.bgView.bounds, UIEdgeInsetsMake(, , , ));
bgBezierPath = [UIBezierPathbezierPathWithRect:rect];
}
self.bgView.shadowLayer.path= bgBezierPath.CGPath;
self.bgView.shadowLayer.shadowPath= bgBezierPath.CGPath;
self.bgView.shadowLayer.fillColor = [UIColor whiteColor].CGColor;
//分割线 非单组单行 非最后一行
if(!(self.indexPath.row==&&self.rowInSection==) && !(self.indexPath.row==self.rowInSection-)) {
self.bgView.separatorLine.frame = CGRectMake(self.bgView.frame.origin.x+, self.bgView.frame.size.height-, self.bgView.frame.size.width-*, 1.0);
}
}
@end
UITableView section 圆角 阴影的更多相关文章
- IOS UIView圆角,阴影,边框,渐增光泽
圆角 sampleView.layer.cornerRadius = 2.5; // 圓角的弧度sampleView.layer.masksToBounds = YES; 阴影 sampleView. ...
- UWP Button添加圆角阴影(三)
原文:UWP Button添加圆角阴影(三) Composition DropShadow是CompositionAPI中的东西,使用Storyboard设置某个属性,就是频繁的触发put_xxx() ...
- UWP Button添加圆角阴影(二)
原文:UWP Button添加圆角阴影(二) 阴影 对于阴影呢,WindowsCommunityToolkit中已经有封装好的DropShadowPanel啦,只要引用Microsoft.Toolki ...
- UWP Button添加圆角阴影(一)
原文:UWP Button添加圆角阴影(一) 众所周知,17763之前的UWP控件,大部分是没有圆角属性的:而阴影也只有17763中的ThemeShadow可以直接在xaml中使用,之前的版本只能用D ...
- CSS3圆角,阴影,透明
CSS实现圆角,阴影,透明的方法很多,传统的方法都比较复杂,用CSS3就方便很多了,虽然现在各浏览器对CSS3的支持还不是很好,但不久的将来CSS3就会普及. 1.圆角 CSS3实现圆角有两种方法. ...
- Qt之圆角阴影边框
Qt的主窗体要做出类似WIN7那种圆角阴影边框,这一直是美工的需求. 这里是有一些门道的,尤其是,这里藏着一个很大的秘密. 这个秘密是一个QT的至少横跨3个版本,存在了2年多的BUG... https ...
- 让group tableview每个section拥有阴影和圆角
#import <UIKit/UIKit.h> @class GroupShadowTableView; @protocol GroupShadowTableViewDelegate &l ...
- iOS tableView Section圆角方案
给tableView的section设置圆角 首先给让cell左右偏移一点的距离,通过重写cell的setframe方法来实现 -(void)setFrame:(CGRect)frame{ CGFlo ...
- 设置UITableView section间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0 ...
随机推荐
- js斐波那契数列
斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...... 这个数列从第3项开始,每一项都等于前两项之和. 1.递归算法: function ...
- spring配置JNDI(Java Naming and Directory Interface,Java命名和目录接口)数据源
1.在tomcat下的server.xml的 <GlobalNamingResources> </GlobalNamingResources>添加下面代码 <Resour ...
- js点击加载更多可以增加几条数据的显示
<div class="list"> <div class="one"> <div class="img" ...
- 《hello--world团队》第一次作业:团队亮相
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验五 团队作业:软件研发团队组建 团队名称 <hello--world团队> ...
- angular6 http.service.ts
import { Injectable, isDevMode } from '@angular/core'; import { HttpClient, HttpParams, HttpHeaders ...
- 与WCAG相关的一些学习心得
1.什么是 WCAG? WCAG全称Web Content Accessibility Guidelines 网页内容无障碍浏览准则,简单的说就是为了方便残障人士(包括低视患者,盲人,聋人,学习障碍, ...
- HTTP Basic和Digest认证介绍与计算
一.说明 web用户认证,最开始是get提交+把用户名密码存放在客户端的cookie中的形式:在意识到这样不安全之后逐渐演变成了post提交+把用户凭证放到了服务端的session中的形式(当然ses ...
- java对象深度拷贝
如何利用序列化来完成对象的拷贝呢?在内存中通过字节流的拷贝是比较容易实现的.把母对象写入到一个字节流中,再从字节流中将其读出来,这样就可以创建一个新的对象了,并且该新对象与母对象之间并不存在引用共享的 ...
- srping的历史与哲学
历史: 要谈Spring的历史,就要先谈J2EE.J2EE应用程序的广泛实现是在1999年和2000年开始的,它的出现带来了诸如事务管理之类的核心中间层概念的标准化,但是在实践中并没有获得绝对的成功, ...
- 颠覆区块链的钱包——AngelToken钱包
币圈的玩家/大佬们的刚需产品Angel token钱包您有吗? 当前由法国凡赛公司技术团队研发的去中心化AngelToken智能钱包,它兼容比特币底层技术系列币种,同时也兼容以太坊底层技术系列币 ...