Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 07-02-2009, 09:19 PM   #17 (permalink)
stlau
Registered Member
 
Join Date: Feb 2009
Posts: 109
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?
__________________

AppRelease
- Track your App Release Dates(Source Code provided).
LY MobileSoft on iTunes Store
stlau is offline   Reply With Quote
 

» Advertisements
» Online Users: 745
23 members and 722 guests
AdamL, ADY, BrianSlick, Desert Diva, gmarro, goldberg113, headkaze, HemiMG, Joseph Nardone, Mah6447, marto1914, mer10, mongoose250, NetGuru, Oral B, Pilsner6910, pinky, Promo Dispenser, ric2z, Shmoopi, syver, vonkal, wesgood
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,731
Threads: 89,189
Posts: 380,525
Top Poster: BrianSlick (7,128)
Welcome to our newest member, goldberg113
Powered by vBadvanced CMPS v3.1.0

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