HelloRotation - Device Orientation, simple UIAnimation, ViewControllers

Discussion in 'iOS Development' started by SkylarEC, Sep 5, 2008.

  1. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Alright, it seems that very few people know how to make an application detect the Orientation of the device, and to redraw its views to the correct size. So, I whipped up this app right quickly for youse guys.

    Working Application w/SourceCode after the posted source code. This build assumes that you are a registered developer with Apple and have access to the SDK. This should work in xcode, but will also work on the toolchain, presuming you have patched your headers appropriately.

    main.m
    Code:
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    #import "HelloRotation.h"
    
    int main(int argc, char **argv){
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        int retValue = UIApplicationMain(argc, argv, @"HelloRotation", @"HelloRotation");
        [pool release];
        return retValue;
    }
    HelloRotation.h
    Code:
    #import <CoreFoundation/CoreFoundation.h>
    #import <Foundation/Foundation.h>
    #import <GraphicsServices/GraphicsServices.h>
    #import <UIKit/UIKit.h>
    #import <UIKit/UIScreen.h>
    #import <UIKit/UITouch.h>
    #include <stdlib.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <strings.h>
    
    #include "HRViewController.h"
    
    @class UIEvent;
    
    
    @interface HelloRotation : UIApplication {
    	
    	UIWindow		*window;
    	HRViewController	*hrViewController;
    }
    
    @end
    HelloRotation.m
    Code:
    #import "HelloRotation.h"
    
    @implementation HelloRotation
    
    
    -(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    
    	struct CGRect rect = [[UIScreen mainScreen] bounds];
    	rect.origin.x = rect.origin.y = 0.0f;
    
    	hrViewController = [[HRViewController alloc] initWithFrame:rect];
    
    	window = [[UIWindow alloc] initWithFrame:rect];
    	[window makeKeyAndVisible];
    	[window setContentView:hrViewController.view];
    }
    
    -(void)dealloc {
    	[window dealloc];
    	[hrViewController dealloc];
    	[super dealloc];
    }
    
    @end
    HRViewController.h
    Code:
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    #import <UIKit/UIColor.h>
    #import <UIKit/UIScreen.h>
    #import <UIKit/UIView.h>
    #import <UIKit/UIViewController.h>
    
    @interface HRViewController : UIViewController {
    	struct CGRect		rect;
    	UIView			*backView;
    	UIImageView 		*imageView;
    }
    
    @property (nonatomic, retain) UIImageView *imageView;
    
    -(id)initWithFrame:(CGRect)_rect;
    
    @end
    HRViewController.m
    Code:
    #include "HRViewController.h"
    
    @implementation HRViewController
    
    @synthesize imageView;
    
    
    //UIViewControllers are full screen.  I have added the initWithFrame because I want to pass the appFrame down
    //to the subviews and methods. It's not important to do this.  init would have sufficed just fine.
    
    -(id)initWithFrame:(CGRect)_rect {
    	rect = _rect;
    	if (self = [super init]) {
    		imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"105.png"]];
    		imageView.alpha = 0.0;
    		[imageView setFrame:rect];
    
    		backView = [[UIView alloc] initWithFrame:rect];
    		backView.backgroundColor = [UIColor blackColor];
    
    		return self;
    	}
    }
    
    - (void)loadView {
    	self.view = [[UIView alloc] initWithFrame:rect];
    	self.view.backgroundColor = [UIColor blackColor];
    	[self.view addSubview:backView];
    }
    
    -(void)viewDidLoad{		//This is not necessary, but I hate Apps that just POP onto the screen
    	[UIView beginAnimations:nil];
    	[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    	[UIView setAnimationDuration:0.50];
    	[self.view addSubview:self.imageView];
    	self.imageView.alpha = 1.0;
    	[UIView endAnimations];
    }
    
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	return YES;
    }
    
    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    	if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
    		float newWidth = rect.size.width *0.66f;
    		[UIView beginAnimations:nil];
    		[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    		[UIView setAnimationDuration:0.50];
    		[self.imageView setFrame:CGRectMake(rect.size.height / 2 - newWidth / 2, 0, newWidth, rect.size.width)];
    		[UIView endAnimations];
    	} else	if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
    		[UIView beginAnimations:nil];
    		[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    		[UIView setAnimationDuration:0.50];
    		[self.imageView setFrame:rect];
    		[UIView endAnimations];
    	}
    }
    
    
    -(void)dealloc {
    	[backView dealloc];
    	[imageView dealloc];
    	[super dealloc];
    }
    
    @end


    To get a feel for the Application, please run it first, then mess around with it. Obviously, if you want the landscape view to fill the screen, just set its frame to (0,0, rect.size.height, rect.size.width)
    http://www.touchrepo.com/SampleCode/HelloRotation.app.7z
  2. Tom_fn

    Tom_fn New Member

    Joined:
    Aug 21, 2008
    Messages:
    226
    Likes Received:
    0
    Thanks for the guide skylar!
  3. BroadStBullies

    BroadStBullies Well-Known Member

    Joined:
    Dec 15, 2007
    Messages:
    2,007
    Likes Received:
    26
    Device:
    2G iPod touch
    Cool. I saw your video of this on youtube.
  4. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    yeah resetting the frame of the views would be the easy way to do this. now i have the sdk which is making this a little easier for me

    Please Register or Log in to view images

  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    That's all Apple does. Watch Safari. Watch Photos. the only thing Apple does different in their Photos app that this demo does is that it pins the image to its center and only resizes. This demo doesn't pin it down, although you could easily modify the code to do that. I wanted to show you three simultaneous animations instead of two.

    Just for the common knowledge, animations are performed in a separate thread automatically, so they shouldn't effect your apps all that much.


    You can expand from this very easy by adding: [UIView setTransform:CGAffineTransformwhatever]

    HITMAN45, that example you saw on YouTube is an example of what not to do. It is not this app at all.
  6. BroadStBullies

    BroadStBullies Well-Known Member

    Joined:
    Dec 15, 2007
    Messages:
    2,007
    Likes Received:
    26
    Device:
    2G iPod touch
    Oh. my bad.
  7. welkingrant

    welkingrant New Member

    Joined:
    Dec 16, 2010
    Messages:
    1
    Likes Received:
    0
    I was confused because there is an error--" Type of property "imageView" dose not match type of ivar "imageView" in the "HRViewController"class. Could you help me?

Share This Page