星空网 > 软件开发 > 操作系统

设置指定边界圆角

最近项目中需要用到一些视图的某些边界设置为圆角,比如指定一个长方形view,需要设置其左边为圆形,所以就封装一个类来实现指定边界的圆角

1,首先创建一个继承于UIView的分类(由于项目中我需设置一个button的圆角, 所以命名为Btn)

设置指定边界圆角images/loading.gif' data-original="http://images2015.cnblogs.com/blog/790189/201604/790189-20160409141848547-708258857.png" />

 

2.在接口文件中设置接口

这里参数:distance 为需要设置圆角的那个边界的高度或宽度

#import <UIKit/UIKit.h>typedef enum {  ZSSideRoundLeft,  ZSSideRoundRight,  ZSSideRoundUp,  ZSSideRoundDown} ZSSideRound;@interface UIView (ZSSideRoundBtn)- (void)roundSide:(ZSSideRound)side distance:(CGFloat)distance;@end

 

 

3.在.m中实现 (在这里指定边界设置为半圆)

#import "UIView+ZSSideRoundBtn.h"@implementation UIView (ZSSideRoundBtn)- (void)roundSide:(ZSSideRound)side distance:(CGFloat)distance{  UIBezierPath* maskPath;  if (side == ZSSideRoundLeft)  {    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds                     byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft)                        cornerRadii:CGSizeMake(distance/2, distance/2)];  }  else if (side == ZSSideRoundRight)  {    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds                     byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight)                        cornerRadii:CGSizeMake(distance/2, distance/2)];  }  else if (side == ZSSideRoundUp)  {    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds                     byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)                        cornerRadii:CGSizeMake(distance/2, distance/2)];  }  else  {    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds                     byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)                        cornerRadii:CGSizeMake(distance/2, distance/2)];  }  // 创建形状图层,设置它的路径  CAShapeLayer* maskLayer = [CAShapeLayer layer];  maskLayer.frame = self.bounds;  maskLayer.path = maskPath.CGPath;  // 新创建的形状图层设置为图像视图层的面具  self.layer.mask = maskLayer;  [self.layer setMasksToBounds:YES];}@end

 

 

搞定,以后需要设置一些UIView的子类视图的边界为半圆时,直接引用就OK

 




原标题:设置指定边界圆角

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

鲜果布衣 Fruit of the Loom :https://www.goluckyvip.com/tag/43797.html
鲜跨境电商:https://www.goluckyvip.com/tag/43798.html
弦镜 Synlen :https://www.goluckyvip.com/tag/43799.html
桨歌位列第6:https://www.goluckyvip.com/tag/438.html
违规合并变体:https://www.goluckyvip.com/tag/4380.html
显盈科技:https://www.goluckyvip.com/tag/43800.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
石象湖景区门票-石象湖景区门票优惠政策:https://www.vstour.cn/a/411243.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流