自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS應用開發(fā)教程:新建UIView的子類

移動開發(fā) iOS
本文我們將從實例想大家介紹iOS應用開發(fā)如何新建UIView的子類。包括自定義繪圖函數、將新視圖綁定到主窗口等內容。

大致步驟

1) 新建一個UIView的子類(@interface HypnosisView : UIView)

2) 自定義繪圖函數:(void) drawRect:(CGRect)rect

◆確定繪圖范圍:CGRect bounds=[self bounds]

◆獲得CGContext, CGContextRef context=UIGraphicsGetCurrentContext();

◆進行繪圖操作

3) 將新視圖綁定到主窗口

◆在HypnosisterAppDelegate中添加一個成員變量HypnosisView *view;

◆確定繪圖范圍

◆在didFinishLaunchingWithOptions中增加子視圖:[_window addSubview:view];

◆進行顯示 [_window makeKeyAndVisible];

待確定事項:

1) CGContextStrokePath的功能

2) makeKeyAndVisible消息的功能

關鍵代碼如下:

Java代碼

1) 綁定處理:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
  2.  
  3.  
  4. NSLog(@"didFinishLaunchingWithOptions."); 
  5.  
  6. CGRect drawingArea=[_window bounds]; 
  7.  
  8. view = [[HypnosisView alloc] initWithFrame:drawingArea]; 
  9.  
  10. [view setBackgroundColor:[UIColor yellowColor]]; 
  11.  
  12. [_window addSubview:view]; 
  13.  
  14. // Override point for customization after application launch. 
  15.  
  16. [_window makeKeyAndVisible]; 
  17.  
  18. return YES; 
  19.  

2) 繪圖處理:

  1. - (void) drawRect:(CGRect)rect  
  2.   
  3. {  
  4.   
  5. NSLog(@"Entering the drawing function of HyponsisView.");  
  6.   
  7. //Get the drawing rectangle  
  8.   
  9. CGRect bounds=[self bounds];  
  10.   
  11. //Calculate the references  
  12.   
  13. CGPoint center;  
  14.   
  15. center.x=bounds.origin.x+bounds.size.width/2.0;  
  16.   
  17. center.y=bounds.origin.y+bounds.size.height/2.0;  
  18.   
  19. float radius=hypot(bounds.size.width, bounds.size.height)/2.0;  
  20.   
  21. //Prepare Drawing  
  22.   
  23. CGContextRef context=UIGraphicsGetCurrentContext();  
  24.   
  25. CGContextSetLineWidth(context,10);  
  26.   
  27. [[UIColor greenColor] setStroke];  
  28.   
  29. //Drawing the circles  
  30.   
  31. forfloat r=radius; r>0; rr=r-25)  
  32.   
  33. {  
  34.   
  35. CGContextAddArc(context, center.x, center.y, r, 0.0, M_PI*2.0,YES);  
  36.   
  37. CGContextStrokePath(context);  
  38.   
  39. }  
  40.   
  41. }  

運行效果:

 

責任編輯:佚名 來源: iteye
相關推薦

2013-07-25 13:43:23

iOS開發(fā)學習UIView的Anim

2011-07-08 14:58:16

iPhone Xcode iOS

2011-08-11 10:16:23

iPhoneUIView視圖

2011-08-11 10:27:37

iPhoneUIView視圖

2011-08-15 13:50:06

IPhone開發(fā)UIView動畫

2011-08-09 10:27:41

iOS剪貼板

2011-08-12 11:31:46

iPhoneUIView動畫

2012-01-18 13:51:39

2012-12-24 13:38:01

iOSUIView

2011-05-11 10:02:37

iOS

2011-07-22 14:18:04

iOS 文件

2012-02-02 15:24:57

2011-07-28 09:58:31

Web

2013-09-13 13:16:05

2011-08-11 16:50:04

iOSTwitter

2015-12-09 14:00:41

ios應用

2012-08-30 09:32:12

FacebookiOS

2014-04-23 13:30:23

類簇iOS開發(fā)

2011-01-14 08:35:03

iPhoneiPad敏捷設計流程

2011-08-09 13:10:32

iPhone地圖開發(fā)
點贊
收藏

51CTO技術棧公眾號