Return to Snippet

Revision: 27099
at May 22, 2010 08:42 by rvachere


Updated Code
// Source @ http://www.jailbyte.ca/safari/files/SpinView.zip
//
//  UntitledViewController.h
//  SpinView
//
//  Created by Richard Vacheresse on 10-05-20.
//  Copyfright jailByte.ca 2010. Use it anyway you like.
//

#import <UIKit/UIKit.h>

@interface UntitledViewController : UIViewController
{
	//-view (the image) used for manipulation by the accelerometer
	UIImageView *imageView;
}

@property (nonatomic, retain) UIImageView *imageView;

- (IBAction) actionBtnOne;

@end

---------------------------------------------------------------------------

//
//  UntitledViewController.m
//
//  SpinView - this is an example of how to deal with the rotation of views via the accelerometer and the
//  overridden method shouldAutorotateToInterfaceOrientation. this example uses an image view as the view
//	that is being rotated; this imageview is created with the press of a button, the resulting action is to
//	simply show the layer in the view. from there you start to spin the device and the updates follow. to reset
//  the view you must press the home key and then restart the app; sorry, just too lazy to add anymore.
//
//	**note: there is a bug to this solution, and it becomes apparent when using the device and the user rotates
//	the phone quickly 180 degrees. i believe it may have something to do with when the floats that are used
//	to hold the x and y values are updated while rotating; like it grabs the coordinates while spinnning past 90
//	degrees. the next time you rotate the view, the view will correct itself.
//
//  Created by Richard Vacheresse on 10-05-20.
//  Copyfright jailByte.ca 2010. Use it anyway you like.
//

#import "UntitledViewController.h"

@implementation UntitledViewController

@synthesize imageView;

- (void)viewDidLoad
{
    [super viewDidLoad];
	
	//-create a button
	UIButton *btnOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btnOne setTitle: @"btnOne" forState: UIControlStateNormal];
	[btnOne setFrame:CGRectMake(0.0f, 0.0f, 105.0f, 55.0f)];
	[btnOne setCenter:CGPointMake(160.0f, 55.0f)];
	[btnOne addTarget:self action:@selector(actionBtnOne) forControlEvents:UIControlEventTouchUpInside];
	//-add it to the display
	[self.view addSubview: btnOne];
}

//
//	actionBtnOne - initializes a UIImageView, sets the initial properties, add it to the display, then releases it.
//	
- (IBAction) actionBtnOne
{
	imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
	[imageView setImage:[UIImage imageNamed:@"image.png"]];
	[imageView setAutoresizesSubviews: YES];
	[self.view addSubview:imageView];
	[imageView release];
}

//
//	shouldAutorotateToInterfaceOrientation - used to interact with the accelerometer; this is an overriden method.
//	
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	if ( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
	{		
		//-set the center of the image from the dimensions of the display
		//-x value is +10 due to status bar
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		CGFloat x = self.view.bounds.size.height / 2 + 10;
		CGFloat y = self.view.bounds.size.width / 2;
		CGPoint center = CGPointMake( x, y); 
		[imageView setCenter: center];
		[UIView commitAnimations];
		
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		
		//-this will keep it in the same position - always standing up
		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 0.5);
		
		//this will keep it standing up as in portrait however it will turn it upsidedown
		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 1.0);
		
		//this will change the view to be upside-down but in proper alignment with the landscape mode
		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 2.0);
		
		//this will change the view to be rightside-up in proper alignment with landscape mode
		CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / -2.0);
		
		imageView.transform = transform;
		[UIView commitAnimations];
		
	}
	else
	{
		//-set the center of the image from the dimensions of the display
		//-x value is +10 due to status bar
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		CGFloat x = self.view.bounds.size.height / 2 + 10;
		CGFloat y = self.view.bounds.size.width / 2;
		CGPoint center = CGPointMake( x, y); 
		[imageView setCenter: center];
		[UIView commitAnimations];
		
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		CGAffineTransform move = CGAffineTransformMakeTranslation(0.0f, 0.0f);
		imageView.transform = move;
		CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 2);
		imageView.transform = transform;
		[UIView commitAnimations];
	}
	
    // Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait ||
			interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
			interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
			interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

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

//-the
@end

Revision: 27098
at May 21, 2010 22:09 by rvachere


Updated Code
// Source @ http://www.jailbyte.ca/files/SpinView.zip
//
//  UntitledViewController.h
//  SpinView
//
//  Created by Richard Vacheresse on 10-05-20.
//  Copyfright jailByte.ca 2010. Use it anyway you like.
//

#import <UIKit/UIKit.h>

@interface UntitledViewController : UIViewController
{
	//-view (the image) used for manipulation by the accelerometer
	UIImageView *imageView;
}

@property (nonatomic, retain) UIImageView *imageView;

- (IBAction) actionBtnOne;

@end

---------------------------------------------------------------------------

//
//  UntitledViewController.m
//
//  SpinView - this is an example of how to deal with the rotation of views via the accelerometer and the
//  overridden method shouldAutorotateToInterfaceOrientation. this example uses an image view as the view
//	that is being rotated; this imageview is created with the press of a button, the resulting action is to
//	simply show the layer in the view. from there you start to spin the device and the updates follow. to reset
//  the view you must press the home key and then restart the app; sorry, just too lazy to add anymore.
//
//	**note: there is a bug to this solution, and it becomes apparent when using the device and the user rotates
//	the phone quickly 180 degrees. i believe it may have something to do with when the floats that are used
//	to hold the x and y values are updated while rotating; like it grabs the coordinates while spinnning past 90
//	degrees. the next time you rotate the view, the view will correct itself.
//
//  Created by Richard Vacheresse on 10-05-20.
//  Copyfright jailByte.ca 2010. Use it anyway you like.
//

#import "UntitledViewController.h"

@implementation UntitledViewController

@synthesize imageView;

- (void)viewDidLoad
{
    [super viewDidLoad];
	
	//-create a button
	UIButton *btnOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btnOne setTitle: @"btnOne" forState: UIControlStateNormal];
	[btnOne setFrame:CGRectMake(0.0f, 0.0f, 105.0f, 55.0f)];
	[btnOne setCenter:CGPointMake(160.0f, 55.0f)];
	[btnOne addTarget:self action:@selector(actionBtnOne) forControlEvents:UIControlEventTouchUpInside];
	//-add it to the display
	[self.view addSubview: btnOne];
}

//
//	actionBtnOne - initializes a UIImageView, sets the initial properties, add it to the display, then releases it.
//	
- (IBAction) actionBtnOne
{
	imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
	[imageView setImage:[UIImage imageNamed:@"image.png"]];
	[imageView setAutoresizesSubviews: YES];
	[self.view addSubview:imageView];
	[imageView release];
}

//
//	shouldAutorotateToInterfaceOrientation - used to interact with the accelerometer; this is an overriden method.
//	
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	if ( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
	{		
		//-set the center of the image from the dimensions of the display
		//-x value is +10 due to status bar
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		CGFloat x = self.view.bounds.size.height / 2 + 10;
		CGFloat y = self.view.bounds.size.width / 2;
		CGPoint center = CGPointMake( x, y); 
		[imageView setCenter: center];
		[UIView commitAnimations];
		
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		
		//-this will keep it in the same position - always standing up
		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 0.5);
		
		//this will keep it standing up as in portrait however it will turn it upsidedown
		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 1.0);
		
		//this will change the view to be upside-down but in proper alignment with the landscape mode
		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 2.0);
		
		//this will change the view to be rightside-up in proper alignment with landscape mode
		CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / -2.0);
		
		imageView.transform = transform;
		[UIView commitAnimations];
		
	}
	else
	{
		//-set the center of the image from the dimensions of the display
		//-x value is +10 due to status bar
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		CGFloat x = self.view.bounds.size.height / 2 + 10;
		CGFloat y = self.view.bounds.size.width / 2;
		CGPoint center = CGPointMake( x, y); 
		[imageView setCenter: center];
		[UIView commitAnimations];
		
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		CGAffineTransform move = CGAffineTransformMakeTranslation(0.0f, 0.0f);
		imageView.transform = move;
		CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 2);
		imageView.transform = transform;
		[UIView commitAnimations];
	}
	
    // Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait ||
			interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
			interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
			interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

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

//-the
@end

Revision: 27097
at May 21, 2010 21:54 by rvachere


Initial Code
//
//  UntitledViewController.h
//  SpinView
//
//  Created by Richard Vacheresse on 10-05-20.
//  Copyfright jailByte.ca 2010. Use it anyway you like.
//

#import <UIKit/UIKit.h>

@interface UntitledViewController : UIViewController
{
	//-view (the image) used for manipulation by the accelerometer
	UIImageView *imageView;
}

@property (nonatomic, retain) UIImageView *imageView;

- (IBAction) actionBtnOne;

@end

---------------------------------------------------------------------------

//
//  UntitledViewController.m
//
//  SpinView - this is an example of how to deal with the rotation of views via the accelerometer and the
//  overridden method shouldAutorotateToInterfaceOrientation. this example uses an image view as the view
//	that is being rotated; this imageview is created with the press of a button, the resulting action is to
//	simply show the layer in the view. from there you start to spin the device and the updates follow. to reset
//  the view you must press the home key and then restart the app; sorry, just too lazy to add anymore.
//
//	**note: there is a bug to this solution, and it becomes apparent when using the device and the user rotates
//	the phone quickly 180 degrees. i believe it may have something to do with when the floats that are used
//	to hold the x and y values are updated while rotating; like it grabs the coordinates while spinnning past 90
//	degrees. the next time you rotate the view, the view will correct itself.
//
//  Created by Richard Vacheresse on 10-05-20.
//  Copyfright jailByte.ca 2010. Use it anyway you like.
//

#import "UntitledViewController.h"

@implementation UntitledViewController

@synthesize imageView;

- (void)viewDidLoad
{
    [super viewDidLoad];
	
	//-create a button
	UIButton *btnOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btnOne setTitle: @"btnOne" forState: UIControlStateNormal];
	[btnOne setFrame:CGRectMake(0.0f, 0.0f, 105.0f, 55.0f)];
	[btnOne setCenter:CGPointMake(160.0f, 55.0f)];
	[btnOne addTarget:self action:@selector(actionBtnOne) forControlEvents:UIControlEventTouchUpInside];
	//-add it to the display
	[self.view addSubview: btnOne];
}

//
//	actionBtnOne - initializes a UIImageView, sets the initial properties, add it to the display, then releases it.
//	
- (IBAction) actionBtnOne
{
	imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
	[imageView setImage:[UIImage imageNamed:@"image.png"]];
	[imageView setAutoresizesSubviews: YES];
	[self.view addSubview:imageView];
	[imageView release];
}

//
//	shouldAutorotateToInterfaceOrientation - used to interact with the accelerometer; this is an overriden method.
//	
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	if ( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
	{		
		//-set the center of the image from the dimensions of the display
		//-x value is +10 due to status bar
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		CGFloat x = self.view.bounds.size.height / 2 + 10;
		CGFloat y = self.view.bounds.size.width / 2;
		CGPoint center = CGPointMake( x, y); 
		[imageView setCenter: center];
		[UIView commitAnimations];
		
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		
		//-this will keep it in the same position - always standing up
		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 0.5);
		
		//this will keep it standing up as in portrait however it will turn it upsidedown
		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 1.0);
		
		//this will change the view to be upside-down but in proper alignment with the landscape mode
		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 2.0);
		
		//this will change the view to be rightside-up in proper alignment with landscape mode
		CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / -2.0);
		
		imageView.transform = transform;
		[UIView commitAnimations];
		
	}
	else
	{
		//-set the center of the image from the dimensions of the display
		//-x value is +10 due to status bar
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		CGFloat x = self.view.bounds.size.height / 2 + 10;
		CGFloat y = self.view.bounds.size.width / 2;
		CGPoint center = CGPointMake( x, y); 
		[imageView setCenter: center];
		[UIView commitAnimations];
		
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.25];
		CGAffineTransform move = CGAffineTransformMakeTranslation(0.0f, 0.0f);
		imageView.transform = move;
		CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 2);
		imageView.transform = transform;
		[UIView commitAnimations];
	}
	
    // Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait ||
			interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
			interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
			interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

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

//-the
@end

Initial URL

                                

Initial Description
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.

Initial Title
iPhone View Auto Rotate With View Updates - Header & Implementation

Initial Tags
update, iphone

Initial Language
Objective C