gpt4 book ai didi

ios - 使用 TouchesMoved 绘制形状

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:34 26 4
gpt4 key购买 nike

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

- (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 中移动的触摸运行代码,箭头确实会出现在屏幕上,但这不是我的想法。我还没有设法让这段代码工作,但我认为即使它工作了,也会导致崩溃,因为我每次都删除并重新绘制形状。这是正确的方法吗?我应该怎么办?

最佳答案

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

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

第二个我将在这里简要描述:

检测事件的类需要一个成员变量/属性,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];
}

在箭头 View 中:

- (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.
...
}

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

如需进一步阅读,start here.

关于ios - 使用 TouchesMoved 绘制形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14467111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com