iOS如何简单实现绘制爱心?
程序员文章站
2022-03-26 14:38:07
灵感来源于前端CSS画红心的原理 "参考" 上代码 简单调用如下: ......
灵感来源于前端css画红心的原理
上代码
#import <uikit/uikit.h> /** 灵感来自于前端css画红心的原理: 一个正方形 + 两个圆 + 整体旋转一定的角度 */ ns_assume_nonnull_begin ib_designable @interface wgbheartview : uiview //❤心有多大? @property (nonatomic,assign) ibinspectable cgfloat heartsize; //❤心的颜色? @property (nonatomic,strong) ibinspectable uicolor *heartcolor; @end ns_assume_nonnull_end #import "wgbheartview.h" @interface wgbheartview() @property (nonatomic,strong) uiview *bottomview; @property (nonatomic,strong) uiview *leftview; @property (nonatomic,strong) uiview *rightview; @end @implementation wgbheartview - (instancetype)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { [self initconfig]; } return self; } - (instancetype)initwithcoder:(nscoder *)coder { self = [super initwithcoder:coder]; if (self) { [self initconfig]; } return self; } - (void)initconfig{ //默认值 self.heartsize = 150.0; self.heartcolor = [uicolor redcolor]; [self addsubview: self.bottomview]; [self addsubview: self.leftview]; [self addsubview: self.rightview]; //提前旋转45度 self.transform = cgaffinetransformmakerotation(m_pi_4); } //去除设置背景色 - (void)setbackgroundcolor:(uicolor *)backgroundcolor{ backgroundcolor = [uicolor clearcolor]; [super setbackgroundcolor:backgroundcolor]; } - (void)setheartsize:(cgfloat)heartsize{ _heartsize = heartsize; cgfloat partsize = heartsize/3.0; self.bottomview.frame = cgrectmake(partsize, partsize, partsize*2 , partsize*2); self.leftview.frame = cgrectmake(0, partsize, partsize*2 , partsize*2); self.rightview.frame = cgrectmake(partsize, 0, partsize*2 , partsize*2); self.leftview.layer.cornerradius = partsize; self.rightview.layer.cornerradius = partsize; } - (void)setheartcolor:(uicolor *)heartcolor{ _heartcolor = heartcolor; self.bottomview.backgroundcolor = heartcolor; self.leftview.backgroundcolor = heartcolor; self.rightview.backgroundcolor = heartcolor; } ///mark:- lazy load - (uiview *)bottomview{ if (!_bottomview) { _bottomview = [[uiview alloc] initwithframe:cgrectzero]; [self addsubview:_bottomview]; } return _bottomview; } - (uiview *)leftview{ if (!_leftview) { _leftview = [[uiview alloc] initwithframe:cgrectzero]; [self addsubview:_leftview]; } return _leftview; } - (uiview *)rightview{ if (!_rightview) { _rightview = [[uiview alloc] initwithframe:cgrectzero]; [self addsubview:_rightview]; } return _rightview; } @end
简单调用如下:
wgbheartview *heartview = [[wgbheartview alloc] initwithframe:cgrectmake(100, 100, 100 , 100)]; heartview.heartcolor = [uicolor blackcolor];//默认颜色是红色 heartview.heartsize = 100; //这个尺寸最好是设置与视图宽高一致 生成的爱心❤️比较规则 [self.view addsubview: heartview]; for (nsinteger i = 0; i < 6; i += 1) { cgfloat heartwh = 50.0f; cgfloat margin = 15.0f; wgbheartview *heartitemview = [[wgbheartview alloc] initwithframe:cgrectmake(20 + (heartwh+margin)*i, 250, heartwh , heartwh)]; heartitemview.heartcolor = [uicolor colorwithred:arc4random()%256/255.0f green:arc4random()%256/255.0f blue:arc4random()%256/255.0f alpha:1.0f]; heartitemview.heartsize = heartwh; [self.view addsubview: heartitemview]; }
下一篇: 还记得第一个看到的Flutter组件吗?