iOS sdk中的view是UIView,我們可以很方便的自訂一個View。
建立一個 Window-based Application程式,在其中添加一個Hypnosister的類,這個類選擇繼承UIObject。修改這個類,使他繼承:UIView
@interface HypnosisView : UIView
自訂View的關鍵是定義drawRect: 方法,因為主要是通過重載這個方法,來改變view的外觀。例如,可以使用下面代碼繪製一個很多環中環的效果的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的效果如:
我們可以繼續繪製一些東西,比如繪製文字,將下面代碼添加帶這個方法後面。
// 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的使用。
代碼:Hypnosister.zip