Objective c iOS中的线条绘制
如何在iOS中绘制直线,而不管用户如何绘制 我希望线条的长度是用户拖动手指的像素数 但它需要是垂直的还是水平的,这取决于用户是将手指从屏幕的左到右滑动,还是从屏幕的上到下滑动 线条不应倾斜或任何其他形状。它必须是直的 我看过一些文章,上面说“OpenGL”是唯一的方法 我曾尝试自己编写代码,但当我将方向从垂直方向更改为水平方向或从水平方向更改为垂直方向时,在水平线结束和垂直线开始之间会有一些额外的线或间隙: 在头文件中:Objective c iOS中的线条绘制,objective-c,core-graphics,line-drawing,Objective C,Core Graphics,Line Drawing,如何在iOS中绘制直线,而不管用户如何绘制 我希望线条的长度是用户拖动手指的像素数 但它需要是垂直的还是水平的,这取决于用户是将手指从屏幕的左到右滑动,还是从屏幕的上到下滑动 线条不应倾斜或任何其他形状。它必须是直的 我看过一些文章,上面说“OpenGL”是唯一的方法 我曾尝试自己编写代码,但当我将方向从垂直方向更改为水平方向或从水平方向更改为垂直方向时,在水平线结束和垂直线开始之间会有一些额外的线或间隙: 在头文件中: @interface ViewController : UIViewCon
@interface ViewController : UIViewController
{
BOOL mouseSwiped;
CGPoint lastPoint;
CGPoint previousPoint;
UIBezierPath *currentPath;
CGFloat lastPointY;
CGFloat lastPointX;
BOOL xchanging;
BOOL ychanging;
NSMutableArray *arrayTouch;
}
@property (weak, nonatomic) IBOutlet UIImageView *drawImage;
@property UIBezierPath *currentPath;
@end
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
[arrayTouch removeAllObjects];
return;
}
lastPoint = [touch locationInView:self.view];
[arrayTouch addObject:touch];
lastPointY = lastPoint.y;
lastPointX = lastPoint.x;
lastPoint.y -= 1;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 1;
int currentPointXVal = currentPoint.x;
int currentPointYVal = currentPoint.y;
int lastPointXVal = lastPoint.x;
int lastPointYVal = lastPoint.y;
int diffX = abs(currentPointXVal - lastPointXVal);
int diffY = abs(currentPointYVal - lastPointYVal);
if(currentPoint.x > lastPoint.x && diffX > diffY)
{
if(ychanging == YES)
{
lastPointY = currentPoint.y;
lastPointX = currentPoint.x;
ychanging = NO;
}
xchanging = YES;
NSLog(@"x increasing");
NSLog(@"currentPoint: %@",NSStringFromCGPoint(currentPoint));
NSLog(@"lastPoint: %@",NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPointY);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, lastPointY);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
if(currentPoint.y > lastPoint.y && diffY > diffX)
{
if(xchanging == YES)
{
lastPointY = currentPoint.y;
lastPointX = currentPoint.x;
xchanging = NO;
}
ychanging = YES;
NSLog(@"y increasing");
NSLog(@"currentPoint: %@",NSStringFromCGPoint(currentPoint));
NSLog(@"lastPoint: %@",NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPointX, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPointX, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
if(currentPoint.x < lastPoint.x && diffX > diffY)
{
if(ychanging == YES)
{
lastPointY = currentPoint.y;
lastPointX = currentPoint.x;
ychanging = NO;
}
xchanging = YES;
NSLog(@"x decreasing");
NSLog(@"currentPoint: %@",NSStringFromCGPoint(currentPoint));
NSLog(@"lastPoint: %@",NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPointY);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, lastPointY);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
if(currentPoint.y < lastPoint.y && diffY > diffX)
{
if(xchanging == YES)
{
lastPointY = currentPoint.y;
lastPointX = currentPoint.x;
xchanging = NO;
}
ychanging = YES;
NSLog(@"y decreasing");
NSLog(@"currentPoint: %@",NSStringFromCGPoint(currentPoint));
NSLog(@"lastPoint: %@",NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPointX, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPointX, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
lastPoint = currentPoint;
}
在实现文件中:
@interface ViewController : UIViewController
{
BOOL mouseSwiped;
CGPoint lastPoint;
CGPoint previousPoint;
UIBezierPath *currentPath;
CGFloat lastPointY;
CGFloat lastPointX;
BOOL xchanging;
BOOL ychanging;
NSMutableArray *arrayTouch;
}
@property (weak, nonatomic) IBOutlet UIImageView *drawImage;
@property UIBezierPath *currentPath;
@end
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
[arrayTouch removeAllObjects];
return;
}
lastPoint = [touch locationInView:self.view];
[arrayTouch addObject:touch];
lastPointY = lastPoint.y;
lastPointX = lastPoint.x;
lastPoint.y -= 1;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 1;
int currentPointXVal = currentPoint.x;
int currentPointYVal = currentPoint.y;
int lastPointXVal = lastPoint.x;
int lastPointYVal = lastPoint.y;
int diffX = abs(currentPointXVal - lastPointXVal);
int diffY = abs(currentPointYVal - lastPointYVal);
if(currentPoint.x > lastPoint.x && diffX > diffY)
{
if(ychanging == YES)
{
lastPointY = currentPoint.y;
lastPointX = currentPoint.x;
ychanging = NO;
}
xchanging = YES;
NSLog(@"x increasing");
NSLog(@"currentPoint: %@",NSStringFromCGPoint(currentPoint));
NSLog(@"lastPoint: %@",NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPointY);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, lastPointY);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
if(currentPoint.y > lastPoint.y && diffY > diffX)
{
if(xchanging == YES)
{
lastPointY = currentPoint.y;
lastPointX = currentPoint.x;
xchanging = NO;
}
ychanging = YES;
NSLog(@"y increasing");
NSLog(@"currentPoint: %@",NSStringFromCGPoint(currentPoint));
NSLog(@"lastPoint: %@",NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPointX, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPointX, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
if(currentPoint.x < lastPoint.x && diffX > diffY)
{
if(ychanging == YES)
{
lastPointY = currentPoint.y;
lastPointX = currentPoint.x;
ychanging = NO;
}
xchanging = YES;
NSLog(@"x decreasing");
NSLog(@"currentPoint: %@",NSStringFromCGPoint(currentPoint));
NSLog(@"lastPoint: %@",NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPointY);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, lastPointY);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
if(currentPoint.y < lastPoint.y && diffY > diffX)
{
if(xchanging == YES)
{
lastPointY = currentPoint.y;
lastPointX = currentPoint.x;
xchanging = NO;
}
ychanging = YES;
NSLog(@"y decreasing");
NSLog(@"currentPoint: %@",NSStringFromCGPoint(currentPoint));
NSLog(@"lastPoint: %@",NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPointX, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPointX, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
lastPoint = currentPoint;
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event{
鼠标擦拭=否;
UITouch*touch=[触摸任何对象];
如果([touch tapCount]==2){
drawImage.image=nil;
[arrayTouch removeAllObjects];
返回;
}
lastPoint=[触摸位置视图:self.view];
[arrayTouch添加对象:触摸];
lastPointY=lastPoint.y;
lastPointX=lastPoint.x;
最后一点y-=1;
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{
鼠标擦拭=是;
UITouch*touch=[触摸任何对象];
CGPoint currentPoint=[触摸位置视图:self.view];
电流点y-=1;
int currentPointXVal=currentPoint.x;
int currentPointYVal=currentPoint.y;
int lastPointXVal=lastPoint.x;
int lastPointYVal=lastPoint.y;
int diffX=abs(currentPointXVal-lastPointXVal);
int diffY=abs(currentPointYVal-lastPointYVal);
if(currentPoint.x>lastPoint.x&&diffX>diffY)
{
如果(ychanging==是)
{
lastPointY=currentPoint.y;
lastPointX=currentPoint.x;
ychanging=否;
}
xchanging=是;
NSLog(@“x增加”);
NSLog(@“currentPoint:%@”,NSStringFromCGPoint(currentPoint));
NSLog(@“lastPoint:%@”,NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),1.0,0.0,0.0,1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(),lastPoint.x,lastPointY);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),currentPoint.x,lastPointY);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
}
if(currentPoint.y>lastPoint.y&&diffY>diffX)
{
如果(xchanging==是)
{
lastPointY=currentPoint.y;
lastPointX=currentPoint.x;
xchanging=否;
}
ychanging=是;
NSLog(@“y增加”);
NSLog(@“currentPoint:%@”,NSStringFromCGPoint(currentPoint));
NSLog(@“lastPoint:%@”,NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),1.0,0.0,0.0,1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(),lastPointX,lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),lastPointX,currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
}
if(currentPoint.xdiffY)
{
如果(ychanging==是)
{
lastPointY=currentPoint.y;
lastPointX=currentPoint.x;
ychanging=否;
}
xchanging=是;
NSLog(@“x递减”);
NSLog(@“currentPoint:%@”,NSStringFromCGPoint(currentPoint));
NSLog(@“lastPoint:%@”,NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),1.0,0.0,0.0,1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(),lastPoint.x,lastPointY);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),currentPoint.x,lastPointY);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
}
if(currentPoint.ydiffX)
{
如果(xchanging==是)
{
lastPointY=currentPoint.y;
lastPointX=currentPoint.x;
xchanging=否;
}
ychanging=是;
NSLog(@“y递减”);
NSLog(@“currentPoint:%@”,NSStringFromCGPoint(currentPoint));
NSLog(@“lastPoint:%@”,NSStringFromCGPoint(lastPoint));
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
CGContextSetLineCap(UIG