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 06-23-2009, 03:43 AM   #16 (permalink)
shiva.0537
shiva
 
shiva.0537's Avatar
 
Join Date: Jun 2009
Location: Hyderabad
Age: 23
Posts: 54
Send a message via Skype™ to shiva.0537
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 ;-)
HI,

Nice post

You could have shared that application. It will be more helpful
shiva.0537 is offline   Reply With Quote
 
Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Online Users: 458
29 members and 429 guests
alexy, asimrs, Boh, bugman, dany88, Dmitry, DoBs, Erle, fnicola, greetings, hatembr, hg4072, ivanraso, joalta, Johanovski, KarlG, manofham, Meoz, myPhone, nikhil, ovidiu, Salman, sandman4sure, skunk, SteveMobs, thomaswguy, tychop, umop, vikysaran
Most users ever online was 779, 05-11-2009 at 09:55 AM.
» Stats
Members: 24,095
Threads: 38,876
Posts: 170,546
Top Poster: smasher (2,563)
Welcome to our newest member, hg4072
Powered by vBadvanced CMPS v3.1.0

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