【移動應(yīng)用開發(fā)技術(shù)】IOS如何自定義UIView_第1頁
【移動應(yīng)用開發(fā)技術(shù)】IOS如何自定義UIView_第2頁
【移動應(yīng)用開發(fā)技術(shù)】IOS如何自定義UIView_第3頁
【移動應(yīng)用開發(fā)技術(shù)】IOS如何自定義UIView_第4頁
【移動應(yīng)用開發(fā)技術(shù)】IOS如何自定義UIView_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】IOS如何自定義UIView

IOS中一般會用到幾種方式自定義UIView1、繼承之UIView的存代碼的自定義View2、使用xib和代碼一起使用的自定義View3、存xib的自定義View(不需要業(yè)務(wù)處理的那種)本文主要就介紹下存代碼的自定義UIView和能夠在storeboard中實時顯示效果的自定義UIView/upload/information/20200623/126/122221.png/upload/information/20200623/126/122223.png1.實現(xiàn)在storeboard中實時顯示效果的自定義UIView

1、創(chuàng)建MyView.h繼承UIView#import

<UIKit/UIKit.h>

//

設(shè)置類為可視化設(shè)計

IB_DESIGNABLE

@interface

MyView

:

UIView

//

IBInspectable

為可視化設(shè)計屬性

//

邊框?qū)挾?/p>

@property

(nonatomic,assign)IBInspectable

float

borderWidth;

//

邊框顏色

@property

(nonatomic,retain)IBInspectable

UIColor*

borderColor;

//

圓角

@property

(nonatomic,assign)IBInspectable

float

cornerRadius;

@endIB_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ū)挾?/p>

-

(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;

}

@end3、在storeboad中添加一個view,并且設(shè)置這個view的類為我們剛才創(chuàng)建的MyView/upload/information/20200623/126/122224.png/upload/information/20200623/126/122225.png2.實現(xiàn)純代碼的自定義View1、創(chuàng)建一個繼承UIView的MyProgress類文件,MyProgress.h如下:#import

<UIKit/UIKit.h>

@interface

MyProgress

:

UIView

//

當(dāng)時進(jìn)度值

@property

(nonatomic,assign)

float

progressValue;

@end2、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);

//

其實就是在連接的那條線上加一個點,讓線條連接到那一個點,就像拉弓,可加多個點

//

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)建自定

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論