/ Published in: Objective C
Tips
1. This example is to make a horizontal image slider with 3 images for iPad.
2. To fix the orientation to horizontal only, modify project-Info.plist -supported interface orientations
3. The scrollView is added to view in code, there is no need to visually create a scrollView in the UIBuilder interface.
1. This example is to make a horizontal image slider with 3 images for iPad.
2. To fix the orientation to horizontal only, modify project-Info.plist -supported interface orientations
3. The scrollView is added to view in code, there is no need to visually create a scrollView in the UIBuilder interface.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// // SimpleScrollViewViewController.h // SimpleScrollView // // Created by Terrence Tee on 3/28/11. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> @interface SimpleScrollViewViewController : UIViewController { IBOutlet UIScrollView *scrollView; } @property (nonatomic, retain) IBOutlet UIScrollView *scrollView; @end // // SimpleScrollViewViewController.m // SimpleScrollView // // Created by Terrence Tee on 3/28/11. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import "SimpleScrollViewViewController.h" @implementation SimpleScrollViewViewController @synthesize scrollView; /* // The designated initializer. Override to perform setup that is required before the view is loaded. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } */ /* // Implement loadView to create a view hierarchy programmatically, without using a nib. - (void)loadView { } */ // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; // Create a UIImage to hold Info.png UIImage *image1 = [UIImage imageNamed:@"WELCOME.PNG"]; UIImage *image2 = [UIImage imageNamed:@"SLIDER.PNG"]; UIImage *image3 = [UIImage imageNamed:@"RESULT_BLUE.PNG"]; object // Now create a UIScrollView to hold the UIImageViews scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,1024,768)]; scrollView.pagingEnabled = YES; [scrollView setIndicatorStyle:UIScrollViewIndicatorStyleWhite]; int numberOfViews = 3; for (int i = 0; i < numberOfViews; i++) { CGFloat xOrigin = i * 1024; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,0,1024,768)]; [imageView setImage:[images objectAtIndex:i]]; [scrollView addSubview:imageView]; [imageView release]; } // Set the contentSize equal to the size of the UIImageView // scrollView.contentSize = imageView.scrollview.size; scrollView.contentSize = CGSizeMake(numberOfViews * 1024, 768); // Finally, add the UIScrollView to the controller's view [self.view addSubview:scrollView]; } @end