iOS sdk中的view是UIView,我們可以很方便的自定義一個View。
創建一個 Window-based Application程序,在其中添加一個Hypnosister的類,這個類選擇繼承UIObject。修改這個類,使他繼承:UIView
@interface HypnosisView : UIView
自定義View的關鍵是定義drawRect: 方法,因爲主要是通過重載這個方法,來改變view的外觀。例如,可以使用下面代碼繪製一個很多環中環的效果的view。
- (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的效果如下圖:
我們可以繼續繪製一些東西,比如繪製文字,將下面代碼添加帶這個方法後面。
// 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的關係如下圖:
使用下面代碼創建一個比iPhone屏幕大4倍的View,然後通過UIScrollView來展示,代碼如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//創建一個窗體大小的CGRect
CGRect wholeWindow = [[self window] bounds];
// 創建一個窗體大小的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我們還可以設置view的縮放功能,將下面代碼添加到中。這樣我們就可以使用兩根手指縮放view了。
// Enable zooming
[scrollView setMinimumZoomScale:0.5];
[scrollView setMaximumZoomScale:5];
[scrollView setDelegate:self];
總結:文本簡單的總結了一下自定義view的使用。
作者:朱祁林
出處:http://zhuqil.cnblogs.com
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。