0

ユーザーの描画方法に関係なく、iOS で直線を描画する方法。

ユーザーが指をドラッグするピクセル数の長さの線にしたい。

ただし、ユーザーが指を画面の左から右にスライドするか、画面の上から下にスライドするかに基づいて、垂直または水平にする必要があります。

線は斜めまたはその他の形状であってはなりません。それはまっすぐである必要があります。

「Open GL」が唯一の方法であるという記事を読みました。

私はそれを自分でコーディングしようとしましたが、方向を垂直から水平に、またはその逆に変更すると、水平線の終了位置と垂直線の開始位置の間に余分な線またはギャップが発生します。

ヘッダー ファイル内:

@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;

}

OpenGLなしでそれを行う方法はありますか?

4

1 に答える 1

3

確かに-これを行うことができます。線から勾配を取り除くだけです:

@implementation LineDrawingView
{
    CGPoint touchStart;
    CGPoint touchCurrent;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        touchStart   = (CGPoint) { -1, -1 };
        touchCurrent = (CGPoint) { -1, -1 };
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    if (touchStart.x != -1)
    {
        // calculate the line rotation
        enum { horizontal, vertical } lineRot = horizontal;

        if (touchStart.y == touchCurrent.y)
        {
            // straight line
            lineRot = horizontal;
        }
        else
        {
            // calculate the slope
            double slope = fabs((touchCurrent.y - touchStart.y) / (touchCurrent.x - touchStart.x));

            if (slope > 1)
                lineRot = vertical;
        }

        // draw the actual line
        CGPoint lineEndPoint = { };

        if (lineRot == horizontal)
            lineEndPoint = (CGPoint) { touchCurrent.x, touchStart.y }; // horizontal line
        else
            lineEndPoint = (CGPoint) { touchStart.x, touchCurrent.y }; // vertical line

        // actually draw the line
        [[UIColor redColor] setStroke];

        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:touchStart];
        [path addLineToPoint:lineEndPoint];

        [path stroke];
    }
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchStart = [[touches anyObject] locationInView:self];
    touchCurrent = touchStart;
    [self setNeedsDisplay];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchCurrent = [[touches anyObject] locationInView:self];
    [self setNeedsDisplay];
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchStart = touchCurrent = (CGPoint) { -1, -1 };
    [self setNeedsDisplay];
}

@end

必要に応じて、これを拡張して行の配列を使用できます。これは簡単な説明です。

于 2012-08-21T12:53:30.797 に答える