如何使用 UIPanGestureRecognizer 在 ios 中拖动线条

发布于 2024-12-15 11:08:21 字数 1766 浏览 4 评论 0原文

我正在编写一个程序,用户可以将一条线从一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗笺 2024-12-22 11:08:21

在我看来,你正在尝试绘制一条路径(你应该看看这个 帖子)。在这里,您似乎只是从最后一个点到当前点画一条线

CGContextMoveToPoint(context,_startX, _startY);
CGContextAddLineToPoint(context,_currentX,_currentY);

,考虑到调用的频率只会画一条很小的线。

您不应该调用drawRect:

[dragView drawRect:CGRectMake (0.0f,0.0f,768.0f, 1024.0f)];

您应该告诉系统视图需要显示

[dragView setNeedsDisplay];

您还应该检查手势识别器状态,定义为

typedef enum {
   UIGestureRecognizerStatePossible,
   UIGestureRecognizerStateBegan,
   UIGestureRecognizerStateChanged,
   UIGestureRecognizerStateEnded,
   UIGestureRecognizerStateCancelled,
   UIGestureRecognizerStateFailed,
   UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
} UIGestureRecognizerState;

并且可能仅在状态为 UIGestureRecognizerStateChangedUIGestureRecognizerStateEnded 时绘制>

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

CGContextMoveToPoint(context,_startX, _startY);
CGContextAddLineToPoint(context,_currentX,_currentY);

which given the frequency of the call would only draw a tiny line.

You should not call drawRect:

[dragView drawRect:CGRectMake (0.0f,0.0f,768.0f, 1024.0f)];

You should tell the system the view needs displaying

[dragView setNeedsDisplay];

Also you should check the gesture recognizer state, defined as

typedef enum {
   UIGestureRecognizerStatePossible,
   UIGestureRecognizerStateBegan,
   UIGestureRecognizerStateChanged,
   UIGestureRecognizerStateEnded,
   UIGestureRecognizerStateCancelled,
   UIGestureRecognizerStateFailed,
   UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
} UIGestureRecognizerState;

And possibly only draw if the state is UIGestureRecognizerStateChanged or UIGestureRecognizerStateEnded

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文