i have an uiscrollview with a uiimageview inside. I subclass the uiscrollview but i can not ge开发者_运维知识库t 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]);
}
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.
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
Make sure you turn on userInteractionEnabled in the UIImageView (it's off by default for those).
Read up on Event Handling and see if you can't figure anything out from that. You also might try canBecomeFirstResponder
and becomeFirstResponder
.
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)?
精彩评论