xcode 当我按下按钮时,如何在 UIview 中画一条线?(iOS)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12857940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw a line in a UIview, when I push button? (iOS)
提问by Nikita Bonachev
I began to study Xcode and I can't understand how to draw a line in a view, when I push button I have this code
我开始学习 Xcode,但我不明白如何在视图中画一条线,当我按下按钮时,我有这个代码
- (void)drawRect:(CGRect)rect
{
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(con, 5);
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGFloat componen [] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(space, componen);
CGContextSetStrokeColorWithColor(con, color);
CGContextMoveToPoint(con, 0, 0);
CGContextAddLineToPoint(con, 100, 100);
CGContextStrokePath(con);
CGColorSpaceRelease(space);
CGColorRelease(color);
}
This code draw a line when I start my application, but I wanna to start this code when I push button with my parameters(x1,x2,y1,y2). I created a function like this
这段代码在我启动应用程序时画了一条线,但是当我按下带有参数(x1,x2,y1,y2)的按钮时,我想启动这段代码。我创建了一个这样的函数
- (void)drawLine
{
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(con, 5);
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGFloat componen [] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(space, componen);
CGContextSetStrokeColorWithColor(con, color);
CGContextMoveToPoint(con, x1, y1);
CGContextAddLineToPoint(con, x2, y2);
CGContextStrokePath(con);
CGColorSpaceRelease(space);
CGColorRelease(color);
}
But it can't draw
但是画不出来
How to do this?
这该怎么做?
采纳答案by Phillip Mills
Define your drawRect:
so that it decides whether it should draw a line or not:
定义你的,drawRect:
以便它决定是否应该画一条线:
- (void)drawRect:(CGRect)rect {
if (self.drawLine) {
[self drawLineWithContext: UIGraphicsGetCurrentContext()];
self.drawLine = NO;
}
}
When the button is tapped, have your view controller set your view parameters and tell the system to refresh your view:
当按钮被点击时,让你的视图控制器设置你的视图参数并告诉系统刷新你的视图:
- (IBAction)doDrawing:(id)sender {
self.drawView.drawLine = YES;
self.drawView.x1 = 20.0;
self.drawView.x2 = 200.0;
self.drawView.y1 = 10.0;
self.drawView.y2 = 350.0;
[self.drawView setNeedsDisplay];
}
回答by brianLikeApple
What I add a line for UIView is pretty simple.
我为 UIView 添加一行非常简单。
UILabel *line = [[UILabel alloc]init];
line.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
line.frame = CGRectMake(0, 0, 320, 1);
[self.view addSubview:line];
And then you can press button to control its visible property.
然后你可以按下按钮来控制它的可见属性。
Is that OK for your question?
你的问题可以吗?
回答by damithH
the
- (void)drawRect:(CGRect)rect { }
method calls by the system.
before the view load it will call by the system and draw the content.
的
- (无效)的drawRect:(的CGRect)RECT {}
由系统方法调用。在视图加载之前,它会被系统调用并绘制内容。
so if you change the drawing content after the view load you have to ask the system to call drawRect method. so you have to call the relevant view's setNeedsDisplay method.
因此,如果在视图加载后更改绘图内容,则必须要求系统调用 drawRect 方法。所以你必须调用相关视图的 setNeedsDisplay 方法。
[myView setNeedsDisplay];
[myView setNeedsDisplay];
thanx
谢谢