iPhone XCode - Create a simple UIScrollView

Finally getting acquainted with iPhone's xcode. Now I'll build a simple window base application to demo the UIScrollView features.

1.) Create a new window base application, named it MyScroll. It should create 2 files: a.) MyScrollAppDelegate.h and b.) MyScrollAppDelegate.m

2.) I'll explain the rest in code (Note I'm using UIImageView as the content of the UIScrollView):
#import <UIKit/UIKit.h>

@interface MyScrollAppDelegate : NSObject  { //notice that we implement UIScrollViewDelegate, this is needed
    UIWindow *window;
 UIScrollView *scrollView; //declare a scrollview
 UIImageView *imageView; //the object which we will add to scrollview

@property (nonatomic, retain) IBOutlet UIWindow *window;


#import "MyScrollAppDelegate.h"

@implementation MyScrollAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
 //initialize the scroll view
 scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
 scrollView.bouncesZoom = YES;
 scrollView.backgroundColor = [UIColor orangeColor];
 scrollView.delegate = self;
 //create the image, 
 //note there should be an image in your Resources folder named edward.jpg
 UIImage *image = [UIImage imageNamed:@"2.jpg"];
 //we need to create a framework the ui image view
 CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
 //initialize the ui image view
 imageView = [[UIImageView alloc] initWithFrame:frame];
 imageView.image = image;
 //set the content size of the scroll view
 //for more information please consult the UIScrollView's documentation
 scrollView.contentSize = imageView.frame.size;
 //set the zooming properties of the scroll view
 scrollView.minimumZoomScale = scrollView.frame.size.width / image.size.width;
 scrollView.maximumZoomScale = 2.0;
 //add the image view to the scroll view
 [scrollView addSubview:imageView];
 [image release];
 //add the scroll view to our main window
 [window addSubview:scrollView];
    // Override point for customization after application launch
    [window makeKeyAndVisible];

//we zoom the image view
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
 return imageView;

- (void)dealloc {
    [window release];
    [super dealloc];

iPhone XCode - Create a simple UIScrollView iPhone XCode - Create a simple UIScrollView Reviewed by Edward Legaspi on Sunday, December 26, 2010 Rating: 5

1 comment:

Kakubei said...

just though you'd like to know this doesn't work on Xcode 4 with iOS 4.3. And this line:


produces an error (even with proper camelCase, Xcode doesn't like the = there.

It's too bad, I'm really looking for a simple image scroll implementation.

Powered by Blogger.