Hi,
have spent an age searching for the answer to this problem and found a number of references to the solution, i.e subclassing the UIScrollView class, but no examples.
I found a lot of people asking the same question as me (or rather I was asking same as them lol)
So I created an app from scratch and thought I would post the detail here for others to use. learn or flame at bad coding....but it works for me so I am happy!
OK..here goes:
Start a new project called ScrollViewTapDetectionViewController of class UIViewController.
Here is the code, read my comments...
Code:
#import
// these will be covered in a flie to come later in this tutorial :)
@class myScrollView;
@class myImageView;
// make sure you set this to be a UIScrollViewDelegate!
@interface ScrollViewTapDetectionViewController : UIViewController {
// this will be our UIScrollView subclass
myScrollView *contentView;
// this will be our UIImageView subclass
myImageView *imageView;
}
@property (retain, nonatomic) myScrollView *contentView;
@property (retain, nonatomic) myImageView *imageView;
@end
Now create another class of UIViewController and name it myScrollView
here is the code, again read my comments...
Code:
#import
// change the class to be of type UIScrollView that is all for this file :)
@interface myScrollView : UIScrollView {
}
@end
And again add another class of type UIViewController
here is the code, again read my comments...
Code:
#import
// change the class to be of type UIImageView that is all for this file :)
@interface myImageView : UIImageView {
}
@end
Now open the file myScrollView.m and ad this code, read my comments ;-)
Code:
#import "myScrollView.h"
@implementation myScrollView
// This code is from ThirtyOne's post.....thanks a million TO ;-)
-(void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
if (!self.dragging) {
[self.nextResponder touchesEnded: touches withEvent:event];
}
[super touchesEnded: touches withEvent: event];
}
Now open the file myImageView.m and add this code ;-)
Code:
#import "myImageView.h"
@implementation myImageView
// simple method you should be familiar with!
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
NSLog(@"Touch detected");
}
Now for the nitty gritty to pull it all together
Code:
// import the required header files
#import "ScrollViewTapDetectionViewController.h"
#import "myScrollView.h"
#import "myImageView.h"
@implementation ScrollViewTapDetectionViewController
@synthesize contentView;
@synthesize imageView;
// we want the instance of our myImageView to be the one used for scrolling
-(UIView *) viewForZoomingInScrollView: (UIScrollView *) ScrollView
{
return imageView;
}
-(void) loadView
{
// set the image to be displayed, pic your own image here
imageView = [[myImageView alloc] initWithImage: [UIImage imageNamed: @"AnyOldImage.png"]];
// yes we want to allow user interaction
[imageView setUserInteractionEnabled:YES];
// set the instance of our myScrollView to use the main screen
contentView = [[myScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// turn on scrolling
[contentView setScrollEnabled: YES];
// set the content size to the size of the image
[contentView setContentSize: imageView.image.size];
// add the instance of our myImageView class to the content view
[contentView addSubview: imageView];
// flush the item
[imageView release];
// set max zoom to what suits you
[contentView setMaximumZoomScale:1.0f];
// set min zoom to what suits you
[contentView setMinimumZoomScale:0.25f];
// set the delegate
[contentView setDelegate: self];
// scroll a portion of image into view (my image is very big) :)
[contentView scrollRectToVisible:CGRectMake(400, 400, 320, 440) animated:NO];
// yes to autoresize
contentView.autoresizesSubviews = YES;
// set the mask
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
// set the view
self.view =contentView;
}
Now compile and run your app and ensure you have the console open.
Drag the image around, zoom in and out etc and nothing appears in the console which is what we want. But not just click on the image and hey presto.......the text Touch detected appears
hope this helps someone
NB I have not entered the code for release instance variables etc....you can do that ;-)