UITableView/UICollectionView section設(shè)置圓角 cell上控件設(shè)置圓角 UIView設(shè)置某個(gè)圓角

自定義UITableViewCell 上面的控件設(shè)置圓角 自定義cell的drawRect 方法,并在該方法里面設(shè)置圓角

-(void)drawRect:(CGRect)rect {

  [super drawRect:rect];

  self.avada.layer.cornerRadius = self.avada.width * 0.5;
  self.avada.layer.borderColor = [UIColor whiteColor].CGColor;
  self.avada.layer.borderWidth = 2;
  self.avada.layer.masksToBounds =  YES;
}

??section分組設(shè)置圓角

 // 1:tableview 分組圓角

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(tintColor)]) {
    //        if (tableView == self.tableView) {
    CGFloat cornerRadius = 10.f;
    cell.backgroundColor = UIColor.clearColor;
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGRect bounds = CGRectInset(cell.bounds, 10, 0);
    BOOL addLine = NO;
    if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
        CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
    } else if (indexPath.row == 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 = YES;
        
    } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-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));
    } else {
        CGPathAddRect(pathRef, nil, bounds);
        addLine = YES;
    }
    layer.path = pathRef;
    CFRelease(pathRef);
    //顏色修改
    layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.5f].CGColor;
    layer.strokeColor=[UIColor blackColor].CGColor;
    if (addLine == YES) {
        CALayer *lineLayer = [[CALayer alloc] init];
        CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
        lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);
        lineLayer.backgroundColor = tableView.separatorColor.CGColor;
        [layer addSublayer:lineLayer];
    }
    UIView *testView = [[UIView alloc] initWithFrame:bounds];
    [testView.layer insertSublayer:layer atIndex:0];
    testView.backgroundColor = UIColor.clearColor;
    cell.backgroundView = testView;
}
 //    }
}

2:collection 設(shè)置分組圓角

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{

 if ([cell respondsToSelector:@selector(tintColor)]) {
    //        if (tableView == self.tableView) {
    CGFloat cornerRadius = 6.f;
    cell.backgroundColor = UIColor.clearColor;
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGRect bounds = CGRectInset(cell.bounds, 10, 0);
    BOOL addLine = NO;

    //只設(shè)置最后一行圓角(如需要設(shè)置整個(gè)section為圓角,參考tableview Section 設(shè)置)
    if (indexPath.row == [collectionView numberOfItemsInSection:indexPath.section]-1 || (indexPath.row == 0 && indexPath.row == [collectionView numberOfItemsInSection:indexPath.section]-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));
    } else {//其他行為舉行
        CGPathAddRect(pathRef, nil, bounds);
        addLine = YES;
    }
    
    layer.path = pathRef;
    CFRelease(pathRef);
    
    //顏色修改
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.strokeColor=[UIColor clearColor].CGColor;
    
    if (addLine == YES) {
        
        CALayer *lineLayer = [[CALayer alloc] init];
        CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
        lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);
        lineLayer.backgroundColor = BackgroundLightGray.CGColor;
        
        [layer addSublayer:lineLayer];
    }
    UIView *testView = [[UIView alloc] initWithFrame:bounds];
    [testView.layer insertSublayer:layer atIndex:0];
    testView.backgroundColor = BackGround_COLOR;
    cell.backgroundView = testView;
  }
}

??UIView設(shè)置某個(gè)圓角:以設(shè)置上面兩個(gè)為圓角例

//設(shè)置上面兩個(gè)為圓角
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: _contentView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(6,6)];
//創(chuàng)建 layer
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = _contentView.bounds;
//賦值
maskLayer.path = maskPath.CGPath;
_contentView.layer.mask = maskLayer;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,606評(píng)論 4 61
  • *面試心聲:其實(shí)這些題本人都沒(méi)怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來(lái)就是把...
    Dove_iOS閱讀 27,653評(píng)論 30 472
  • 轉(zhuǎn)載自:https://github.com/Tim9Liu9/TimLiu-iOS 目錄 UI下拉刷新模糊效果A...
    袁俊亮技術(shù)博客閱讀 12,147評(píng)論 9 105
  • 說(shuō)好的今天有客人來(lái)吃飯,結(jié)果卻又說(shuō)明天才能來(lái),公子仲還買(mǎi)了許多菜呢…… 不管他們,今天先做點(diǎn)嘗嘗——雞肉、火腿腸、...
    唐仲仁閱讀 232評(píng)論 0 0
  • 今天要寫(xiě)的內(nèi)容是今天聽(tīng)到的一本音頻書(shū),就是得到每天聽(tīng)本書(shū)的音頻,書(shū)的名字是《重塑心靈》,當(dāng)然是關(guān)于心理學(xué)的書(shū)籍。之...
    踏上筆尖閱讀 243評(píng)論 0 0

友情鏈接更多精彩內(nèi)容