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

詳解iOS開發(fā)之自定義View

移動開發(fā) iOS
iOS開發(fā)之自定義View是本文要將介紹的內(nèi)容,iOS SDK中的View是UIView,我們可以很方便的自定義一個View。來看詳細內(nèi)容。

iOS開發(fā)之自定義View是本文要將介紹的內(nèi)容,iOS SDK中的View是UIView,我們可以很方便的自定義一個View。創(chuàng)建一個 Window-based Application程序,在其中添加一個Hypnosister的類,這個類選擇繼承UIObject。修改這個類,使他繼承:UIView

  1. @interface HypnosisView : UIView 

自定義View的關(guān)鍵是定義drawRect: 方法,因為主要是通過重載這個方法,來改變view的外觀。例如,可以使用下面代碼繪制一個很多環(huán)中環(huán)的效果的view

  1. View Code   
  2. - (void)drawRect:(CGRect)rect   
  3. {      
  4. // What rectangle am I filling?    CGRect bounds = [self bounds];      
  5. // Where is its center?    CGPoint center;      
  6. center.x = bounds.origin.x + bounds.size.width / 2.0;      
  7. center.y = bounds.origin.y + bounds.size.height / 2.0;      
  8. // From the center how far out to a corner?    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;      
  9. // Get the context being drawn upon    CGContextRef context = UIGraphicsGetCurrentContext();      
  10. // All lines will be drawn 10 points wide    CGContextSetLineWidth(context, 10);      
  11. // Set the stroke color to light gray    [[UIColor lightGrayColor] setStroke];      
  12. // Draw concentric circles from the outside in    for (float currentRadius = maxRadius; currentRadius > 0;   
  13. currentRadius -20)    {          
  14. CGContextAddArc(context, center.x, center.y,                          
  15.                           currentRadius, 0.0, M_PI * 2.0, YES);         
  16.  CGContextStrokePath(context);      
  17. }  

這樣view的效果如下圖:

詳解iOS開發(fā)之自定義View

我們可以繼續(xù)繪制一些東西,比如繪制文字,將下面代碼添加帶這個方法后面。

  1. // Create a string    NSString *text = @"我是朱祁林,不是朱麒麟";  
  2.     // Get a font to draw it in    UIFont *font = [UIFont boldSystemFontOfSize:28];  
  3.     // Where am I going to draw it?    CGRect textRect;   
  4.     textRect.size = [text sizeWithFont:font];  
  5.    textRect.origin.x = center.x - textRect.size.width / 2.0;  
  6.     textRect.origin.y = center.y - textRect.size.height / 2.0;      
  7. // Set the fill color of the current context to black     [[UIColor blackColor] setFill];      
  8. // Set the shadow to be offset 4 points right, 3 points down,       
  9. // dark gray and with a blur radius of 2 points     CGSize offset = CGSizeMake(4, 3);      
  10. CGColorRef color = [[UIColor darkGrayColor] CGColor];      
  11. CGContextSetShadowWithColor(context, offset, 2.0, color);      
  12. // Draw the string    [text drawInRect:textRect     
  13.                                withFont:font]; 

效果:

詳解iOS開發(fā)之自定義View

如果view過大,我們可以把它放置到一個UIScrollView中間,這樣就可以進行拖動了。UIScrollView與View的關(guān)系如下圖:

詳解iOS開發(fā)之自定義View

使用下面代碼創(chuàng)建一個比iPhone屏幕大4倍的View,然后通過UIScrollView來展示,代碼如下:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {   
  3.        //創(chuàng)建一個窗體大小的CGRect     
  4.         CGRect wholeWindow = [[self window] bounds];          
  5.         // 創(chuàng)建一個窗體大小的HypnosisView實例      
  6.         view = [[HypnosisView alloc] initWithFrame:wholeWindow];          
  7.         UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];      
  8.         [[self window] addSubview:scrollView];      
  9.         // Make your view twice as large as the window    CGRect reallyBigRect;      
  10.         reallyBigRect.origin = CGPointZero;      
  11.         reallyBigRect.size.width = wholeWindow.size.width * 2.0;     
  12.          reallyBigRect.size.height = wholeWindow.size.height * 2.0;      
  13.          [scrollView setContentSize:reallyBigRect.size];      
  14.          CGPoint offset;      
  15.          offset.x = wholeWindow.size.width * 0.5;      
  16.          offset.y = wholeWindow.size.height * 0.5;      
  17.          [scrollView setContentOffset:offset];          
  18.          // Create the view    view = [[HypnosisView alloc] initWithFrame:reallyBigRect];     
  19.           [view setBackgroundColor:[UIColor clearColor]];      
  20.           [scrollView addSubview:view];      
  21.           [scrollView release];          
  22.           [[UIApplication sharedApplication] setStatusBarHidden:YES  
  23.                                        withAnimation:UIStatusBarAnimationFade];      
  24.           [[self window] makeKeyAndVisible];      
  25.   return YES;  

這樣我們就可以拖動來展示看不到的view了,如下圖:

詳解iOS開發(fā)之自定義View

通過UIScrollView我們還可以設(shè)置view的縮放功能,將下面代碼添加到中。這樣我們就可以使用兩根手指縮放view了。

  1. // Enable zooming      
  2. [scrollView setMinimumZoomScale:0.5];      
  3. [scrollView setMaximumZoomScale:5];     
  4.  [scrollView setDelegate:self]; 

小結(jié):詳解iOS開發(fā)之自定義View的內(nèi)容介紹完了,簡單的總結(jié)了一下自定義view的使用,希望本文對你有所幫助!本文為了方便友們更好的去學(xué)IOS開發(fā)中的View,提供代碼下載,地址為:http://files.cnblogs.com/zhuqil/Hypnosister.zip 。

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2013-05-20 17:33:44

Android游戲開發(fā)自定義View

2021-10-26 10:07:02

鴻蒙HarmonyOS應(yīng)用

2016-12-26 15:25:59

Android自定義View

2016-11-16 21:55:55

源碼分析自定義view androi

2011-08-18 09:44:33

iPhone SDK儀表控件UIDialView

2016-04-12 10:07:55

AndroidViewList

2017-03-14 15:09:18

AndroidView圓形進度條

2012-05-18 10:52:20

TitaniumAndroid模塊自定義View模塊

2023-08-10 17:14:52

鴻蒙自定義彈窗

2021-11-01 17:31:21

Camera2 相機開發(fā)

2013-05-20 17:48:20

2009-06-08 20:13:36

Eclipse自定義控

2011-08-18 17:32:55

iPhone開發(fā)Table Cell

2011-04-19 10:33:16

ASP.NET自定義控

2013-06-27 11:10:01

iOS開發(fā)自定義UISlider

2011-12-05 15:02:21

Knockout

2017-03-02 13:33:19

Android自定義View

2024-10-14 17:18:27

2013-07-18 16:09:10

自定義iOS狀態(tài)欄iOS開發(fā)iOS學(xué)習(xí)

2011-10-09 11:07:40

百度地圖API
點贊
收藏

51CTO技術(shù)棧公眾號