MOBILE

iPhone Developer : Assembling Views and Animations - View Geometry

12/21/2013 12:24:43 AM

As you’d expect, geometry plays an important role when working with views. Geometry defines where each view appears onscreen, what its size is, and how it is oriented. The UIView class provides two built-in properties that define these aspects.

Every view uses a frame to define its boundaries. The frame specifies the outline of the view: its location, width, and height. If you change a view’s frame, the view updates to match the new frame. Use a bigger width and the view stretches. Use a new location and the view moves. The view’s frame delineates each view’s onscreen outline. View sizes are not limited to the screen size. A view can be smaller than the screen or larger. It can also be smaller or larger than its parent.

Views also use a transform property that sets the view’s orientation and any geometric transformations that have been applied to it. For example, a view might be stretched or squashed by applying a transform, or it might be rotated away from vertical. Together the frame and transform fully define a view’s geometry.

Frames

Frame rectangles use a CGRect structure, which is defined as part of the Core Graphics framework as its CG prefix suggests. A CGRect is made up of an origin (a CGPoint, x and y) and a size (a CGSize, width and height). When you create views, you normally allocate them and initialize them with a frame, for example:

CGRect rect = CGRectMake(0.0f, 0.0f, 320.0f, 416.0f);
myView = [[UIView alloc] initWithFrame: rect];

The CGRectMake function creates a new rectangle using four parameters, the origin’s x and y locations, the width of the rectangle, and its height. In addition to CGRectMake, there are several other convenience functions you may want to be aware of that help you work with rectangles and frames.

  • NSStringFromCGRect(aCGRect) converts a CGRect structure to a formatted string. This function makes it easy to log a view’s frame when you’re debugging.

  • CGRectFromString(aString) recovers a rectangle from its string representation. It proves useful when you’ve stored a view’s frame as a string in user defaults and want to convert that string back to a CGRect.

  • CGRectInset(aRect, xinset, yinset) enables you to create a smaller or larger rectangle that’s centered on the same point as the source rectangle. Use a positive inset for smaller rectangles, negative for larger ones.

  • CGRectIntersectsRect(rect1, rect2) lets you know whether rectangle structures intersect. Use this function to know when two rectangular onscreen objects overlap.

  • CGRectCreateDictionaryRepresentation(aRect) transforms a rectangle structure into a standard CFDictionaryRef, also known (via the magic of toll-free bridging) as (NSDictionary *) instances. Transform the dictionary back to a rectangle by using CGRectMakeWithDictionaryRepresentation(aDict, aRect).

  • CGRectZero is a rectangle constant located at (0,0) whose width and height are zero. You can use this constant when you’re required to create a frame but are still unsure what that frame size or location will be at the time of creation.

The CGRect structure is made up of two substructures: CGPoint, which defines the rectangle’s origin, and CGSize, which defines its bounds. Points refer to locations defined with x and y coordinates; sizes have width and height. Use CGPointMake(x, y) to create points. CGSizeMake(width, height) creates sizes. Although these two structures appear to be the same (two floating-point values), the iPhone SDK differentiates between them. Points refer to locations. Sizes refer to extents. You cannot set myFrame.origin to a size.

As with rectangles, you can convert them to and from strings: NSStringFromCGPoint(), NSStringFromCGSize(), CGSizeFromString(), and CGPointFromString() perform these functions. You can also transform points and sizes to and from dictionaries.

Transforms

The iPhone supports standard affine transformations as part of its Core Graphics implementation. Affine transforms allow points in one coordinate system to transform into another coordinate system. These functions are widely used in both 2D and 3D animations. The version used in the iPhone SDK uses a 3-by-3 matrix to define UIView transforms, making it a 2D-only solution. With affine transforms, you can scale, translate, and rotate your views in real time. You do so by setting the view’s transform property, for example:

float angle = theta * (PI / 100);
CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
myView.transform = transform;

The transform is always applied with respect to the view’s center. So when you apply a rotation like this, the view rotates around its center. If you need to rotate around another point, you must first translate the view, then rotate, and then return from that translation.

To revert any changes, set the transform property to the identity transform. This restores the view back to the last settings for its frame.

myView.transform = CGAffineTransformIdentity;

Coordinate Systems

Views live in two worlds. Their frames are defined in the coordinate system of their parents. Their bounds and subviews are defined in their own coordinate system. The iPhone SDK offers several utilities that allow you move between these coordinate systems so long as the views involved live within the same UIWindow. To convert a point from another view into your own coordinate system, use convertPoint: fromView:, for example:

myPoint = [myView convertPoint:somePoint fromView:otherView];

If the original point indicated the location of some object, the new point retains that location but gives the coordinates with respect to myView’s origin. To go the other way, use convertPoint: toView: to transform a point into another view’s coordinate system. Similarly, convertRect: toView: and convertRect: fromView: work with CGRect structures rather than CGPoint ones.

Other  
  •  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
  •  Android Application Development : Rolling Your Own Widgets (part 3) - Canvas Drawing - Drawing text, Matrix transformations
  •  Android Application Development : Rolling Your Own Widgets (part 2) - Canvas Drawing
  •  Android Application Development : Rolling Your Own Widgets (part 1) - Layout
  •  iPhone SDK 3 Programming : XML Processing - An RSS Reader Application
  •  
    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