2

我正在尝试用手指在屏幕上绘制箭头。我的想法是通过触摸屏幕设置箭头的初始坐标,当我在屏幕上拖动时,箭头会延伸并跟随我的手指。箭头的高度和宽度将相同,重要的是箭头的大小。当我将它拖离起点时,箭头会变长。我试过用这样的东西东它:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIGraphicsBeginImageContext(CGSizeMake(1536, 2048));

    UITouch *touch = [touches anyObject];
    CGPoint p1 = [touch locationInView:self.view];

    CGSize size;
    size.width = 50;
    size.height = 400;

    CGContextRef context = UIGraphicsGetCurrentContext();

    [self drawArrowWithContext:context atPoint:p1 withSize:size lineWidth:4 arrowHeight:20 andColor:[UIColor whiteColor]];

    // converts your context into a UIImage
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // Adds that image into an imageView and sticks it on the screen.
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:imageView];
}

- (void) drawArrowWithContext:(CGContextRef)context atPoint:(CGPoint)startPoint withSize: (CGSize)size lineWidth:(float)width arrowHeight:(float)aheight andColor:(UIColor *)color
{
    float width_wing = (size.width - width) / 2;
    float main = size.height-aheight;

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextSetStrokeColorWithColor(context, [color CGColor]);

    CGPoint rectangle_points[] = {
        CGPointMake(startPoint.x + width_wing, startPoint.y + 0.0),
        CGPointMake(startPoint.x + width_wing, startPoint.y + main),
        CGPointMake(startPoint.x + 0.0, startPoint.y + main), // left point
        CGPointMake(startPoint.x + size.width / 2, startPoint.y + size.height),

        CGPointMake(startPoint.x + size.width, startPoint.y + main), // right point

        CGPointMake(startPoint.x + size.width-width_wing, startPoint.y + main),

        CGPointMake(startPoint.x + size.width-width_wing, startPoint.y + 0.0),
        CGPointMake(startPoint.x + width_wing, startPoint.y + 0.0),
    };

    CGContextAddLines(context, rectangle_points, 8);

    CGContextFillPath(context);
}

如果我从在普通 IBOutlet 中移动的触摸运行代码,箭头确实会出现在屏幕上,但这不是我的想法。我还没有设法让这段代码工作,但我认为即使它工作,它也会导致崩溃,因为我每次都在删除和重绘形状。这是正确的方法吗?我应该怎么办?

4

2 回答 2

1

基本上,有一些不同的选择。

  • 坚持使用 UIImageView 方法并一直停止重新创建图像视图。在检测触摸事件的类中保留对该 UIImageView 的引用,如果发生更改,只需替换图像。如果您需要图像用于其他用途,可能值得为屏幕外绘图付出额外的努力。
  • 在额外的视图中动态实现箭头绘制。

我将在这里简要介绍第二个:

检测事件的类需要一个成员变量/属性,ArrowView *arrowView 和一些记住起点的东西,也许是 CGPoint startPoint。ArrowView 需要箭头参数的属性,CGPoint arrowStart,CGSize arrowSize。在触摸事件处理程序中,执行

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint position = [touch locationInView:self.view];

    [self.startPoint startFrom:position];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint next = [touch locationInView:self.view];

    CGSize size;
    size.width = next.x - self.startPoint.x;
    size.height = next.y - self.startPoint.y;

    self.arrowView.arrowPoint = self.startPoint;
    self.arrowView.arrowSize = size;

    [self.arrowView setNeedsDisplay];
}

在箭头视图中:

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    // Now do the drawing stuff here using that context!
    // self.arrowSize / self.arrowPoint do contain your drawing parameters.
    ...
}

我希望这能给你一个想法。

如需进一步阅读,请从这里开始。

于 2013-01-22T20:41:30.383 回答
0

哟可以尝试使用UIPanGestureRecognizer跟随你的手指并在它上面画图。

于 2013-01-22T21:22:48.207 回答