简体   繁体   English

Swift 2:UIPinchGestureRecognizer中的选择器:如何从另一个类访问func

[英]Swift 2: Selector in UIPinchGestureRecognizer: How to access func from another class

I'm following this class on Swift and building apps. 我正在Swift和构建应用程序上节课。

At 43:30 in the video, the instructor teaches how to set up a UIPinchGestureRecognizer , which takes in a function from another file in its Selector. 视频中的43:30,讲师教您如何设置UIPinchGestureRecognizer ,该UIPinchGestureRecognizer从其Selector中的另一个文件中获取功能。
This is the code the instructor uses: 这是教师使用的代码:

@IBOutlet weak var faceView: FaceView! {
        didSet {
            faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: #selector(FaceView.changeScale(_:))))
            updateUI()
        }
    }

I get 2 errors: 我收到2个错误:

Expected expression in list of expressions, 表达式列表中的期望表达式,

and: 和:

Expected ',' separator. 预期为','分隔符。

I have tried changing #selector to Selector with no luck. 我尝试将#selector更改为Selector ,但没有运气。

the function changeScale : 函数changeScale

func changeScale(recognizer: UIPinchGestureRecognizer)
    {
        switch recognizer.state {
        case .Changed, .Ended:
            scale *= recognizer.scale //scale is the displayed image scale
            recognizer.scale = 1.0
        default:
            break
        }

If I surround the Selector argument with quotes, the app crashes when I pinch, giving the following error: 如果我将Selector参数用引号引起来,则在我捏住应用程序时会崩溃,并出现以下错误:

unrecognized selector sent to instance. 无法识别的选择器已发送到实例。

I am running Xcode 8.1(8B62) and a iPhone 5 simulator on MacBook Air (Ver 100.11.6) 我正在MacBook Air(Ver 100.11.6)上运行Xcode 8.1(8B62)和iPhone 5模拟器

This is the code I used that works 这是我使用的代码

faceView.addGestureRecognizer(UIPinchGestureRecognizer(target:faceView,

action:#selector(FaceView.changeScale(recognizer:))))

When you pinch the face, make sure click "Option + Left Click + Movement on your mousepad". 捏脸时,请确保单击“选项+鼠标左键+移动”。 I make the mistake of just pressing "Option + Movement on the mousepad" and it does not work. 我犯了一个错误,那就是只按“ Option + Movement on mousepad”,它不起作用。

Hope it helps. 希望能帮助到你。

As can be seen in the comments above, the Xcode version is 7.2 and the #selector syntax was introduced in Xcode 7.3 and therefore not available here. 从上面的注释中可以看出,Xcode版本是7.2,并且#selector语法是在Xcode 7.3中引入的,因此在这里不可用。 This just means that you should be able to use the "old" Selector syntax. 这只是意味着您应该能够使用“旧的” Selector语法。

The difference is that you just give pass a strings to the Selector with the name of your function and then a : for each of the parameters your function requires. 所不同的是,您只需要给函数传递一个带有函数名称的字符串,然后传递给函数Selector需要的每个参数: You require one parameter (the recognizer ) so in your case the string looks like this: 您需要一个参数( recognizer ),因此在您的情况下,字符串如下所示:

"changeScale:"

So you'd use: 因此,您将使用:

Selector("changeScale:")

And you end up with: 结果是:

    @IBOutlet weak var faceView: FaceView! {
    didSet {
            faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: Selector("changeScale:")))
        }
    }

As you can see, this is error prone...one typo and kaboom! 如您所见,这容易出错...一个错字和kaboom! Which is why the new #selector syntax is a fine improvement...sorry...not trying to rub it in. 这就是为什么新的#selector语法是一个很好的改进的原因...对不起...不打算尝试使用它。

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM