詳解iOS開發(fā)之自定義View
iOS開發(fā)之自定義View是本文要將介紹的內(nèi)容,iOS SDK中的View是UIView,我們可以很方便的自定義一個View。創(chuàng)建一個 Window-based Application程序,在其中添加一個Hypnosister的類,這個類選擇繼承UIObject。修改這個類,使他繼承:UIView
- @interface HypnosisView : UIView
自定義View的關(guān)鍵是定義drawRect: 方法,因為主要是通過重載這個方法,來改變view的外觀。例如,可以使用下面代碼繪制一個很多環(huán)中環(huán)的效果的view。
- View Code
- - (void)drawRect:(CGRect)rect
- {
- // What rectangle am I filling? CGRect bounds = [self bounds];
- // Where is its center? CGPoint center;
- center.x = bounds.origin.x + bounds.size.width / 2.0;
- center.y = bounds.origin.y + bounds.size.height / 2.0;
- // From the center how far out to a corner? float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
- // Get the context being drawn upon CGContextRef context = UIGraphicsGetCurrentContext();
- // All lines will be drawn 10 points wide CGContextSetLineWidth(context, 10);
- // Set the stroke color to light gray [[UIColor lightGrayColor] setStroke];
- // Draw concentric circles from the outside in for (float currentRadius = maxRadius; currentRadius > 0;
- currentRadius -= 20) {
- CGContextAddArc(context, center.x, center.y,
- currentRadius, 0.0, M_PI * 2.0, YES);
- CGContextStrokePath(context);
- }
- }
這樣view的效果如下圖:
我們可以繼續(xù)繪制一些東西,比如繪制文字,將下面代碼添加帶這個方法后面。
- // Create a string NSString *text = @"我是朱祁林,不是朱麒麟";
- // Get a font to draw it in UIFont *font = [UIFont boldSystemFontOfSize:28];
- // Where am I going to draw it? CGRect textRect;
- textRect.size = [text sizeWithFont:font];
- textRect.origin.x = center.x - textRect.size.width / 2.0;
- textRect.origin.y = center.y - textRect.size.height / 2.0;
- // Set the fill color of the current context to black [[UIColor blackColor] setFill];
- // Set the shadow to be offset 4 points right, 3 points down,
- // dark gray and with a blur radius of 2 points CGSize offset = CGSizeMake(4, 3);
- CGColorRef color = [[UIColor darkGrayColor] CGColor];
- CGContextSetShadowWithColor(context, offset, 2.0, color);
- // Draw the string [text drawInRect:textRect
- withFont:font];
效果:
如果view過大,我們可以把它放置到一個UIScrollView中間,這樣就可以進行拖動了。UIScrollView與View的關(guān)系如下圖:
使用下面代碼創(chuàng)建一個比iPhone屏幕大4倍的View,然后通過UIScrollView來展示,代碼如下:
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- //創(chuàng)建一個窗體大小的CGRect
- CGRect wholeWindow = [[self window] bounds];
- // 創(chuàng)建一個窗體大小的HypnosisView實例
- view = [[HypnosisView alloc] initWithFrame:wholeWindow];
- UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];
- [[self window] addSubview:scrollView];
- // Make your view twice as large as the window CGRect reallyBigRect;
- reallyBigRect.origin = CGPointZero;
- reallyBigRect.size.width = wholeWindow.size.width * 2.0;
- reallyBigRect.size.height = wholeWindow.size.height * 2.0;
- [scrollView setContentSize:reallyBigRect.size];
- CGPoint offset;
- offset.x = wholeWindow.size.width * 0.5;
- offset.y = wholeWindow.size.height * 0.5;
- [scrollView setContentOffset:offset];
- // Create the view view = [[HypnosisView alloc] initWithFrame:reallyBigRect];
- [view setBackgroundColor:[UIColor clearColor]];
- [scrollView addSubview:view];
- [scrollView release];
- [[UIApplication sharedApplication] setStatusBarHidden:YES
- withAnimation:UIStatusBarAnimationFade];
- [[self window] makeKeyAndVisible];
- return YES;
- }
這樣我們就可以拖動來展示看不到的view了,如下圖:
通過UIScrollView我們還可以設(shè)置view的縮放功能,將下面代碼添加到中。這樣我們就可以使用兩根手指縮放view了。
- // Enable zooming
- [scrollView setMinimumZoomScale:0.5];
- [scrollView setMaximumZoomScale:5];
- [scrollView setDelegate:self];
小結(jié):詳解iOS開發(fā)之自定義View的內(nèi)容介紹完了,簡單的總結(jié)了一下自定義view的使用,希望本文對你有所幫助!本文為了方便友們更好的去學(xué)IOS開發(fā)中的View,提供代碼下載,地址為:http://files.cnblogs.com/zhuqil/Hypnosister.zip 。