IOS自定義UIView
IOS中一般會用到幾種方式自定義UIView
1、繼承之UIView的存代碼的自定義View
2、使用xib和代碼一起使用的自定義View
3、存xib的自定義View(不需要業(yè)務(wù)處理的那種)
本文主要就介紹下存代碼的自定義UIView和能夠在storeboard中實時顯示效果的自定義UIView
先上效果圖

上面為設(shè)計界面,能夠直接顯示一個View的圓角與邊框線

上面那個圓形餅圖是用純代碼自定義的
1.實現(xiàn)在storeboard中實時顯示效果的自定義UIView
1、創(chuàng)建MyView.h 繼承 UIView
#import <UIKit/UIKit.h> // 設(shè)置類為可視化設(shè)計 IB_DESIGNABLE @interface MyView : UIView // IBInspectable 為可視化設(shè)計屬性 // 邊框?qū)挾? @property (nonatomic,assign)IBInspectable float borderWidth; // 邊框顏色 @property (nonatomic,retain)IBInspectable UIColor* borderColor; // 圓角 @property (nonatomic,assign)IBInspectable float cornerRadius; @end
這里要注意的就是上面的兩個關(guān)鍵標(biāo)簽
IB_DESIGNABLE :代表的是這個類可以在storeboard中顯示實時的效果
IBInspectable :代表把這個屬性能在storeboard中修改
2、MyView.m的實現(xiàn)
//
// MyView.m
// 01_CirProgress
//
// Created by xgao on 15/10/29.
// Copyright (c) 2015年 xgao. All rights reserved.
//
#import "MyView.h"
@implementation MyView
// 邊框?qū)挾?
- (void)setBorderWidth:(float)borderWidth{
self.layer.borderWidth = borderWidth;
}
// 邊框顏色
- (void)setBorderColor:(UIColor *)borderColor{
self.layer.borderColor = borderColor.CGColor;
}
// 圓角
- (void)setCornerRadius:(float)cornerRadius{
self.layer.cornerRadius = cornerRadius;
}
@end
3、在storeboad中添加一個view,并且設(shè)置這個view的類為 我們剛才創(chuàng)建的 MyView


上圖里面的那些屬性就是我們在.h文件里面加了IBInspectable關(guān)鍵字的屬性,這里就能實時修改看效果了。
2.實現(xiàn)純代碼的自定義View
1、創(chuàng)建一個繼承UIView的 MyProgress 類文件,MyProgress.h 如下:
#import <UIKit/UIKit.h> @interface MyProgress : UIView // 當(dāng)時進(jìn)度值 @property (nonatomic,assign) float progressValue; @end
2、MyProgress.m 如下:
#import "MyProgress.h"
@implementation MyProgress
{
float _proValue;
}
// 重寫初始化方法
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// 設(shè)置背影為透明色
self.backgroundColor = [UIColor clearColor];
}
return self;
}
// 重設(shè)progressValue屬性
- (void)setProgressValue:(float)progressValue
{
_progressValue = progressValue;
// 重新畫UI
[self setNeedsDisplay];
}
// 繪圖
- (void)drawRect:(CGRect)rect {
// 獲取畫圖的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
/**** 繪制圓形背景線 ****/
// 圓的半徑
float r = rect.size.width / 2.0;
// 全圓
CGFloat endAngle = M_PI * 2;
// 畫圓形線
CGContextAddArc(ctx, r, r, r, 0, endAngle, 0);
// 背影顏色
CGContextSetRGBFillColor(ctx, 0.7, 0.7,0.7, 1);
// 完成繪制
CGContextFillPath(ctx);
/**** 繪制扇形區(qū)域 ****/
// 計算結(jié)束角度
endAngle = M_PI * 2 * _progressValue;
/** 畫圓
* 參數(shù)1:c 當(dāng)前上下文
* 參數(shù)2:x 圓的X坐標(biāo)
* 參數(shù)3:y 圓的Y坐標(biāo)
* 參數(shù)4:radius 圓的半徑
* 參數(shù)5:startAngle 開始角度
* 參數(shù)6:endAngle 結(jié)束角度
* 參數(shù)7:clockwise 是否逆時針
*/
CGContextAddArc(ctx, r, r, r, 0, endAngle, 0);
// 連成線,成弧形
CGContextAddLineToPoint(ctx, r, r);
// 其實就是在連接的那條線上加一個點(diǎn),讓線條連接到那一個點(diǎn),就像拉弓,可加多個點(diǎn)
// CGContextAddLineToPoint(ctx, r + 20, r + 20);
// 填充顏色
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
// 完成繪制
CGContextFillPath(ctx);
}
3、調(diào)用自定義的MyProgress類
#import "MyProgress.h"
@interface ViewController ()
{
MyProgress* _myProView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建自定義控件
_myProView = [[MyProgress alloc]initWithFrame:CGRectMake(100, 50, 180, 180)];
// 默認(rèn)進(jìn)度
_myProView.progressValue = 0.2;
[self.view addSubview:_myProView];
}
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
iOS適配https證書問題(AFNetworking3.0為例)
本篇文章主要介紹了iOS適配https問題(AFNetworking3.0為例)。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
詳解iOS多線程之2.NSThread的加鎖@synchronized
這篇文章主要介紹了詳解iOS多線程之2.NSThread的加鎖@synchronized,有需要的小伙伴可以參考下。2016-11-11
ios App加載本地HTML網(wǎng)頁,點(diǎn)擊網(wǎng)頁鏈接跳轉(zhuǎn)到app頁面的方法
下面小編就為大家分享一篇ios App加載本地HTML網(wǎng)頁,點(diǎn)擊網(wǎng)頁鏈接跳轉(zhuǎn)到app頁面的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
iOS UILabel根據(jù)內(nèi)容自動調(diào)整高度
這篇文章主要為大家詳細(xì)介紹了iOS UILabel根據(jù)內(nèi)容自動調(diào)整高度,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
iOS App中UIPickerView選擇欄控件的使用實例解析
這篇文章主要介紹了iOS App中的UIPickerView選擇欄控件的使用,文中演示了兩個超詳細(xì)的例子,示例代碼為Objective-C,需要的朋友可以參考下2016-04-04

