通過此方法可以設置任何控件(UIView的子類)的任意一個方向的圓角
效果如圖:
上邊兩個角是圓角的輸入框
下邊兩個角是圓角的輸入框
當然,你也可以其他方法實現(xiàn),比如用一個四個角圓角的view作為背景,然后里面裝兩個透明的textfield,中間畫一條線,但是這種方法稍微有點耗內(nèi)存,定向圓角是直接對layer進行操作,更加輕量級,所以建議用定向圓角的方法
代碼:
如果使用系統(tǒng)的API是辦不到的,只能同時設置四個角的圓角,這里我講介紹一種辦法,可以設置定向圓角
使用方法:
//1.導入頭文件
#import "ZHRoundView.h"```
//2.讓你的view/label/button...繼承自ZHRoundView
//3.只需要一句代碼就可搞定
//設置左上角圓角
self.myView.roundType = ZHRoundTypeLeft;
//設置上面兩個角是圓角
//self.myView.roundType = ZHRoundTypeTop;
如果想繪制邊框
view.borderWidth = 2;
view.borderColor = [UIColor blackColor].CGColor;
view.cornerRadius = 10;
源碼:
頭文件:ZHRoundView.h
import <UIKit/UIKit.h>
typedef enum{
ZHRoundTypeTop = 0,
ZHRoundTypeLeft,
ZHRoundTypeRight,
ZHRoundTypeBottom,
ZHRoundTypeAll,
ZHRoundTypeNone
}ZHRoundType;
@interface ZHRoundView : UIView
/**
- 圓角類型
*/
@property(nonatomic,assign)ZHRoundType roundType;
/**
- 邊框?qū)挾?br>
*/
@property (nonatomic ,assign) CGFloat borderWidth;
/**
- 邊框顏色
/
@property (nonatomic ,assign) CGColorRef borderColor;
/* - 圓角的半徑
*/
@property (nonatomic ,assign) CGFloat cornerRadius;
@end
.m文件
ZHRoundView.m
//
// ZHRoundView.m
// 核心動畫
//
// Created by 演員新之助 on 15/12/29.
// Copyright ? 2015年 演員新之助. All rights reserved.
//
import "ZHRoundView.h"
@interface ZHRoundView()
@end
@implementation ZHRoundView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
[self setup];
return [super initWithFrame:frame];
}
-(void)awakeFromNib
{
[self setup];
}
/**
- 初始化
*/
-(void)setup{
//默認cornerRadius為10
_cornerRadius = 10;
}
/**
設置圓角方向(設置邊框顏色和寬度應放在此方法之前)
-
@param roundType 定向圓角的方向
*/
-(void)setRoundType:(ZHRoundType)roundType{_roundType = roundType;
self.layer.mask = nil;
UIRectCorner corners;
switch (roundType) {
case ZHRoundTypeLeft:
corners = UIRectCornerTopLeft;
break;
case ZHRoundTypeRight:
corners = UIRectCornerBottomRight | UIRectCornerTopRight;
break;
case ZHRoundTypeBottom:
corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
break;
case ZHRoundTypeTop:
corners = UIRectCornerTopRight | UIRectCornerTopLeft;
break;
case ZHRoundTypeNone:
corners = UIRectCornerBottomLeft & UIRectCornerBottomRight;
break;
case ZHRoundTypeAll:
corners = UIRectCornerAllCorners;
break;
default:
break;
}
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
//繪制邊框
CAShapeLayer *strokeLayer = [CAShapeLayer layer];
strokeLayer.path = maskPath.CGPath;
strokeLayer.fillColor = [UIColor clearColor].CGColor;
strokeLayer.strokeColor = _borderColor;
strokeLayer.lineWidth = _borderWidth;
// Transparent view that will contain the stroke layer
UIView *strokeView = [[UIView alloc] initWithFrame:self.bounds];
strokeView.userInteractionEnabled = NO; // in case your container view contains controls
[strokeView.layer addSublayer:strokeLayer];
// configure and add any subviews to the container view
// stroke view goes in last, above all the subviews
[self addSubview:strokeView];
}
@end
PS:源碼是在群里大神"恒總"的基礎上改進噠╭(╯3╰)╮,他的github地址:https://github.com/iPermanent