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

MapSwift - lesson 7

leveton
May 24, 2017
51

MapSwift - lesson 7

leveton

May 24, 2017
Tweet

Transcript

  1. Blocks As app devs, we want it both ways -

    responsive UI backed by computationally heavy programs. For this our main tool is the block...
  2. Blocks Notice the ‘completion’ argument in dismiss(animated: completion:). The curly

    braces and the ‘in’ 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.
  3. 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.
  4. 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:
  5. Blocks Think about this a moment. How powerful is it

    that you can call code based on events? This is just scratching the surface of block programming. Go more in depth.
  6. Presenting a modal is one of the two main ways

    of presenting new views onto existing views in iOS. We’ll go over the other (popping a view) later.
  7. Our detail view is pretty ugly Let’s fill out our

    detail view with some cool animations and gesture recognizers.
  8. 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 image view around the detail’s view. Recognizers are easy to set up and offer a powerful API for developers to utilize
  9. UIGestureRecognizers Looking at MSLocationDetailViewController, we 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 isUserInteractionEnabled property to true. 4) Do not overuse gesture recognizers - see Apple’s Human Interface Guidelines. 5) If you were to add ‘pan’ to the dismiss button, it still fires even after you pan it around the view, this fact has allowed many a developer to create simple 2D games.
  10. 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.
  11. 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.
  12. animations Just like with gesture recognizers, Apple engineers 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:
  13. animations In our case, the ‘frame’ value has a different

    y-offset than the distance label’s frame. The difference will be animated after a tenth of a second. The animation will occur for .3 seconds.