concept moment in category ios

appears as: moment
iOS Development with Swift

This is an excerpt from Manning's book iOS Development with Swift.

You saw the range operator earlier, when you used it to extract a range of elements from an Array. You’ll explore another use of range in a switch statement in a moment.

  • You can add this x,y coordinate to the image view’s current position to move the image. Use the UIView’s center property to set the image view’s current position:
    imageView.center = CGPoint(
        x: imageView.center.x + translation.x,
        y: imageView.center.y + translation.y)
    Because gesture recognizers report on movement since the moment a gesture began, and the center property reports on the current location of the image view, if we continue adding the gesture movement to the image view location every time the gesture recognizer reports a movement, the image view will move exponentially. To illustrate this, consider if the image view begins at (x:0, y:0). The first time the gesture recognizer is called, the translation may be a movement of (x:1, y:1), so the image view is moved to (x:1, y:1). The second time the gesture recognizer is called, the translation may have moved another 1 point in the x direction and 1 point in the y direction, so the translation (representing the movement from the moment the gesture began) will be (x:2, y:2). The new location of the image view should be (x:2, y:2) but following the code above, instead it will be (x:3, y:3). What can be done about this? There are two possible solutions:
    • You could use the gesture recognizer’s state property to detect when the gesture begins, and at this point record the initial location of the view. You could then base all view movement calculations from this initial location rather than the view’s current location.
    • You could reset the gesture recognizer every time you respond to a gesture event, so that the gesture recognizer now reports on movement since the last pan gesture event.
    Let’s follow the second solution.
  • You’ll use system buttons to make your distance converter app interactive. The user can convert the number of miles they’ve entered to kilometers or vice versa (see figure 5.2). Okay, we’re not going to win any design awards, but we’re focused on functionality for the moment.