Wednesday, June 30, 2010

Zooming and Scrolling in UIScrollView(iphone Application)

Hi,
When i was learning how to use UIScrollview for a controls like image or label. Scrolling is not worked properly so i google for it then i finally found then we need to give contentsize above the scrollview height and width.


Following code gives you image zooming and scrolling.
  1. First create a navigation based application or create a window based application in .h file declare the UIScrollView and UIImageview and add the scrollviewdelegete to zoom the image
#import
@interface ScrollingImage : UIViewController
{
UIScrollView *scrollView;
UIImageView *imageView;
}
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;
@end


    2) In .m file synthesize the scrollView and imageView and in dealloc delegate release them both.


#import "ScrollingImage.h"


@implementation ScrollingImage
@synthesize scrollView, imageView;


- (void)dealloc {
    [super dealloc];
[imageView release]; [scrollView release];
}

//To zoom

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return imageView;
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Lighthouse.jpg"]]; self.imageView = tempImageView;
tempImageView.frame = CGRectMake(0, 0, 320, 370); scrollView.pagingEnabled = NO; scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320, 370)];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width + 150 , scrollView.frame.size.height + 150); scrollView.maximumZoomScale = 10.0; scrollView.minimumZoomScale = 1; scrollView.delegate = self; scrollView.scrollsToTop = NO; scrollView.backgroundColor = [UIColor whiteColor]; scrollView.showsHorizontalScrollIndicator=YES; scrollView.showsVerticalScrollIndicator=YES; [scrollView addSubview:imageView]; [self.view addSubview:scrollView]; [tempImageView release];
}


Now build and go in the simulator to run the application.

No comments:

Post a Comment