CGAffineTransformScale in Swift 3.0
Asked Answered
H

2

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?

Halflight answered 17/4, 2017 at 18:8 Comment(0)
U
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)
Unchancy answered 17/4, 2017 at 18:20 Comment(4)
Thanks, @Alexander. Is there anything in my questions that I should write to make it better?Halflight
@Halflight 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.Washrag
@Halflight This is the case for many structs, like CGAffineTransform, CGPoint, CGRect, etc.Washrag
Thanks, @Alexander. I will definitely be more diligent about Googling in the future.Halflight
S
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)
    ...
  }
Sydelle answered 17/4, 2017 at 18:30 Comment(1)
That just makes a new transform altogether.Washrag

© 2022 - 2024 — McMap. All rights reserved.