Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this on for size! ;-)</p> <p><em><strong>ViewController.h</em></strong></p> <pre><code>#define BUFFER 10 #define WIDTH 90 #import &lt;UIKit/UIKit.h&gt; @interface ViewController : UIViewController @property (strong, nonatomic) NSMutableArray *ViewArray; - (IBAction)hideViews:(id)sender; - (void)centerVisibleViews; @end </code></pre> <p><em><strong>ViewController.m</em></strong></p> <pre><code>#import "ViewController.h" @implementation ViewController @synthesize ViewArray; - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *colorArray = [NSMutableArray array]; [colorArray addObject:[UIColor redColor]]; [colorArray addObject:[UIColor orangeColor]]; [colorArray addObject:[UIColor yellowColor]]; [colorArray addObject:[UIColor greenColor]]; [colorArray addObject:[UIColor blueColor]]; [colorArray addObject:[UIColor purpleColor]]; [colorArray addObject:[UIColor blackColor]]; [self setViewArray:[NSMutableArray array]]; for (int i = 0; i &lt; 7; i = i + 1) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(BUFFER + ((WIDTH + BUFFER) * i), BUFFER, WIDTH, WIDTH)]; [view setBackgroundColor:[colorArray objectAtIndex:i]]; [[self ViewArray] addObject:view]; [[self view] addSubview:view]; } } - (IBAction)hideViews:(id)sender { for (int i = 0; i &lt; [[self ViewArray] count]; i = i + 1) { if (i == 1 || i == 4) { [(UIView *)[[self ViewArray] objectAtIndex:i] setHidden:YES]; } } [self centerVisibleViews]; } - (void)centerVisibleViews { int visibleViews = 0; for (int i = 0; i &lt; [[self ViewArray] count]; i = i + 1) { if (![(UIView *)[[self ViewArray] objectAtIndex:i] isHidden]) { visibleViews = visibleViews + 1; } } float totalWidth = (BUFFER * 2) + (WIDTH * [[self ViewArray] count]) + (BUFFER * ([[self ViewArray] count] - 1)); float visibleWidth = (BUFFER * 2) + (WIDTH * visibleViews) + (BUFFER * (visibleViews - 1)); // Make it sexy with an animation!!! [UIView animateWithDuration:1.0 animations:^{ float offset = (totalWidth - visibleWidth) / 2.0; for (int i = 0; i &lt; [[self ViewArray] count]; i = i + 1) { UIView *thisView = [[self ViewArray] objectAtIndex:i]; if (![thisView isHidden]) { [thisView setFrame:CGRectMake(offset, BUFFER, WIDTH, WIDTH)]; offset = offset + WIDTH + BUFFER; } } } ]; } @end </code></pre> <p>Only thing to do is add a button to the main view and tie it to the <code>IBOutlet</code>. Nice and pretty on my iPad simulator.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload