Posted By

magickaito on 03/28/11


Tagged

objectivec


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

gudeidea
athanhcong
Tintenklecks
cnathan


UIScrollView with 3 images


 / 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. //
  2. // SimpleScrollViewViewController.h
  3. // SimpleScrollView
  4. //
  5. // Created by Terrence Tee on 3/28/11.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface SimpleScrollViewViewController : UIViewController {
  12. IBOutlet UIScrollView *scrollView;
  13. }
  14.  
  15. @property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
  16.  
  17. @end
  18.  
  19.  
  20. //
  21. // SimpleScrollViewViewController.m
  22. // SimpleScrollView
  23. //
  24. // Created by Terrence Tee on 3/28/11.
  25. // Copyright 2011 __MyCompanyName__. All rights reserved.
  26. //
  27.  
  28. #import "SimpleScrollViewViewController.h"
  29.  
  30. @implementation SimpleScrollViewViewController
  31.  
  32. @synthesize scrollView;
  33.  
  34. /*
  35. // The designated initializer. Override to perform setup that is required before the view is loaded.
  36. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  37.   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  38.   if (self) {
  39.   // Custom initialization
  40.   }
  41.   return self;
  42. }
  43. */
  44.  
  45. /*
  46. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  47. - (void)loadView {
  48. }
  49. */
  50.  
  51.  
  52.  
  53. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  54. - (void)viewDidLoad {
  55. [super viewDidLoad];
  56.  
  57. // Create a UIImage to hold Info.png
  58. UIImage *image1 = [UIImage imageNamed:@"WELCOME.PNG"];
  59. UIImage *image2 = [UIImage imageNamed:@"SLIDER.PNG"];
  60. UIImage *image3 = [UIImage imageNamed:@"RESULT_BLUE.PNG"];
  61.  
  62. NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,image3,nil];
  63. object
  64. // Now create a UIScrollView to hold the UIImageViews
  65. scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,1024,768)];
  66.  
  67. scrollView.pagingEnabled = YES;
  68.  
  69. [scrollView setIndicatorStyle:UIScrollViewIndicatorStyleWhite];
  70.  
  71. int numberOfViews = 3;
  72. for (int i = 0; i < numberOfViews; i++) {
  73. CGFloat xOrigin = i * 1024;
  74. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,0,1024,768)];
  75. [imageView setImage:[images objectAtIndex:i]];
  76. [scrollView addSubview:imageView];
  77. [imageView release];
  78. }
  79.  
  80. // Set the contentSize equal to the size of the UIImageView
  81. // scrollView.contentSize = imageView.scrollview.size;
  82. scrollView.contentSize = CGSizeMake(numberOfViews * 1024, 768);
  83.  
  84.  
  85. // Finally, add the UIScrollView to the controller's view
  86. [self.view addSubview:scrollView];
  87. }
  88.  
  89. @end

Report this snippet  

You need to login to post a comment.