1
votes

I have an image inside of a UIScrollView and I have code which draws text as the user drags their finger around. I'd like to be able to control, when the user is touching the screen, if

a) the user should be using their finger to draw OR

b) the user should be moving the scroll view around with their finger

I have a boolean which keeps track of what the user is doing. And I have touches methods:

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

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    if(draw == false) {
        printf("calling super");
        [scrollView touchesBegan:touches withEvent:event];
    }
    else
        [myPath moveToPoint:[mytouch locationInView:self]];

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

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    if(draw == false)
        [scrollView touchesMoved:touches withEvent:event];
    else {
        [myPath addLineToPoint:[mytouch locationInView:self]];
        [self setNeedsDisplay];
    }

Why doesn't this work? Basically, if I'm not drawing, I call the touchesBegan and touchesMoved of the scroll view. If I am drawing, I draw using myPath.

However, the scroll view is not moved around or zoomed in, etc, as it should be, when draw is false.

2

2 Answers

1
votes

i have met this problem before. And i solve like this:

you should make a mainView . It has 2 property. One is yourScrollView , and One is yourDrawImageView. [yourScrollView addSubviews:yourDrawImageView];[mainView addSubviews:yourScrollView];

and then write your touches method in the mainView.m like this (ignor the scrollView statment)

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

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
   if ([[touches allObjects] isKindOfClass:[yourDrawImageView class]]) 
   {

        [myPath moveToPoint:[mytouch locationInView:self]];
   }

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

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
   if ([[touches allObjects] isKindOfClass:[yourDrawImageView class]]) 
   {

        [myPath addLineToPoint:[mytouch locationInView:self]];
        [self setNeedsDisplay];
   }


}

The last step , whats you ignor is to code scrollView touches event , its write in yourSceollView.m,like this:

#import "yourScrollView.h"


@implementation yourScrollView


- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    if(!self.dragging)
        [[self nextResponder] touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    if(!self.dragging)
        [[self nextResponder] touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    if(!self.dragging)
        [[self nextResponder] touchesEnded:touches withEvent:event];
}

- (void)dealloc {
    [super dealloc];
}


@end 

I wish it can help you :-)

0
votes

UIScrollView uses UIGestureRecognizers, which are called by the system separately from touchesBegan:withEvent: and friends. An easier way to do this is to simply disable scrolling on the scroll view when you don't want it to react to touches.