Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MapStack - lesson 5

leveton
March 11, 2016
34

MapStack - lesson 5

leveton

March 11, 2016
Tweet

Transcript

  1. UIGestureRecognizers We’ll add two gesture recognizers to the detail view.

    A tap gesture will show and hide the distance label while a pan gesture will allow us to move the dismiss button around the detail view. Recognizers are easy to set up and offer a powerful API for developers to utilize
  2. UIGestureRecognizers Looking at MSLocationDetailViewController, it takes roughly 16 lines of

    code to set up the gesture recognizers and assign them to views. Some Caveats: 1) A gesture recognizer can be added to any subclass of UIView 2) Two views cannot share a gesture recognizer. Last one in! 3) Often you have to set a view’s userInteractionEnabled property to true. 4) Do not overuse gesture recognizers - see Apple’s Human Interface Guidelines. 5) Notice that the dismiss button still fires even after you pan it around the view, this fact has allowed many a developer to create simple 2D games.
  3. Blocks As app devs, we want it both ways -

    responsive UI backed by computationally heavy programs. For this our main tool is the block...
  4. Blocks Notice the ‘completion’ argument in dismissViewControllerAnimated:. The carrot and

    curly braces means it’s a block type variable but what is that exactly... A block variable encapsulates code that is executed at either a different time, on a different thread, or both. In our case, the code in the block isn’t executed until the view controller leaves the Window.
  5. Blocks When you tap the dismiss button, your console will

    log out “reached end of didTapDimiss scope” before “completion block fired”. This is counter to what normally happens where code is executed sequentially from top to bottom.
  6. Blocks The view controller (self in our case) knows not

    to execute the block variable until some event has occurred. In our case, the event is the view controller sliding off of the main window. A block doesn’t have to be inline. We can declare it as a separate variable:
  7. Blocks If you think about this a moment, you can

    see just how powerful it is. This is just scratching the surface of block programming. Go more in depth.
  8. animations Notice when we double tap our title label, that

    the distance label is crowded right below the title label. Also the user experience of a label just popping into view could be better.
  9. animations Fix this by uncommenting everything inside animateDistanceLabel. The uncommented

    code is a custom animation. A class method on UIView that takes two blocks handling both the animation and the code to execute when the animation finishes.
  10. animations Just like with gesture recognizers, the engineers at Apple

    have done a ton of work to create an API that is simple for us to implement. Just toss your desired animation inside the curly braces of the animation block and let UIKit do the rest:
  11. animations In our case, the ‘frame’ value has a different

    y-offset that the distance labels frame. The difference will be animated after a tenth of a second. The animation will occur for .3 seconds.
  12. animations Like gesture recognizers and blocks, this is the tip

    of the iceberg. Go more in depth by researching the options parameter animateWithDuration. Look at pitfalls of animations e.g. your app’s process being interrupted causing the animation to fail.
  13. The singleton pattern Globals are evil, right? The changing of

    a global variable can have disastrous effects. So mostly yes, but sometimes the alternative to have every class keep a property. Also there are times when you want a variable to change without the class that uses it to have to do any work.
  14. The singleton pattern Looking at MSSingleton.m we see some complicated

    code. This is all to prevent the object from being allocated more than once.
  15. The singleton pattern dispatch_once will stop the main thread and

    initialize the singleton. the @synchronized compiler directive will prevent other objects from accessing this class while the initialization is taking place.
  16. The singleton pattern Looking back at our detail view, we

    access the singleton’s themeColor property and set the view’s background color as such. Any singleton that’s an object should be checked for existence before use.
  17. The singleton pattern If this theme color is reassigned: Then

    any object in the app that is using the theme color will now be working with a red theme color.
  18. The singleton pattern We set the theme color in the

    appDelegate in didFinishLaunchingWithOptions so that’s it’s set before any of the app’s views are laid out.
  19. Sorting collections Let’s present our table logically by either sorting

    by title or distance. NSSortDescriptor is magic - it can sort by primitives (CGFloat distance) and certain objects (NSString *title). Here we sort alphabetically from lowest to highest from the top of the table (the 0th index)
  20. Sorting collections Change the key and the ascending parameter to

    get the table sorted by distance from highest to lowest. Notice that ‘title’ and ‘distance’ are completely different types. Under the hood, sortDescriptorWithKey determines the type and goes from there.