新建UIView的子类

大致步骤
1) 新建一个UIView的子类(@interface HypnosisView : UIView)
2) 自定义绘图函数:(void) drawRect:(CGRect)rect
[list]
[*] 确定绘图范围:CGRect bounds=[self bounds]
[*] 获得CGContext, CGContextRef context=UIGraphicsGetCurrentContext();
[*] 进行绘图操作
[/list]
3) 将新视图绑定到主窗口
[*] 在HypnosisterAppDelegate中添加一个成员变量HypnosisView *view;
[*] 确定绘图范围
[*] 在didFinishLaunchingWithOptions中增加子视图:[_window addSubview:view];
[*] 进行显示 [_window makeKeyAndVisible];

待确定事项:
1) CGContextStrokePath的功能
2) makeKeyAndVisible消息的功能

关键代码如下:

1) 绑定处理:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"didFinishLaunchingWithOptions.");

CGRect drawingArea=[_window bounds];
view = [[HypnosisView alloc] initWithFrame:drawingArea];
[view setBackgroundColor:[UIColor yellowColor]];
[_window addSubview:view];

// Override point for customization after application launch.
[_window makeKeyAndVisible];
return YES;
}
2) 绘图处理:
- (void) drawRect:(CGRect)rect
{

NSLog(@"Entering the drawing function of HyponsisView.");
//Get the drawing rectangle
CGRect bounds=[self bounds];


//Calculate the references
CGPoint center;
center.x=bounds.origin.x+bounds.size.width/2.0;
center.y=bounds.origin.y+bounds.size.height/2.0;

float radius=hypot(bounds.size.width, bounds.size.height)/2.0;

//Prepare Drawing
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,10);
[[UIColor greenColor] setStroke];

//Drawing the circles
for( float r=radius; r>0; r=r-25)
{
CGContextAddArc(context, center.x, center.y, r, 0.0, M_PI*2.0,YES);
CGContextStrokePath(context);
}
}


运行效果:

[img]http://dl.iteye.com/upload/attachment/504910/5fa4f3a9-3b37-355f-816a-b7c591c16247.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值