2

I need to make an UIImageView, on a scroll View, active for touching. is a little UiimageView 100x50 were people can make the signature.

I am using the same code and it works, but with scroll view it doesn't. I looked around, but I could not find a code for that.

.h

UIImageView *drawImage;

@property (nonatomic, retain) IBOutlet UIImageView *drawImage;

.m

    @synthesize drawImage;

    - (void)viewDidLoad    {
    [scrollView setScrollEnabled:YES];
    [scrollView setContentSize:CGSizeMake(320,1540)];
    if ([MFMailComposeViewController canSendMail])
        button.enabled = YES;
    drawImage.userInteractionEnabled = YES;


    UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchesBegan:)];
    gestureRec.numberOfTouchesRequired = 1;
    gestureRec.numberOfTapsRequired = 1;
    [drawImage addGestureRecognizer:gestureRec];
    [gestureRec release];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(printItem) forControlEvents:UIControlEventTouchDown];
    btn.frame = CGRectMake(75, 0, 44, 44);
   [btn setImage:[UIImage imageNamed:@"print.png"] forState:UIControlStateNormal];

    [self.view addSubview:btn];

}

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

        mouseSwiped = NO;
        UITouch *touch = [touches anyObject];

        if ([touch tapCount] == 3) {
            drawImage.image = nil;
            return;
        }

        lastPoint = [touch locationInView:drawImage];
        //  lastPoint.y -=20;// only if signature goes bottom of the screen
    }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        mouseSwiped = YES;

        UITouch *touch = [touches anyObject];   
        CGPoint currentPoint = [touch locationInView:drawImage];
        currentPoint.y -=5; // only if signature goes bottom of the screen


        UIGraphicsBeginImageContext(drawImage.frame.size);

        CGContextRef currentContext = UIGraphicsGetCurrentContext();
        [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
        CGContextSetLineCap(currentContext, kCGLineCapRound);
        CGContextSetLineWidth(currentContext, 2.0);
        CGContextSetRGBStrokeColor(currentContext, 0.0, 0.0, 0.0, 1.0);
        CGContextBeginPath(currentContext);
        CGContextMoveToPoint(currentContext, lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(currentContext, currentPoint.x, currentPoint.y);
        CGContextStrokePath(currentContext);
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        lastPoint = currentPoint;
    }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

        UITouch *touch = [touches anyObject];

        if ([touch tapCount] == 3) {
            drawImage.image = nil;
            return;
        }

        if(!mouseSwiped) {
            UIGraphicsBeginImageContext(drawImage.frame.size);

            CGContextRef currentContext = UIGraphicsGetCurrentContext();
            [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
            CGContextSetLineCap(currentContext, kCGLineCapRound);
            CGContextSetLineWidth(currentContext, 3.0);
            CGContextSetRGBStrokeColor(currentContext, 1.0, 1.0, 2.0, 2.0);
            CGContextMoveToPoint(currentContext, lastPoint.x, lastPoint.y);
            CGContextAddLineToPoint(currentContext, lastPoint.x, lastPoint.y);
            CGContextStrokePath(currentContext);
            drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();
        }
    }

I tried the NSZombieEnabled and I get a SIGABRIT on the UITouch line on touchesBegan

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

    mouseSwiped = NO;

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 3) {
        drawImage.image = nil;
        return;
    }

    lastPoint = [touch locationInView:drawImage];
    //  lastPoint.y -=20;// only if signature goes bottom of the screen
}

and a SIGABRIT here, in the int main(int argc, char *argv[]) line

    #import <UIKit/UIKit.h>

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

I still get the error like before:

2011-05-10 22:14:22.509 anamnesiprova[90427:207] -[UITapGestureRecognizer anyObject]: unrecognized selector sent to instance 0x6bd5ba0

2011-05-10 22:14:22.512 anamnesiprova[90427:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer anyObject]: unrecognized selector sent to instance 0x6bd5ba0'
cyberlobe
  • 1,783
  • 1
  • 18
  • 30
Marco
  • 149
  • 1
  • 4
  • 12
  • anyone please? what's wrong with the code. thanks – Marco May 06 '11 at 22:20
  • +1 for tidying up, thanks again. – Nick Weaver May 08 '11 at 20:58
  • Okay, can you enable NSZombieEnabled for your active executable and see if something new turns up instead of `unrecognized selector sent to instance 0x68adc80` or alike? – Nick Weaver May 08 '11 at 21:01
  • I tried to enable NSZombieEnabled: product/profile/ and in argument in the Enviromental Variables I out NSZombieEnabled YES, but it did not work :(, I do not now what is it this NSZombie, so probably I need to activate it in a different way. thanks again for your help – Marco May 09 '11 at 20:36
  • Have a look at [this Q&A](http://stackoverflow.com/questions/1211923/how-to-use-nszombie-in-xcode) and this [tutorial](http://iphonedevelopertips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html) it'll help you to get the hang of it. – Nick Weaver May 09 '11 at 20:54
  • @Nick, hello again. I tried different thing with this code and I noticed that. when the source is like -(void) somethig {} it works always, but when the source is like -(void) something : (id) sender {} it never works. – Marco May 13 '11 at 11:18
  • @Marco yes that's because you have to add a colon to the selector like: `@selector(myMethodToBeCalled:)` this tells the selector to choose a method which expects one parameter. With this you can pass the `(id)sender` – Nick Weaver May 13 '11 at 18:04
  • @Nick, sorry can I put the @ when I put a reply ? I do not want to annoy to much. I added the colon but it crashes, actually the action I am using is this, with NSSet and UIEvent. can it be the problem? -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event – Marco May 13 '11 at 22:19
  • right I think I have to crate an IBAction instead that right, but how can I use touchesbegan as IBAction? tahnks -(IBAction)touchesbegan:(id)sender; – Marco May 14 '11 at 12:37
  • @Marco An IBAction is for the action target pattern, for example used with the UIButton, it's meant for controls that inherit from UIControl. UIImageView is not suitable for that. Use a gesture recognizer or subclassing and overwriting the touches methods. – Nick Weaver May 14 '11 at 12:55
  • @NicK, I am still having problem with this code. anyway I noticed that when you touch with one finger the scrollview you can hold the scrolling, so is it possible stop the scrolling with one finger and use a second finger for writing? – Marco May 21 '11 at 08:42
  • @Marco I don't know, I haven't tried that before. But please let us know if you try and what came out. – Nick Weaver May 21 '11 at 08:50

1 Answers1

0

I think you have to set userInteractionEnabled of your UIImageView to YES.

Another idea would be to add a GestureRecognizer to your imageView, for the beginning a simple one:

UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myMethodToBeCalled)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[myImageView addGestureRecognizer:gestureRec];
[gestureRec release];
Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
  • thanks Nick, I set the userInteractionEnable YES, but still I do not have any responses from UIImageView. – Marco May 07 '11 at 21:25
  • 1
    @Marco try to set delaysContentTouches of the UIScrollView to NO. If you put NSLogs in your touch methods do they get called at all? I've added some code for a gesture recognizer, try this. – Nick Weaver May 07 '11 at 21:27
  • that's very kind of you. I tried your code, but as soon as I touch my UImageView the app crashes and I get this error: – Marco May 08 '11 at 11:37
  • I tried this action to check if it works with my UImageView: - (void)twoFingersTwoTaps { NSLog(@"Action: tap"); } , and it gives me the the correct action without crashing . BUt when I use the touchesbegan action it crashes. thanks for your help – Marco May 08 '11 at 20:56
  • I am trying UILongPressGestureRecognizer now, so wen u move the finger it repeat the action. I am wondering how to do an action that makes a dot every time I tap the view. -(void) drawdot: (id) sender {} any help please? – Marco May 22 '11 at 12:55
  • ok I found a solution for that -(IBAction)swoosh:(id)sender{ [scrollView setScrollEnabled:NO]; scrollView.userInteractionEnabled = NO; } – Marco Mar 03 '12 at 13:59