1.创建一个新的ios object-c 类 HypnosisView ,继承UIView。
HypnosisView.h
#import <Foundation/Foundation.h>
@interface HypnosisView : UIView
{
}
@end
2.实现drawRect 方法。
#import "HypnosisView.h"
@implementation HypnosisView
- (void)drawRect:(CGRect)rect
{
// What rectangle am I filling?
CGRect bounds = [self bounds];
// 中心点
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;
// 中心点到线的距离
float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
// 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 10宽度画线
CGContextSetLineWidth(context, 10);
// 线颜色
[[UIColor lightGrayColor] setStroke];
// 循环画园
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20)
{
CGContextAddArc(context, center.x, center.y,
currentRadius, 0.0, M_PI * 2.0, YES);
CGContextStrokePath(context);
}
// Create a string
NSString *text = @"You are getting sleepy.";
// 获取font对象
UIFont *font = [UIFont boldSystemFontOfSize:28];
// 设置矩形
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;
// 设置当前上下文颜色为黑色
[[UIColor blackColor] setFill];
//设置字体阴影
CGSize offset = CGSizeMake(4, 3);
CGColorRef color = [[UIColor darkGrayColor] CGColor];
CGContextSetShadowWithColor(context, offset, 2.0, color);
// 字符串画到textRect里
[text drawInRect:textRect
withFont:font];
}
@end
3.应用HypnosisView.
HypnosisterAppDelegate.h
#import <UIKit/UIKit.h>
// This is a forward declaration
@class HypnosisView;
@interface HypnosisterAppDelegate : NSObject
<UIApplicationDelegate, UIScrollViewDelegate> {
HypnosisView *view;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
HypnosisterAppDelegate.m
#import "HypnosisterAppDelegate.h"
#import "HypnosisView.h"
@implementation HypnosisterAppDelegate
@synthesize window=_window;
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 获的窗口矩形
CGRect wholeWindow = [[self window] bounds];
//创建UIScrollView对象,添加到窗口里
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];
[[self window] addSubview:scrollView];
// 创建矩形空间 ,长宽是窗口的两倍
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];
// 设置scrollview里面的view的初始位置
CGPoint offset;
offset.x = wholeWindow.size.width * 0.5;
offset.y = wholeWindow.size.height * 0.5;
[scrollView setContentOffset:offset];
// 设置缩放
[scrollView setMinimumZoomScale:0.5];
[scrollView setMaximumZoomScale:5];
[scrollView setDelegate:self];
// 创建自定义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;
}
//因为委托对象为self ,所有缩放要实现此方法,返回缩放的View对象
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return view;
}
// A dealloc method that will never get called because
// HypnosisterAppDelegate will exist for the life of the application
- (void)dealloc
{
[view release];
[_window release];
[super dealloc];
}
@end
完成。