MOBILE

iPhone Developer : Assembling Views and Animations - Working with View Frames (part 2) - Other Utility Methods

12/21/2013 12:26:28 AM

3. Other Utility Methods

As you’ve seen, it’s convenient to expose a view’s origin and size in parallel to its center, allowing you to work more natively with Core Graphics calls. You can build on this idea to expose other properties of the view including its width and height, as well as basic geometry like its left, right, top, and bottom points.

In some ways, this breaks Apple’s design philosophy. This exposes items that normally fall into structures without reflecting the structures. At the same time, it can be argued that these elements are true view properties. They reflect fundamental view characteristics and deserve to be exposed as properties.

Recipe 1 provides a full view frame utility category for UIView, letting you make the choice of whether to use these properties.

Recipe 1. UIView Frame Geometry Category
@interface UIView (ViewFrameGeometry)
@property CGPoint origin;
@property CGSize size;
@property (readonly) CGPoint bottomLeft;
@property (readonly) CGPoint bottomRight;
@property (readonly) CGPoint topRight;
@property CGFloat height;
@property CGFloat width;
@property CGFloat top;
@property CGFloat left;
@property CGFloat bottom;
@property CGFloat right;
- (void) moveBy: (CGPoint) delta;
- (void) scaleBy: (CGFloat) scaleFactor;
- (void) fitInSize: (CGSize) aSize;
@end

@implementation UIView (ViewGeometry)
// Retrieve and set the origin
- (CGPoint) origin
{
return self.frame.origin;
}

- (void) setOrigin: (CGPoint) aPoint
{
CGRect newframe = self.frame;
newframe.origin = aPoint;
self.frame = newframe;
}

// Retrieve and set the size
- (CGSize) size
{
return self.frame.size;
}

- (void) setSize: (CGSize) aSize
{
CGRect newframe = self.frame;
newframe.size = aSize;
self.frame = newframe;
}

// Query other frame locations
- (CGPoint) bottomRight
{
CGFloat x = self.frame.origin.x + self.frame.size.width;
CGFloat y = self.frame.origin.y + self.frame.size.height;
return CGPointMake(x, y);
}

- (CGPoint) bottomLeft
{
CGFloat x = self.frame.origin.x;
CGFloat y = self.frame.origin.y + self.frame.size.height;
return CGPointMake(x, y);
}

- (CGPoint) topRight
{
CGFloat x = self.frame.origin.x + self.frame.size.width;
CGFloat y = self.frame.origin.y;
return CGPointMake(x, y);
}

// Retrieve and set height, width, top, bottom, left, right
- (CGFloat) height
{
return self.frame.size.height;
}

- (void) setHeight: (CGFloat) newheight
{
CGRect newframe = self.frame;
newframe.size.height = newheight;
self.frame = newframe;
}

- (CGFloat) width
{
return self.frame.size.width;
}

- (void) setWidth: (CGFloat) newwidth
{
CGRect newframe = self.frame;
newframe.size.width = newwidth;
self.frame = newframe;
}

- (CGFloat) top
{
return self.frame.origin.y;
}

- (void) setTop: (CGFloat) newtop
{
CGRect newframe = self.frame;
newframe.origin.y = newtop;
self.frame = newframe;
}

- (CGFloat) left
{
return self.frame.origin.x;
}

- (void) setLeft: (CGFloat) newleft
{
CGRect newframe = self.frame;
newframe.origin.x = newleft;
self.frame = newframe;
}

- (CGFloat) bottom
{
return self.frame.origin.y + self.frame.size.height;
}

- (void) setBottom: (CGFloat) newbottom
{
CGRect newframe = self.frame;
newframe.origin.y = newbottom - self.frame.size.height;
self.frame = newframe;
}

- (CGFloat) right
{
return self.frame.origin.x + self.frame.size.width;
}

- (void) setRight: (CGFloat) newright
{
CGFloat delta = newright - (self.frame.origin.x + self.frame.size.width);
CGRect newframe = self.frame;
newframe.origin.x += delta;
self.frame = newframe;
}
@end


Other  
  •  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
  •  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
  •  
    Most View
    Spring Is Here (Part 2)
    Is 802.11ac Worth Adopting?
    BlackBerry Z10 - A Touchscreen-Based Smartphone (Part 1)
    LG Intuition Review - Skirts The Line Between Smartphone And Tablet (Part 5)
    Fujifilm X-E1 - A Retro Camera That Inspires (Part 4)
    My SQL : Replication for High Availability - Procedures (part 6) - Slave Promotion - A revised method for promoting a slave
    10 Contenders For The 'Ultimate Protector' Crown (Part 3) : Eset Smart Security 6, Kaspersky Internet Security 2013, Zonealarm Internet Security 2013
    HTC Desire C - Does It Have Anything Good?
    Windows Phone 7 : Understanding Matrix Transformations (part 2) - Applying Multiple Transformations
    How To Lock Windows By Image Password
    REVIEW
    - First look: Apple Watch

    - 10 Amazing Tools You Should Be Using with Dropbox
    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 BlackBerry Android Ipad Iphone iOS
    Top 10
    OPEL MERIVA : Making a grand entrance
    FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
    BMW 650i COUPE : Sexy retooling of BMW's 6-series
    BMW 120d; M135i - Finely tuned
    PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
    PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
    Java Tutorials : Nested For Loop (part 2) - Program to create a Two-Dimensional Array
    Java Tutorials : Nested For Loop (part 1)
    C# Tutorial: Reading and Writing XML Files (part 2) - Reading XML Files
    C# Tutorial: Reading and Writing XML Files (part 1) - Writing XML Files