浏览 2293 次
锁定老帖子 主题:新建UIView的子类
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-06-26
1) 新建一个UIView的子类(@interface HypnosisView : UIView) 2) 自定义绘图函数:(void) drawRect:(CGRect)rect
3) 将新视图绑定到主窗口 待确定事项: 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); } } 运行效果: 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |