iPhone View Auto Rotate With View Updates - Header & Implementation


/ Published in: Objective C
Save to your folder(s)

this was just an exercise to learn about updating a UIImageView to the desired orientation;the accelerometer was used to detect the movements through the overridden method shouldAutorotateToInterfaceOrientation. wurd.


Copy this code and paste it in your HTML
  1. // Source @ http://www.jailbyte.ca/safari/files/SpinView.zip
  2. //
  3. // UntitledViewController.h
  4. // SpinView
  5. //
  6. // Created by Richard Vacheresse on 10-05-20.
  7. // Copyfright jailByte.ca 2010. Use it anyway you like.
  8. //
  9.  
  10. #import <UIKit/UIKit.h>
  11.  
  12. @interface UntitledViewController : UIViewController
  13. {
  14. //-view (the image) used for manipulation by the accelerometer
  15. UIImageView *imageView;
  16. }
  17.  
  18. @property (nonatomic, retain) UIImageView *imageView;
  19.  
  20. - (IBAction) actionBtnOne;
  21.  
  22. @end
  23.  
  24. ---------------------------------------------------------------------------
  25.  
  26. //
  27. // UntitledViewController.m
  28. //
  29. // SpinView - this is an example of how to deal with the rotation of views via the accelerometer and the
  30. // overridden method shouldAutorotateToInterfaceOrientation. this example uses an image view as the view
  31. // that is being rotated; this imageview is created with the press of a button, the resulting action is to
  32. // simply show the layer in the view. from there you start to spin the device and the updates follow. to reset
  33. // the view you must press the home key and then restart the app; sorry, just too lazy to add anymore.
  34. //
  35. // **note: there is a bug to this solution, and it becomes apparent when using the device and the user rotates
  36. // the phone quickly 180 degrees. i believe it may have something to do with when the floats that are used
  37. // to hold the x and y values are updated while rotating; like it grabs the coordinates while spinnning past 90
  38. // degrees. the next time you rotate the view, the view will correct itself.
  39. //
  40. // Created by Richard Vacheresse on 10-05-20.
  41. // Copyfright jailByte.ca 2010. Use it anyway you like.
  42. //
  43.  
  44. #import "UntitledViewController.h"
  45.  
  46. @implementation UntitledViewController
  47.  
  48. @synthesize imageView;
  49.  
  50. - (void)viewDidLoad
  51. {
  52. [super viewDidLoad];
  53.  
  54. //-create a button
  55. UIButton *btnOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  56. [btnOne setTitle: @"btnOne" forState: UIControlStateNormal];
  57. [btnOne setFrame:CGRectMake(0.0f, 0.0f, 105.0f, 55.0f)];
  58. [btnOne setCenter:CGPointMake(160.0f, 55.0f)];
  59. [btnOne addTarget:self action:@selector(actionBtnOne) forControlEvents:UIControlEventTouchUpInside];
  60. //-add it to the display
  61. [self.view addSubview: btnOne];
  62. }
  63.  
  64. //
  65. // actionBtnOne - initializes a UIImageView, sets the initial properties, add it to the display, then releases it.
  66. //
  67. - (IBAction) actionBtnOne
  68. {
  69. imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
  70. [imageView setImage:[UIImage imageNamed:@"image.png"]];
  71. [imageView setAutoresizesSubviews: YES];
  72. [self.view addSubview:imageView];
  73. [imageView release];
  74. }
  75.  
  76. //
  77. // shouldAutorotateToInterfaceOrientation - used to interact with the accelerometer; this is an overriden method.
  78. //
  79. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  80. {
  81. if ( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
  82. {
  83. //-set the center of the image from the dimensions of the display
  84. //-x value is +10 due to status bar
  85. [UIView beginAnimations: nil context: NULL];
  86. [UIView setAnimationDuration: 0.25];
  87. CGFloat x = self.view.bounds.size.height / 2 + 10;
  88. CGFloat y = self.view.bounds.size.width / 2;
  89. CGPoint center = CGPointMake( x, y);
  90. [imageView setCenter: center];
  91. [UIView commitAnimations];
  92.  
  93. [UIView beginAnimations: nil context: NULL];
  94. [UIView setAnimationDuration: 0.25];
  95.  
  96. //-this will keep it in the same position - always standing up
  97. //CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 0.5);
  98.  
  99. //this will keep it standing up as in portrait however it will turn it upsidedown
  100. //CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 1.0);
  101.  
  102. //this will change the view to be upside-down but in proper alignment with the landscape mode
  103. //CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 2.0);
  104.  
  105. //this will change the view to be rightside-up in proper alignment with landscape mode
  106. CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / -2.0);
  107.  
  108. imageView.transform = transform;
  109. [UIView commitAnimations];
  110.  
  111. }
  112. else
  113. {
  114. //-set the center of the image from the dimensions of the display
  115. //-x value is +10 due to status bar
  116. [UIView beginAnimations: nil context: NULL];
  117. [UIView setAnimationDuration: 0.25];
  118. CGFloat x = self.view.bounds.size.height / 2 + 10;
  119. CGFloat y = self.view.bounds.size.width / 2;
  120. CGPoint center = CGPointMake( x, y);
  121. [imageView setCenter: center];
  122. [UIView commitAnimations];
  123.  
  124. [UIView beginAnimations: nil context: NULL];
  125. [UIView setAnimationDuration: 0.25];
  126. CGAffineTransform move = CGAffineTransformMakeTranslation(0.0f, 0.0f);
  127. imageView.transform = move;
  128. CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 2);
  129. imageView.transform = transform;
  130. [UIView commitAnimations];
  131. }
  132.  
  133. // Return YES for supported orientations
  134. return (interfaceOrientation == UIInterfaceOrientationPortrait ||
  135. interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
  136. interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
  137. interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
  138. }
  139.  
  140. - (void)dealloc
  141. {
  142. [imageView release];
  143. [super dealloc];
  144. }
  145.  
  146. //-the
  147. @end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.