1

ViewController を作成し、Custom クラスで 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

これを試してください:

-(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 に答える
0

-[UIView setUserInteractionEnabled:]デフォルトではNOでタッチを有効にすることができますUIImageView

描画について...ドキュメントから:

UIImageView クラスは、その画像をディスプレイに描画するように最適化されています。UIImageView は drawRect: サブクラスを呼び出しません。サブクラスにカスタム描画コードが必要な場合は、UIView を基本クラスとして使用することをお勧めします。

于 2013-03-19T10:32:51.103 に答える