Design with iPhone Project 7: Rotatable/resizable apps, gestures/swipes, motion 

Warning:
Content of this website may change at any time up until class meeting.

Project Topics


Preparation


Create a new iOS app in Xcode

p5fig-newTabBarApp.jpg
Fig. 1
p7fig-newTabBar3.jpg
Fig. 3


Supporting Rotated Views
p7fig-rotRight1.jpg
Fig. 4

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait ||           
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
p7fig-thirdViewControlClass.jpg
Fig. 5

p7fig-rotRight2.jpg
Fig. 6


Resizing

p7fig-resize.jpg
Fig. 7
p7fig-resize2.jpg
Fig. 8


Reframing

-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation  duration:(NSTimeInterval)duration

    [super willRotateToInterfaceOrientation:toInterfaceOrientation 
                                   duration:duration];
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight ||
        toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        myLabel1.frame=CGRectMake(20.0,50.0,100.0,50.0);
        myNavBar1.frame=CGRectMake(-1.0,-1.0,481.0,44.0);
        myTextView1.frame=CGRectMake(150.0,50.0,300.0,250.0);       
    } else {
        myLabel1.frame=CGRectMake(55.0,140.0,210.0,45.0);
        myNavBar1.frame=CGRectMake(-1.0,-1.0,320.0,44.0);
        myTextView1.frame=CGRectMake(20.0,240.0,280.0,120.0);       
    }
}

p7fig-reframe.jpg
Fig. 9

Gesture recognition
p7fig-multiTouchOn.jpg
Fig. 10

@property (nonatomic, retain) IBOutlet UILabel *myLabel2;
@property (nonatomic, retain) IBOutlet UIImageView *myImageView2;
- (void)viewDidLoad
{   [super viewDidLoad];
   
    UITapGestureRecognizer *mytap1;
    mytap1=[[UITapGestureRecognizer alloc]
            initWithTarget:self
            action:@selector(foundTap1:)];
    mytap1.numberOfTapsRequired=1;
    mytap1.numberOfTouchesRequired=1;
    [myImageView2 addGestureRecognizer:mytap1];
    [mytap1 release];
 
    UISwipeGestureRecognizer *myswiper;
    myswiper=[[UISwipeGestureRecognizer alloc]
                     initWithTarget:self
                     action:@selector(foundSwiper:)];
    myswiper.direction=UISwipeGestureRecognizerDirectionRight;
    myswiper.numberOfTouchesRequired=1;
    [myImageView2 addGestureRecognizer:myswiper];
    [myswiper release];
}


- (void)foundTap1:(UITapGestureRecognizer *)recognizer {
    myLabel2.text=@"Single-Tap";
}

- (void)foundSwiper:(UISwipeGestureRecognizer *)recognizer {
    myLabel2.text=@"Swipe-Right";
}
p7fig-appSimGestures.jpg
Fig. 11


Accelerometer & Gyro

p7fig-appSimAccel.jpg
Fig. 12
#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>
@interface ThirdViewController : UIViewController { 
    UILabel *myXlabel;
    UILabel *myYlabel;
    UILabel *myZlabel;
    UILabel *myXlabel2;
    UILabel *myYlabel2;
    UILabel *myZlabel2;
    CMMotionManager *motionManager;
}
@property (nonatomic, retain) IBOutlet UILabel *myXlabel;
@property (nonatomic, retain) IBOutlet UILabel *myYlabel;
@property (nonatomic, retain) IBOutlet UILabel *myZlabel;
@property (nonatomic, retain) IBOutlet UILabel *myXlabel2;
@property (nonatomic, retain) IBOutlet UILabel *myYlabel2;
@property (nonatomic, retain) IBOutlet UILabel *myZlabel2;
- (void)printAcceleration:(CMAcceleration)acceleration ;
- (void)printGyros:(CMRotationRate)rotation ;
@end

- (void)viewDidLoad
{
    motionManager = [[CMMotionManager alloc] init];
    motionManager.accelerometerUpdateInterval = .1;  
    [motionManager
     startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
     withHandler:^(CMAccelerometerData *accelData, NSError *error) {
         [self printAcceleration:accelData.acceleration];
     }];
    [super viewDidLoad];
}
p7fig-appSimGyros.jpg
Fig. 13
p7fig-appSimGyrosRotRight.jpg
Fig. 14





Demonstration



Report



Copyright 2011 by T.P. Weldon


Apple, iPhone, iPad, and Xcode  are registered trademarks of Apple Inc.