1

我创建了一个 ViewController 并在自定义类上选择了 myView。在 myView 中,我工作过,当用户单击并拖动手指时,它会画一条直线,当再次拖动时,前一行消失并创建新的。

我的代码:

@implementation GestureView

{
    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

我在 ViewController 上添加了 3 个 imageView,我想在用户单击任何 imageView 时检测触摸,然后当他拖动手指时,该线开始从该 imageView 绘制。我可以在屏幕上的任何地方画线,但我想限制只在单击 imageView 并在另一个 imageView 上结束时画线

在此处输入图像描述

在此处输入图像描述

4

2 回答 2

0

您可以启用触摸,-[UIView setUserInteractionEnabled:]默认情况下它是 NO onUIImageView

关于绘图...来自文档:

UIImageView 类经过优化,可以将其图像绘制到显示器上。UIImageView 不会调用 drawRect: 一个子类。如果您的子类需要自定义绘图代码,建议您使用 UIView 作为基类。

于 2013-03-19T10:32:51.103 回答
0

试试这个:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    //To get tag of touch view
    int viewTag=[touch view].tag;

    if ([[touch view] isKindOfClass:[UIImageView class]])
    {
      //Write your code...
    }
}

并像这样设置您的所有触摸图像 UserInteractionEnabled:

[imageName setUserInteractionEnabled:YES];
于 2013-03-19T13:54:05.950 回答