日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

画一个单实线,方向可以定制

發布時間:2023/12/14 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 画一个单实线,方向可以定制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  app中很多地方用到了單實線,有的是橫著的,有的是豎著的,偷懶的時候直接用UIView,設置背景色就搞定了。。。不過,心里很是不安&不爽。下邊就上代碼了。

  SingleLineView.h

1 #import <UIKit/UIKit.h> 2 3 // 單實線方向 4 typedef enum { 5 // 橫向 6 SingleLineOrientationVertical, 7 // 縱向 8 SingleLineOrientationHorizontal 9 } SingleLineOrientation; 10 11 @interface SingleLineView : UIView { 12 CGFloat _lineWidth; 13 UIColor *_lineColor; 14 SingleLineOrientation _lineOrientation; 15 } 16 17 // 單實線寬度 18 @property (nonatomic) CGFloat lineWidth; 19 // 單實線顏色 20 @property (nonatomic, strong) UIColor *lineColor; 21 // 單實線方向 22 @property (nonatomic) SingleLineOrientation lineOrientation; 23 24 @end

  SingleLineView.m

1 #import "SingleLineView.h" 2 3 @implementation SingleLineView 4 5 - (instancetype)init { 6 self = [super init]; 7 8 // 默認情況下單實線樣式 9 if (self) { 10 [self setLineWidth:0.5]; 11 [self setLineColor:[UIColor grayColor]]; 12 [self setLineOrientation:SingleLineOrientationHorizontal]; 13 [self setBackgroundColor:[UIColor clearColor]]; 14 } 15 16 return self; 17 } 18 19 // Only override drawRect: if you perform custom drawing. 20 // An empty implementation adversely affects performance during animation. 21 - (void)drawRect:(CGRect)rect { 22 CGContextRef context = UIGraphicsGetCurrentContext(); 23 CGContextSetLineCap(context, kCGLineCapRound); 24 CGContextSetLineWidth(context, self.lineWidth); 25 CGContextSetAllowsAntialiasing(context, YES); 26 CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor); 27 CGContextBeginPath(context); 28 29 CGContextMoveToPoint(context, 0, 0); 30 if (self.lineOrientation == SingleLineOrientationHorizontal) { 31 CGContextAddLineToPoint(context, self.frame.size.width, 0); 32 } else { 33 CGContextAddLineToPoint(context, 0, self.frame.size.height); 34 } 35 36 CGContextStrokePath(context); 37 } 38 39 #pragma mark - getters && setters 40 41 - (CGFloat)lineWidth { 42 return _lineWidth; 43 } 44 45 - (void)setLineWidth:(CGFloat)lineWidth { 46 _lineWidth = lineWidth; 47 [self setNeedsDisplay]; 48 } 49 50 - (UIColor *)lineColor { 51 return _lineColor; 52 } 53 54 - (void)setLineColor:(UIColor *)lineColor { 55 _lineColor = lineColor; 56 [self setNeedsDisplay]; 57 } 58 59 - (SingleLineOrientation)lineOrientation { 60 return _lineOrientation; 61 } 62 63 - (void)setLineOrientation:(SingleLineOrientation)lineOrientation { 64 _lineOrientation = lineOrientation; 65 } 66 67 @end

  使用方法:

self.singleLineTest = [[SingleLineView alloc] init]; //[self.singleLineTest setLineOrientation:SingleLineOrientationVertical]; [self.view addSubview:self.singleLineTest];[self.singleLineTest mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.view).with.offset(50);make.right.equalTo(self.view).with.offset(-50);make.top.equalTo(self.view).with.offset(200);make.height.mas_equalTo(1);}];

  以上。

轉載于:https://www.cnblogs.com/zpz501/p/5147665.html

總結

以上是生活随笔為你收集整理的画一个单实线,方向可以定制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。