MOBILE

iPhone Developer : Assembling Views and Animations - Randomly Moving a Bounded View

12/21/2013 12:27:46 AM

When you move a view to a random point, you must take into account several things. Often a view must fit entirely within its parent’s view container so there aren’t parts of the view clipped off. You may also want to add a boundary to that container so the view does not quite touch the parent’s edge at any time. Finally, if you’re working with out-of-the-box SDK versions of the UIView class, you need to work with random centers, not random positions. Just picking a point somewhere in the parent view fails some or all of these qualifications.

Recipe 1 approaches this problem by creating a series of insets. It uses the UIEdgeInset structure to define the boundaries for the view. This structure contains four inset values, corresponding to the amount to inset a rectangle at its top, left, bottom, and right.

typedef struct {
CGFloat top, left, bottom, right;
} UIEdgeInsets;

This method uses the UIEdgeInsetsInsetRect() function to narrow a CGRect rectangle to create an inner container, which is called innerRect in this method.

It then narrows the container even further. It insets that rectangle by half the child’s height and width. This leaves enough room around any point in the subrectangle to allow the placement of the child view, guaranteeing that the view can do so without overlapping the inner bounded rectangle. Select any point in that subrectangle to return a valid center for the child view.

Recipe 1. Randomly Moving a Bounded View
- (CGPoint) randomCenterInView: (UIView *) aView withInsets: (UIEdgeInsets) insets
{
// Move in by the inset amount and then by size of the subview
CGRect innerRect = UIEdgeInsetsInsetRect([aView bounds], insets);
CGRect subRect = CGRectInset(innerRect,
self.frame.size.width / 2.0f, self.frame.size.height / 2.0f);
// Return a random point
float rx = (float)(random() % (int)floor(subRect.size.width));
float ry = (float)(random() % (int)floor(subRect.size.height));
return CGPointMake(rx + subRect.origin.x, ry + subRect.origin.y);
}

- (CGPoint) randomCenterInView: (UIView *) aView
withInset: (float) inset
{
UIEdgeInsets insets = UIEdgeInsetsMake(inset, inset, inset, inset);
return [self randomCenterInView:aView withInsets:insets];
}

- (void) moveToRandomLocationInView: (UIView *) aView {
self.center = [self randomCenterInView:aView withInset:5];
return;
}


Other  
  •  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
  •  Android Application Development : Rolling Your Own Widgets (part 3) - Canvas Drawing - Drawing text, Matrix transformations
  •  
    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