我正试着用手指在屏幕上画箭头。这个想法是,通过触摸屏幕,我设置了箭头的初始坐标,当我在屏幕上拖动时,箭头将延伸并跟随我的手指。箭头的高度和宽度将是相同的,重要的是箭头的大小。当我将箭头拖离起点时,箭头会变长。我试着这样做:
- (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中移动的触摸代码,屏幕上确实会出现箭头,但这不是我的想法。我还没有设法让这段代码工作,但我认为即使它工作了,它也会导致崩溃,因为我每次都会删除并重绘形状。这是正确的方法吗?我该怎么办?
发布于 2013-01-23 04:41:30
基本上,有一些不同的选择。
第二个我将在这里简要描述:
检测事件的类需要一个成员变量/属性,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];
}在ArrowView中:
- (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.
发布于 2013-01-23 05:22:48
你可以尝试使用UIPanGestureRecognizer来跟踪你的手指并在上面绘制。
https://stackoverflow.com/questions/14467111
复制相似问题