菜鸟贴:如何在UiViewController中绘制图形? UiViewcontroller.view就是UIView,,UiViewcontroller addsubview方法可以把自定义的UIView添加到UiViewcontroller里面去并展示出来。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的头文件中以下: @interface testDrawVC : UIViewController{ UIView *view;}对应的m文件的viewDidLoad定义view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]]; view.backgroundColor =[UIColor blackColor];//黑色背景 self.view = view; //此处self没有uiviewcontroller在m文件中定义-(void)drawRect:(CGRect)rect{ CGContextRef ctx=UIGraphicsGetCurrentContext(); CGContextSetLineWidth(ctx, 2); CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);//白色线条 const CGPoint points[]={CGPointMake(5, 45),CGPointMake(315, 245)}; CGContextStrokeLineSegments(ctx, points, 2);}运行后在黑色背景中并没有看到白色线条。是否这种加uiview的方法并没 有执行drawRect?继续请教,这是怎么回事呢? 可以有两种方法第一种:自定义一个UIView实现一个继承自UIView的子类/////.h 头文件@interface myView:UIView @end////.m 实现文件@implemention myView-(void)drawRect:(CGRect)rect{ CGContextRef ctx=UIGraphicsGetCurrentContext(); CGContextSetLineWidth(ctx, 2); CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);//白色线条 const CGPoint points[]={CGPointMake(5, 45),CGPointMake(315, 245)}; CGContextStrokeLineSegments(ctx, points, 2);}@end自定义的UIView 写好以后,在你的UIViewController中导入@class myView; @interface testDrawVC : UIViewController{ myView *_view;}对应的m文件的viewDidLoad定义_view = [[myView alloc] initWithFrame:[[UIScreen mainScreen]applicationFrame]];[self.view addSubview:_view];第二种方法:是使用CAShapeLayer+UIBezierPath 来绘制 回zhanglei5415: 试过了,完全可以实现我的要求!讲得很详细透彻,非常佩服! 学习了! 结贴,给分! 可以有两种方法第一种:自定义一个UIView实现一个继承自UIView的子类 UITableViewCell如何显示固定尺寸图片? 怎么在iPhone应用程序中调用命令行并返回结果??? openflow 问题 求助:iOS手机app内设置手机可以做ap有开放的api否? 关于xcode5启动模拟器报错的问题 tableview设置为static cell后为何在模拟器中的排版就不对了 iOS 全局变量赋值失败,求求各位大神救救我这个新手 ios一次请求失败,再次请求一直无法连接,严重bug! ios 自定义navigationitem 右滑返回时会走自定义返回按钮的方法吗 iOS谁能帮我解析一下这个地址 app启动页面的问题 UIButton 编译错误
我的头文件中以下: @interface testDrawVC : UIViewController
{
UIView *view;
}
对应的m文件的viewDidLoad定义view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
view.backgroundColor =[UIColor blackColor];//黑色背景
self.view = view; //此处self没有uiviewcontroller在m文件中定义-(void)drawRect:(CGRect)rect
{
CGContextRef ctx=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 2);
CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);//白色线条
const CGPoint points[]={CGPointMake(5, 45),CGPointMake(315, 245)};
CGContextStrokeLineSegments(ctx, points, 2);
}
运行后在黑色背景中并没有看到白色线条。是否这种加uiview的方法并没 有执行drawRect?继续请教,这是怎么回事呢?
第一种:自定义一个UIView
实现一个继承自UIView的子类
/////.h 头文件
@interface myView:UIView @end
////.m 实现文件
@implemention myView-(void)drawRect:(CGRect)rect
{
CGContextRef ctx=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 2);
CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);//白色线条
const CGPoint points[]={CGPointMake(5, 45),CGPointMake(315, 245)};
CGContextStrokeLineSegments(ctx, points, 2);
}
@end自定义的UIView 写好以后,在你的UIViewController中导入@class myView;
@interface testDrawVC : UIViewController
{
myView *_view;
}对应的m文件的viewDidLoad定义_view = [[myView alloc] initWithFrame:[[UIScreen mainScreen]applicationFrame]];
[self.view addSubview:_view];第二种方法:是使用CAShapeLayer+UIBezierPath 来绘制
试过了,完全可以实现我的要求!讲得很详细透彻,非常佩服!
学习了!
结贴,给分!
第一种:自定义一个UIView
实现一个继承自UIView的子类