MOBILE

iPhone Developer : Assembling Views and Animations - Transforming Views

12/21/2013 12:28:39 AM

Affine transforms enable you to change an object’s geometry by mapping that object from one view coordinate system into another. The iPhone SDK fully supports standard affine 2D transforms. With them, you can scale, translate, rotate, and skew your views however your heart desires and your application demands.

Transforms are defined in Core Graphics and consist of calls such as CGAffineTransformMakeRotation and CGAffineTransformScale. These build and modify the 3-by-3 transform matrices. Once built, use UIView’s setTransform call to apply 2D affine transformations to UIView objects.

Recipe 1 demonstrates how to build and apply an affine transform of a UIView. To create the sample, I kept things simple. I build an NSTimer that ticks every 1/30th of a second. On ticking, it rotates a view by 1% of pi and scales over a cosine curve. I use the cosine’s absolute value for two reasons. It keeps the view visible at all times, and it provides a nice bounce effect when the scaling changes direction. This produces a rotating bounce animation.

This is one of those samples that it’s best to build and view as you read through the code. You are better able to see how the handleTimer: method correlates to the visual effects you’re looking at.

Note

This recipe uses the standard C math library, which provides both the cosine function and the M_PI constant.


Recipe 1. Example of an Affine Transform of a UIView
#import <math.h>
#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc]
initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]

@interface TestBedViewController : UIViewController
{
NSTimer *timer;
int theta;
}
@end

@implementation TestBedViewController
- (void) move: (NSTimer *) aTimer
{
// Rotate each iteration by 1% of PI
CGFloat angle = theta * (M_PI / 100.0f);
CGAffineTransform transform = CGAffineTransformMakeRotation(angle);

// Theta ranges between 0% and 199% of PI, i.e. between 0 and 2*PI
theta = (theta + 1) % 200;

// For fun, scale by the absolute value of the cosine
float degree = cos(angle);
if (degree < 0.0) degree *= -1.0f;
degree += 0.5f;

// Create add scaling to the rotation transform
CGAffineTransform scaled = CGAffineTransformScale(transform,
degree, degree);

// Apply the affine transform
[[self.view viewWithTag:999] setTransform:scaled];
}

- (void) start: (id) sender
{
// The timer is automatically retained by the runloop
// You can start and stop it without being the owner
// or messing with its retain count.
timer = [NSTimer scheduledTimerWithTimeInterval:0.03f target:self
@selector(move:selector:) userInfo:nil repeats:YES];
[self move:nil];
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Stop",
@selector(stop:));
}

- (void) stop: (id) sender
{
[timer invalidate];
timer = nil;
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Start",
@selector(start:));
}

- (void) viewDidLoad
{
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Start",
@selector(start:));
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:@"BflyCircle.png"]];
imgView.tag = 999;
imgView.center = CGPointMake(160.0f, 143.0f);
[self.view addSubview:imgView];
[imgView release];

timer = nil;
theta = 0;
}
@end



Centering Landscape Views

Use the same affine transform approach to center landscape-oriented views. This snippet creates a 480-by-320 pixel view, centers it at [160, 240] (using portrait view coordinates), and then rotates it into place. Half of pi corresponds to 90 degrees, creating a landscaperight rotation. Centering keeps the entire view onscreen. All subviews, including text fields, labels, switches, and so on rotate into place along with the parent view.

#define PI 3.141592f

- (void)loadView
{
contentView = [[UIView alloc] initWithFrame:
CGRectMake(0.0f, 0.0f, 480.0f, 320.0f)];
[contentView setCenter:CGPointMake(160.0f, 240.0f)];
[contentView setBackgroundColor:[UIColor blackColor]];
[contentView setTransform:CGAffineTransformMakeRotation(PI/2.0f)];
self.view = contentView;
[contentView release];
}

For the most part, it’s far easier using UIViewControllers to work with reorientation events than manually rotating and presenting views. Additionally, manual view rotation does not change the status bar orientation nor the keyboard orientation.

Other  
  •  iPhone Developer : Assembling Views and Animations - Randomly Moving a Bounded View
  •  iPhone Developer : Assembling Views and Animations - Working with View Frames (part 2) - Other Utility Methods
  •  iPhone Developer : Assembling Views and Animations - Working with View Frames (part 1) - Adjusting Sizes , CGRects and Centers
  •  iPhone Developer : Assembling Views and Animations - View Geometry
  •  Windows Phone 7 : Drawing with Vertices and Matrices - Drawing Primitives
  •  Windows Phone 7 : Understanding Matrix Transformations (part 3) - Drawing Multiple Objects at Different Positions
  •  Windows Phone 7 : Understanding Matrix Transformations (part 2) - Applying Multiple Transformations
  •  Windows Phone 7 : Understanding Matrix Transformations (part 1) - Applying Rotation Transformations
  •  Windows Phone 7 : Drawing with Vertices and Matrices - Tinting Objects
  •  Android Application Development : Rolling Your Own Widgets (part 4) - Drawables, Bitmaps
  •  
    Top 10
    Review : Sigma 24mm f/1.4 DG HSM Art
    Review : Canon EF11-24mm f/4L USM
    Review : Creative Sound Blaster Roar 2
    Review : Philips Fidelio M2L
    Review : Alienware 17 - Dell's Alienware laptops
    Review Smartwatch : Wellograph
    Review : Xiaomi Redmi 2
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8