iOS应用开发教程:新建UIView的子类
大致步骤1) 新建一个UIView的子类(@interface HypnosisView : UIView)
2) 自定义绘图函数:(void) drawRect:(CGRect)rect
◆确定绘图范围:CGRect bounds=
◆获得CGContext, CGContextRef context=UIGraphicsGetCurrentContext();
◆进行绘图操作
3) 将新视图绑定到主窗口
◆在HypnosisterAppDelegate中添加一个成员变量HypnosisView *view;
◆确定绘图范围
◆在didFinishLaunchingWithOptions中增加子视图:;
◆进行显示 ;
待确定事项:
1) CGContextStrokePath的功能
2) makeKeyAndVisible消息的功能
关键代码如下:
Java代码
1) 绑定处理:
[*]- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
[*]
[*]{
[*]
[*]NSLog(@"didFinishLaunchingWithOptions.");
[*]
[*]CGRect drawingArea=;
[*]
[*]view = [ initWithFrame:drawingArea];
[*]
[*]];
[*]
[*];
[*]
[*]// Override point for customization after application launch.
[*]
[*];
[*]
[*]return YES;
[*]
[*]}
2) 绘图处理:
[*]- (void) drawRect:(CGRect)rect
[*]
[*]{
[*]
[*]NSLog(@"Entering the drawing function of HyponsisView.");
[*]
[*]//Get the drawing rectangle
[*]
[*]CGRect 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);
[*]
[*][ setStroke];
[*]
[*]//Drawing the circles
[*]
[*]for( float r=radius; r>0; rr=r-25)
[*]
[*]{
[*]
[*]CGContextAddArc(context, center.x, center.y, r, 0.0, M_PI*2.0,YES);
[*]
[*]CGContextStrokePath(context);
[*]
[*]}
[*]
[*]}
运行效果:
http://images.51cto.com/files/uploadimg/20110628/1037170.png<DIV align=right>【责任编辑:立方 TEL:(010)68476606】
页:
[1]