我想拖动由Me完成的绘制线。下面的代码片段在堆栈溢出用户回答的帮助下正常工作。
//我在.h文件中的实例
CGPoint location1,location2;
LineView* l;
- (void)viewDidLoad
{
[super viewDidLoad];
l = [[LineView alloc]initWithFrame:self.view.frame];
[self.view addSubview:l];
UIPinchGestureRecognizer *linepinch = [[UIPinchGestureRecognizer alloc]
initWithTarget:l action:@selector(handleLinePinch:)];
[l addGestureRecognizer:linepinch];
}
- (void)handleLinePinch:(UIPinchGestureRecognizer *)gesture
{
NSUInteger num_touches = [gesture numberOfTouches];
// save locations to some instance variables, like `CGPoint location1, location2;`
if (num_touches >= 1) {
location1 = [gesture locationOfTouch:0 inView:l];
}
if (num_touches >= 2) {
location2 = [gesture locationOfTouch:1 inView:l];
}
[l drawRect:location1 Loc2:location2];
[l setNeedsDisplay];
}
LineView.m
- (void)drawRect:(CGPoint)location1 Loc2:(CGPoint)location2 {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5.0);
CGContextMoveToPoint(context, location1.x, location1.y);
CGContextAddLineToPoint(context, location2.x, location2.y);
CGContextStrokePath(context);
}
上面的代码片段工作正常,但我想提前使用UITapGeustureRecognizer或someother.thanks拖动此行。