8

Swift 3.0 does not have a method called CGAffineTransformScale.

func didPinchGesture(pinchRecognizer : UIPinchGestureRecognizer) {
  if let view = pinchRecognizer.view {
    view.transform = CGAffineTransformScale(view.transform,
                            recognizer.scale, recognizer.scale)
    pinchRecognizer.scale = 1
  }
}

scaleBy does not autocomplete but added to CGAffineTransform scaleBy throws error, since scale is not a property of CGAffineTransform anymore: CGAffineTransform.scaledBy(view.transform, pinchRecognizer.scale, pinchRecognizer.scale).

What is the best way to configure the pinch gesture recognizer with Swift 3.0?

Eric
  • 893
  • 10
  • 25

2 Answers2

20

In Swift, CGAffineTransformScale is imported as an instance method on the CGAffineTransform struct, called scaledBy(x:y:):

view.transform = view.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
Alexander
  • 59,041
  • 12
  • 98
  • 151
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Thanks, @Alexander. Is there anything in my questions that I should write to make it better? – Eric Apr 17 '17 at 18:29
  • @Eric Not really, but it would be good to be a bit more diligent about googling this stuff before answering. A lot of Cocoa APIs were implemented as C structs (like `CGAffineTransform`), with a suite of global functions that act on them (like `CGAffineTransformScale`), because they offered faster performance than Objective C classes/instance methods. Swift supports structs natively, and support instance methods on structs, so such C APIs are imported as native Swift structs, and their corresponding global functions are imported as instance methods on those native Swift structs. – Alexander Apr 17 '17 at 18:37
  • @Eric This is the case for many structs, like `CGAffineTransform`, `CGPoint`, `CGRect`, etc. – Alexander Apr 17 '17 at 18:37
  • Thanks, @Alexander. I will definitely be more diligent about Googling in the future. – Eric Apr 17 '17 at 18:56
1

You can use a scale transform in Swift 3 like this:

if let view = pinchRecognizer.view {
    view.transform = CGAffineTransform(scaleX: recognizer.scale, y: recognizer.scale)
    ...
  }
David S.
  • 6,567
  • 1
  • 25
  • 45