【问题标题】:iOS - How To Draw Point On A Line In Which The User Touches When DraggingiOS - 如何在用户拖动时触摸的线上绘制点
【发布时间】:2016-05-20 08:31:37
【问题描述】:

我有一个矩形视图,其中包含我以编程方式绘制的垂直细线。

这是我用来画线的代码:

- (void)drawLines
{
    CGFloat spacing = _bgView.frame.size.width / 11.0f;

    for(int i = 1; i < 11; i++)
    {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake((CGFloat)(i * spacing), 0, 1, _bgView.frame.size.height)];
        line.backgroundColor = [UIColor whiteColor];
        line.tag = i;

        [_bgView addSubview:line];
    }
}

线条由根据矩形视图的宽度计算的均匀分布的空间分隔(_bgView)。

下面是它的外观概览:

当用户在矩形视图中执行拖动操作时,当他/她的手指穿过或触摸一条线时,将在该特定点上绘制一个点。

这是检测用户在线上的触摸点并在该点上绘制一个点的代码。

- (IBAction)drawDots:(id)sender
{
    UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)sender;

    CGPoint pannedPoint = [pan locationInView:_bgView];

    for (UIView *subview in _bgView.subviews)
    {
        if (CGRectContainsPoint(subview.frame, pannedPoint) && subview.tag > 0)
        {
            NSLog(@"Point x:%.2f y:%.2f TAG:%i", pannedPoint.x, pannedPoint.y, subview.tag);

            UIView *point = [[UIView alloc] initWithFrame:CGRectMake(pannedPoint.x - 2.5, pannedPoint.y - 2.5, 5, 5)];
                point.backgroundColor = [UIColor redColor];

            [_bgView addSubview:point];
        }
    }
}

现在,如果用户平移速度非常快,结果如下:

可以看出,只画了一些点,这就是为什么其他线上有很多缺失的点。

这应该是预期的结果:

但是,这仅在用户平移超慢时才会发生。

即使用户的手指移动得非常快,如何在用户触摸截取的线条中绘制点?

谢谢!

【问题讨论】:

  • 您可以使用在平移手势动作之前调用的委托方法
  • @PKT 谢谢!我试试看。
  • @PKT 你好,我只是想问一下,你指的是 UIGestureRecognizerDelegate 吗?如果是这样,那么我看不到任何可以使用的委托方法。
  • 根据要求,您应该使用 touchesBegan:withEvent: - touchesMoved:withEvent: - touchesEnded:withEvent: - touchesCancelled:withEvent: 方法作为@Alex Answer
  • 立即查看答案

标签: ios objective-c uipangesturerecognizer


【解决方案1】:

您应该使用 UIResponder 方法来检测用户触摸,而不是平移手势:

- touchesBegan:withEvent:
- touchesMoved:withEvent:
- touchesEnded:withEvent:
- touchesCancelled:withEvent:

第二种方法 - touchesMoved:withEvent: 当用户在屏幕上不断移动手指时调用,这样你就可以得到它的手指位置:

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];    

然后只需添加您的检测逻辑。

查看文档了解更多详情: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/

【讨论】:

  • 谢谢!我会试试的。
  • 嗨,它没有按我的预期工作。结果和我现在面临的一样。
【解决方案2】:

请参考此代码

满足您的要求

  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

//    NSLog(@"DEBUG: Touches moved" );

[super touchesMoved:touches withEvent:event];

if ([[event allTouches] count] > 1) {

} else {

    UITouch *touch = [[event allTouches] anyObject];
    UIView *view = touch.view;


    NSLog(@"DEBUG: Touches Moved");

   CGPoint location = [touch locationInView:self.bgView];
    for (UIView *subview in _bgView.subviews)
    {
        if (CGRectContainsPoint(subview.frame, location) && subview.tag > 0) {

            NSLog(@"Point x:%.2f y:%.2f TAG:%i", location.x, location.y, subview.tag);

            UIView *point = [[UIView alloc] initWithFrame:CGRectMake(location.x - 2.5, location.y - 2.5, 5, 5)];
            point.backgroundColor = [UIColor redColor];

            [_bgView addSubview:point];

        }


    }





    }
}

会帮助你

【讨论】:

  • 那个 imageView 是干什么用的?那代表线条吗?
  • 这只是一个例子,我正在屏幕上移动图像视图
  • 该代码可以工作,但它仍然与我编写的代码没有什么不同。当用户快速绘制时,很多点无法检测到。
猜你喜欢
  • 1970-01-01
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
相关资源
最近更新 更多