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
- (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
- (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?
Define your
drawRect:so that it decides whether it should draw a line or not:When the button is tapped, have your view controller set your view parameters and tell the system to refresh your view: