views:

260

answers:

4

i have an uiscrollview with a uiimageview inside. I subclass the uiscrollview but i can not get touches to work touchbegin does not called. What should i do to call uitouch event?

what is wrong?

.h

#import <UIKit/UIKit.h>

@interface myScrollView : UIScrollView {

}

@end

.m

#import "myScrollView.h"


@implementation myScrollView


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

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    if (touch.view.tag > 0) {
        touch.view.center = location;
    }

    NSLog(@"tag=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);

}
A: 

Make sure you turn on userInteractionEnabled in the UIImageView (it's off by default for those).

Ben Gottlieb
userinteraction is enabled! both on uiscrollview and uiimageview
stefanosn
are you trying to get touches for the UIImageView or the UIScrollView? I think you'll get into trouble overriding -touchesXXX: on a UIScrollView. Have you double checked that your view is getting instantiated, and not a stock UIScrollView?
Ben Gottlieb
A: 

Read up on Event Handling and see if you can't figure anything out from that. You also might try canBecomeFirstResponder and becomeFirstResponder.

David Kanarek
A: 

How do you use these view? Do you create them from IB or from the code? Things to check:

  • If you use the IB then make sure that you've set the Class Identity (in View Identity tab in Tools > Inspector) to "myScrollView"
  • If you use IB then which parameters have you change from default in both views?
  • If you create the views in the code then post your initiation code
  • Do you get any method called in "myScrollView" (initializer, for example)?
Michael Kessler
A: 

I'm having the same problem. With the 2.0+ compiler, everything works as expected, but now that I have to use a 3.0+ compiler, my UIScrollView no longer receives the touch events.

Did you ever have any luck resolving this?

UPDATE: I found the answer to the problem here:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/21374-uiscrollview-subclass-not-detecting-touches.html

basically, apple removed this ability in 3.0

"In 2.2.1 and earlier, it was possible to prevent a UIScrollView from scrolling by implementing the UIResponder touch methods in a subclass and not calling the superclass' implementation. This was never supported, and often resulted in inconsistent state on the UIScrollView, but it worked in certain specific situations "

Ty Landercasper