开发者

UIScrollView does not respond to touches in iPhone

开发者 https://www.devze.com 2023-01-13 17:55 出处:网络
I have a scroll view with images inside it and I\'m us开发者_运维问答ing touhesBegan, touchesMoved, and touchesEnded to tap on an image to enlarge it. But It does not seem that the scroll view is resp

I have a scroll view with images inside it and I'm us开发者_运维问答ing touhesBegan, touchesMoved, and touchesEnded to tap on an image to enlarge it. But It does not seem that the scroll view is responding to touches at all.

I tried most of methods in similar questions but still does not work.

I have been working on that for long so please any help would be appreciate it. Thanks,


Did you link them correctly in Interface Builder? Did you add the proper delegates? If not, then link the scrollview to your view controller in Interface Builder and make sure you view controller has the proper delegates added.


A UIScrollView handles certain touches and does not pass these events down to included views. If you subclass the scroll view, then you can inspect events and handle or pass the appropriate ones down the responder chain. But you can't handle every touch event, or the scrollview itself will stop working (scrolling, zooming, etc.)


Did you create a custom imageview class to handle the touches? If not, those methods will not work as you want them to.

What you have to do is create a custom subclass of a UIImageView, and then use delegation to communicate back to the scrollview what happened:

Ex: MyImageView.h


@protocol MyImageViewDelegate
- (void)imageTouched:(UIImage *)img;
@end
@interface MyImageView : UIImageView {
    id *MyImageViewDelegate* delegate;
}
@property (nonatomic, assign) id *MyImageViewDelegate* delegate;
@end

(replace *'s with <'s and >'s respectively, the code box thinks its html and tries to render it)

And MyImageView.m


@implementation MyImageView
@synthesize delegate;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
} 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [delegate imageTouched:self.image];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
@end

When you alloc/init a MyImageView make sure to set the delegate to the scrollview. Then on the scrollview page, put the imageTouched: method, and handle the popup there.

0

精彩评论

暂无评论...
验证码 换一张
取 消