Kicking it with Auto Layout

OS X Lion introduced a new layout system for Cocoa, replacing the historical springs and struts model.  iOS 6 adopts this system.
Autolayout is a constraint-based layout engine that can handle an amazing variety of user interfaces.

Yesterday, I reviewed CS193p Lecture 8 and then took about 5 minutes to make my Card Playing View all nice in Autolayout.

Prof Hegarty isn’t kidding when he calls it whack-a-mole system since changing one constraint introduces another item to review in a chain of constraints. But it is a speedy way nonetheless to get something working for an iPhone 4 or 5 or iPad Retina or mini.

If you’d like further information on Auto Layout there are the following three great WWDC 2012 Sessions (that’s close to 3 hours of Auto Layout):

  • Session 202: Introduction to Auto Layout for iOS and OS X: Come on in, the water’s fine!
  • Session 228: Best Practices for Mastering Autolayout for OS X and iOS
  • Session 232: Autolayout By Example

Also, Ben Scheirman’s NSScreencast Episode #35, is a great, quick, 13 minute tour of Autolayout for subscribers.

Fun with UICollectionView

Introduced into iOS 6, and like its OS X 10.5 cousin NSCollectionView, I spent some time yesterday with UICollectionView.

I made a PlayingCard View in a PlayingCardGame be a UICollectionViewCell of a UICollectionView.

Here is my tap gesture to the ViewController that flips the playing card:

- (IBAction)flipCard:(UITapGestureRecognizer *)gesture
{
   CGPoint tapLocation = [gesture locationInView:self.cardCollectionView];
   NSIndexPath *indexpath = [self.cardCollectionView indexPathForItemAtPoint:tapLocation];
   if (indexpath) {
      [self.game flipCardAtIndex:indexpath.item];
      self.flipCount++;
      self.gameResult.score = self.game.score;
      [self updateUI];
   }
}

This result is some very clean code.

Tomorrow, Autolayout.

Swipe & Pinch

My first Swipe and Pinch methods yesterday.
This is the pinch from my View from my HW #3:

- (void)pinch:(UIPinchGestureRecognizer *)gesture
{
   if ((gesture.state == UIGestureRecognizerStateChanged) ||
       (gesture.state == UIGestureRecognizerStateEnded)) {
   self.faceCardScaleFactor *= gesture.scale;
   gesture.scale = 1; // reset
    }
}

And here is the Swipe from my Controller with a block for animating the card flip (line 6-9):

- (IBAction)swipe:(UISwipeGestureRecognizer *)sender
{
   [UIView transitionWithView:self.playingCardView
                     duration:0.5
                      options:UIViewAnimationOptionTransitionFlipFromLeft
                   animations:^{
                       if (!self.playingCardView.faceUp)
                             [self drawRandomPlayingCard];
                       self.playingCardView.faceUp = !self.playingCardView.faceUp;
                               }
                   completion:NULL];
}

Next UICollectionView.

3-1-13

Somewhat of a palindrome day, but not quite, today is a day for beginnings.

I am taking the Winter 2013 Stanford CS193p iOS Development course on iTunes U with the legendary Paul A Hegarty (former VP of Engineering at NeXT, close associate to Steve Jobs, co-founder of ORM (Operating Resource Management) pioneer Ariba, and author of AppKit) in order to teach myself all the new iOS 6 SDK & Xcode updates such as (but not limited to):

  • Storyboards,
  • Autolayout,
  • NSAttributedString’s new methods,
  • UICollectionView,
  • Allocating arrays on the fly,
  • @property updates (goodbye synthesize – almost).

Today I’m starting Assignment #3: Graphical Set.

I’ll post the code to the following GitHub repo when I’m done (where I already have my first two assignments checked-in):
https://github.com/duliodenis/cs193p-Winter-2013-hw3