Swift-设置tableView每个分区cell圆角

更新时间:2023-06-08 22:43:30 阅读: 评论:0

Swift-设置tableView每个分区cell圆⾓
1.// 重新绘制cell边框
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {            let cornerRadius: CGFloat = 10
cell.backgroundColor = UIColor.clearColor()
let layer = CAShapeLayer()
let pathRef = CGPathCreateMutable()
let bounds = CGRectInt(cell.bounds, 10, 0)
var addLine = fal
w == 0 && w == tableView.ion)-1 {
CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius)
} el w == 0 {
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds))
项羽与刘邦CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds),
CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius)
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds),
CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius)
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))
addLine = true
} el w == tableView.ion)-1 {
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds))
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds),
CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius)
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds),
CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius)
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds))
} el {
CGPathAddRect(pathRef, nil, bounds)
addLine = true
}
layer.path = pathRef
//颜⾊修改
layer.fillColor = UIColor.init(white: 1, alpha: 0.5).CGColor
layer.strokeColor = UIColor.lightGrayColor().CGColor
一口咬定打一个字
if addLine == true {
let lineLayer = CALayer()
let lineHeight = (1 / UIScreen.mainScreen().scale)
lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight,
bounds.size.width-10, lineHeight)
lineLayer.backgroundColor = tableView.paratorColor?.CGColor
layer.addSublayer(lineLayer)
}
let testView = UIView(frame: bounds)
testView.layer.inrtSublayer(layer, atIndex: 0)
风吹过山岗
testView.backgroundColor = UIColor.clearColor()
cell.backgroundView = testView
}
2.// 设置tableView每个分区cell圆⾓
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {        // 圆⾓弧度半径
let cornerRadius: CGFloat = 6
// 设置cell的背景⾊为透明,如果不设置这个的话,则原来的背景⾊不会被覆盖
cell.backgroundColor = UIColor.clearColor()
丁亚// 创建⼀个shapeLayer
let layer = CAShapeLayer()
let backgroundLayer = CAShapeLayer() //显⽰选中
// 创建⼀个可变的图像Path句柄,该路径⽤于保存绘图信息
let pathRef = CGPathCreateMutable()
// 获取cell的size
// 第⼀个参数,是整个 cell 的 bounds, 第⼆个参数是距左右两端的距离,第三个参数是距上下两端的距离
let bounds = CGRectInt(cell.bounds, 10, 0)
// CGRectGetMinY:返回对象顶点坐标
// CGRectGetMaxY:返回对象底点坐标
// CGRectGetMinX:返回对象左边缘坐标
// CGRectGetMaxX:返回对象右边缘坐标
// CGRectGetMidX: 返回对象中⼼点的X坐标
// CGRectGetMidY: 返回对象中⼼点的Y坐标
// 这⾥要判断分组列表中的第⼀⾏,每组ction的第⼀⾏,每组ction的中间⾏
// CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius)
w == 0 {
// 初始起点为cell的左下⾓坐标
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds))
// 起始坐标为左下⾓,设为p,(CGRectGetMinX(bounds), CGRectGetMinY(bounds))为左上⾓的点,设
为p1(x1,y1),(CGRectGetMidX(bounds), CGRectGetMinY(bounds))为顶部中点的点,设为p2(x2,y2)。然后连接p1和p2为⼀条直线l1,连接初始点p到p1成⼀条直线l,则在两条直线相交处绘制弧度为r的圆⾓。
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius)
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius)
// 终点坐标为右下⾓坐标点,把绘图信息都放到路径中去,根据这些路径就构成了⼀块区域了
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))
} el w == tableView.ion)-1 {
// 初始起点为cell的左上⾓坐标
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds))
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius)
儿童趣味数学CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius)
// 添加⼀条直线,终点坐标为右下⾓坐标点并放到路径中去
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds))
} el {
微信怎么解除黑名单// 添加cell的rectangle信息到path中(不包括圆⾓)
//假如⽤填充⾊,⽤这个
//        CGPathAddRect(pathRef, nil, bounds)
//假如只要边框
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds))
CGPathAddLineToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds))
CGPathMoveToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds))
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))
}
// 把已经绘制好的可变图像路径赋值给图层,然后图层根据这图像path进⾏图像渲染render        layer.path = pathRef
backgroundLayer.path = pathRef
《飘》
// 按照shape layer的path填充颜⾊,类似于渲染render
// layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor
layer.strokeColor = UIColor.blackColor().CGColor
layer.fillColor = UIColor.clearColor().CGColor
// view⼤⼩与cell⼀致
let roundView = UIView(frame: lf.view.bounds)
// 添加⾃定义圆⾓后的图层到roundView中
roundView.layer.inrtSublayer(layer, atIndex: 0)
男人和女人拍拍roundView.backgroundColor = UIColor.clearColor()
// cell的背景view
cell.backgroundView = roundView
/
/ 以上⽅法存在缺陷当点击cell时还是出现cell⽅形效果,因此还需要添加以下⽅法
// 如果你 cell 已经取消选中状态的话,那以下⽅法是不需要的.
let lectedBackgroundView = UIView(frame: lf.view.bounds)
backgroundLayer.fillColor = anColor().CGColor
lectedBackgroundView.layer.inrtSublayer(backgroundLayer, atIndex: 0)
lectedBackgroundView.backgroundColor = UIColor.clearColor()
cell.lectedBackgroundView = lectedBackgroundView
// 在使⽤上⾯代码前需要把tableView默认的分割线设置为None
}

本文发布于:2023-06-08 22:43:30,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/906184.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:坐标   路径   图像   设置   返回   对象   直线   可变
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图