【问题标题】:Making a constant line between a dot and your finger在一个点和你的手指之间画一条恒定的线
【发布时间】:2012-08-07 12:15:40
【问题描述】:

我想做与 Scalar 应用程序类似的事情,他们能够从一个点拖动到记事本上,将数字粘贴到他们拖动点的位置。我真正感兴趣的是与点和手指保持连接的线,如下所示:

我的问题是我真的不知道这叫什么,所以我在寻找如何做到这一点时遇到了麻烦。有谁知道这会被称为什么,他们有没有遇到过关于这个主题的教程?如果你有一些代码让我看,那就更好了。

谢谢。

【问题讨论】:

标签: objective-c ios xcode


【解决方案1】:

当手指在其上拖动时绘制线条并检测到要触摸的第一个视图的 UIView 示例应该可以帮助您开始。

//this goes in the header file called "UILineView.h"

#import <UIKit/UIKit.h>

@interface UILineView : UIView

@end

//this in the implementation file called "UILineView.m"

#import "UILineView.h"

@implementation UILineView

{
    CGPoint _originOfTouchPoint; // your fist touch detected in touchesBegan: method
    CGPoint _currentFingerPositionPoint; // the position you have dragged your finger to
    CGFloat _strokeWidth; // the width of the line you wish to draw
    id _touchStartedObject; // the object(UIView) that the first touch was detected on
}

// If you use Interface Builder to design your interface, Objects in a nib file are reconstituted and then initialized using
// their initWithCoder: method
- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super initWithCoder:decoder];
    if (self) {
        // Initialization code
        _originOfTouchPoint = CGPointMake( 0.0, 0.0 );
        _currentFingerPositionPoint = CGPointMake( 100.0, 100.0 );
        _strokeWidth = 2.0;
    }
    return self;
}
/*
// Use initWithFrame if you are not loding the UIView from a nib file
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _originOfTouchPoint = CGPointMake( 0.0, 0.0 );
        _currentFingerPositionPoint = CGPointMake( 100.0, 100.0 );
        _strokeWidth = 2.0;
    }
    return self;
}
*/

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context    = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor( context, [UIColor blueColor].CGColor );
    CGContextSetLineWidth( context, _strokeWidth );
    // fisrt point of line
    CGContextMoveToPoint( context, _originOfTouchPoint.x, _originOfTouchPoint.y );
    // last point of line
    CGContextAddLineToPoint( context, _currentFingerPositionPoint.x, _currentFingerPositionPoint.y );
    // draw the line
    CGContextStrokePath( context );
}

#pragma mark touches

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get starting point and first view touched (if you need to send that view messages)
    _originOfTouchPoint = [[touches anyObject] locationInView:self];
    _touchStartedObject = [[touches anyObject] view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint movedToPoint = [[touches anyObject] locationInView:self];
    // if moved to a new point redraw the line 
    if ( CGPointEqualToPoint( movedToPoint, _currentFingerPositionPoint ) == NO )
    {
        _currentFingerPositionPoint = movedToPoint;
        // calls drawRect: method to show updated line
        [self setNeedsDisplay];
    }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // reset values
    _originOfTouchPoint = CGPointZero;
    _currentFingerPositionPoint = CGPointZero;
    _touchStartedObject = nil;
}

@end

【讨论】:

  • 问题:如果我想限制它只在我点击某个位置或点击一个按钮时工作,我该怎么办?
  • 您可以在 touchesBegun: 中读取 _touchStartedObject:如果它是您想要的对象(UIButton 或 UIView),您可以将布尔变量设置为 YES。检查此变量在 drawRect 中是否设置为 YES:
  • - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // get starting point and first view touched (if you need to send that view messages) _originOfTouchPoint = [[touches anyObject] locationInView:self]; _touchStartedObject = [[touches anyObject] view]; if ( [_touchStartedObject isEqual:someUIButtonOrView] ) { _shouldDrawLine = YES; } }
  • - (void)drawRect:(CGRect)rect { if ( _shouldDrawLine ) { //.......... } }
  • 我唯一的问题是我不能在 UIView 中使用按钮。它必须在 UIViewController 上,我想不出在 ViewController 中执行相同代码的方法。
【解决方案2】:

它将涉及子类化UIView 并实现touchesBegantouchesMoved 方法,并在您的视图子类drawRect 方法中从初始触摸到当前触摸画一条线。

这是一个对How do I draw a line on the iPhone?有帮助的上一个问题

您只需要更改以下内容,以便坐标是您从 touches 方法获得的初始触摸和当前触摸的坐标

CGContextMoveToPoint(c, 5.0f, 5.0f);
CGContextAddLineToPoint(c, 50.0f, 50.0f);

然后在touchesMoved 方法中调用[self setNeedsDisplay]; 将在您移动时重新绘制线条以跟随您的手指。

然后在手指离开时实现touchesEnded代码。

希望对你有帮助!

【讨论】:

    【解决方案3】:

    UIGestureRecognizer 可以做到这一点,而不会混淆触摸和拖动逻辑与视图。我已将它们用于编辑地图上的形状、长按拖动等。请参阅this tutorial

    【讨论】:

      猜你喜欢
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多