码迷,mamicode.com
首页 > 其他好文 > 详细

圆角的tableView

时间:2015-12-11 12:51:27      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

---恢复内容开始---

效果:

技术分享

实现原理,先画一个矩形,把这个矩形当作tableView的mask;代码:

//画一个圆角的矩形
- (void)drawRect:(CGRect)rect {
    //获取当前上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGFloat cornerRadius = 10.0;
    CGFloat minx = CGRectGetMinX(rect);
    CGFloat midx = CGRectGetMidX(rect);
    CGFloat maxx = CGRectGetMaxX(rect);
    CGFloat miny = CGRectGetMinY(rect);
    CGFloat midy = CGRectGetMidY(rect);
    CGFloat maxy = CGRectGetMaxY(rect);
    
    CGContextMoveToPoint(context, minx, midy);
    CGContextAddArcToPoint(context, minx, miny, midx, miny, cornerRadius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, cornerRadius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, cornerRadius);
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, cornerRadius);
    
    CGContextClosePath(context);
    CGContextFillPath(context);
}
- (void)setupView {
    mask = [[ConnerView alloc]initWithFrame:CGRectZero];
    self.layer.mask = mask.layer;
    self.layer.cornerRadius = 10.0;
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setupView];
    }
    return self;
}

- (void)adjustMask {
    CGRect frame = mask.frame;
    if (self.contentSize.height > self.frame.size.height) {
        frame.size = self.contentSize;
    }else {
        frame.size = self.frame.size;
    }
    mask.frame = frame;
    [mask setNeedsLayout];
    [self setNeedsLayout];
}

- (void)reloadData {
    [super reloadData];
    [self adjustMask];
}
#pragma mark delegata
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

 

圆角的tableView

标签:

原文地址:http://www.cnblogs.com/developzhou/p/5038368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!