Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 07-02-2009, 08:19 PM   #17 (permalink)
stlau
Registered Member
 
Join Date: Feb 2009
Posts: 87
Default

Quote:
Originally Posted by elpuerco View Post
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 ;-)
I tried. but it seems the imageview still cannot reponse the multi-touch message?
__________________
iCurrencyX - Free currency converter (140 currencies & 3 metals)
AppRank & AppRank Pro - View app rankings over the world.
AppRelease
- Track your App Release Dates(Source Code provided).
LY MobileSoft on iTunes Store
stlau is offline   Reply With Quote
 
Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 23,966
Threads: 38,751
Posts: 170,022
Top Poster: smasher (2,560)
Welcome to our newest member, cruisetom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:46 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.