如何使用 UIPanGestureRecognizer 在 ios 中拖动线条
我正在编写一个程序,用户可以将一条线从一个 UILabel 拖到另一个 UILabel 上。我创建了一个名为 DragView 的 UIView
子类,并重写了 drawRect
方法。我将 UIView
设为 RootController
视图的子视图。 DragView
肯定是可见的,drawRect
方法肯定被调用,但没有任何线条可见。
这些是(我认为是)相关的代码片段。
//DragView.m
- (void)drawRect:(CGRect)rect
{
if (context == nil)
{
context = UIGraphicsGetCurrentContext();
}
if (_drawLineFlag)
{
CGContextSetLineWidth(context,2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0,0.0,1.0,1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context,color);
CGContextMoveToPoint(context,_startX, _startY);
CGContextAddLineToPoint(context,_currentX,_currentY);
CGContextStrokePath(context);
}
}
DrawProgramAppDelegate
- (void) initializeUI
{
.....
dragView = [[DragView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)] ;
[view addSubview: dragView];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[dragView addGestureRecognizer:panGestureRecognizer];
.....
}
事件处理程序是:
- (void) handlePan:(UIPanGestureRecognizer *) recognizer
{
CGPoint point = [recognizer translationInView:dragView];
[dragView setCurrentX:point.x];
[dragView setCurrentY:point.y];
[dragView setDrawLineFlag:YES];
[view bringSubviewToFront:drawView];
[dragView drawRect:CGRectMake (0.0f,0.0f,768.0f, 1024.0f)];
}
非常感谢您的帮助。
乔恩
I am writing a program where the user will drag a line from one UILabel
to another. I created a UIView
subclass called DragView, and overrode the drawRect
method. I made the UIView
a subview of the RootController
view. The DragView
is definitely visible, the drawRect
method is definitely being called, but no line is visible.
These are (what I think are) the relevant pieces of code.
//DragView.m
- (void)drawRect:(CGRect)rect
{
if (context == nil)
{
context = UIGraphicsGetCurrentContext();
}
if (_drawLineFlag)
{
CGContextSetLineWidth(context,2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0,0.0,1.0,1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context,color);
CGContextMoveToPoint(context,_startX, _startY);
CGContextAddLineToPoint(context,_currentX,_currentY);
CGContextStrokePath(context);
}
}
DrawProgramAppDelegate
- (void) initializeUI
{
.....
dragView = [[DragView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)] ;
[view addSubview: dragView];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[dragView addGestureRecognizer:panGestureRecognizer];
.....
}
The event handler is:
- (void) handlePan:(UIPanGestureRecognizer *) recognizer
{
CGPoint point = [recognizer translationInView:dragView];
[dragView setCurrentX:point.x];
[dragView setCurrentY:point.y];
[dragView setDrawLineFlag:YES];
[view bringSubviewToFront:drawView];
[dragView drawRect:CGRectMake (0.0f,0.0f,768.0f, 1024.0f)];
}
Many thanks for your help.
Jon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,你正在尝试绘制一条路径(你应该看看这个 帖子)。在这里,您似乎只是从最后一个点到当前点画一条线
,考虑到调用的频率只会画一条很小的线。
您不应该调用drawRect:
您应该告诉系统视图需要显示
您还应该检查手势识别器状态,定义为
并且可能仅在状态为
UIGestureRecognizerStateChanged
或UIGestureRecognizerStateEnded
时绘制>It seems to me you are trying to draw a path (you should take a look at this post). Here it seems you just draw a line from the last point to the current point
which given the frequency of the call would only draw a tiny line.
You should not call drawRect:
You should tell the system the view needs displaying
Also you should check the gesture recognizer state, defined as
And possibly only draw if the state is
UIGestureRecognizerStateChanged
orUIGestureRecognizerStateEnded