如何在同一个函数中再次调用某些代码?

时间:2016-12-23 12:31:44

标签: ios swift

我想在相同的函数中再次使用代码而不为它做额外的功能。

而我的pinchGesture是。ended现在我想称这个为法令 (如代码所示)。

    func isParformPinch(recognizer:UIPinchGestureRecognizer, forTag tag:Int, protectedTag protact:Int){

        if protact == tag{
            return
         }

    //THIS PART CALL AGAIN 

        if isPinching(forView: imageArray[tag],inView: recognizer.view!,  fromTransformation: recognizer.scale) {                                
             imageArray[tag].transform = imageArray[tag].transform.scaledBy(x: recognizer.scale, y: recognizer.scale)                     
         }

      if recognizer.state == .ended{
          // call again above condition

       }
    }

2 个答案:

答案 0 :(得分:5)

func isParformPinch(recognizer:UIPinchGestureRecognizer, forTag tag:Int, protectedTag protact:Int){

    if protact == tag{
        return
    }

    do {
        //THIS PART CALL AGAIN

        if isPinching(forView: imageArray[tag],inView: recognizer.view!,  fromTransformation: recognizer.scale) {
            imageArray[tag].transform = imageArray[tag].transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
        }

    }while(recognizer.state == .ended);  // if true, call again above condition

}

答案 1 :(得分:4)

我不确定我是否完全理解你想要达到的目标,但我想你正在寻找的是闭合的概念(相当于Objective-C ):

om.megster.cordova.FileChooser 0.0.0 "File Chooser"
cordova-plugin-app-event 1.2.0 "Application Events"
cordova-plugin-badge 0.7.2 "Badges"
cordova-plugin-compat 1.0.0 "Compat"
cordova-plugin-console 1.0.3 "Console"
cordova-plugin-datepicker 0.9.3 "DatePicker"
cordova-plugin-device 1.1.2 "Device"
cordova-plugin-file 4.1.1 "File"
cordova-plugin-inappbrowser 1.2.1 "InAppBrowser"
cordova-plugin-network-information 1.3.1 "Network Information"
cordova-plugin-splashscreen 3.1.0 "Splashscreen"
cordova-plugin-whitelist 1.2.2 "Whitelist"
ionic-plugin-keyboard 2.1.0 "Keyboard"
phonegap-plugin-push 1.6.3 "PushPlugin"
cordova -v : 6.4.0

我强烈邀请您阅读有关闭包的更多信息,因为它是一个功能强大但又不易操作的功能:https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

编辑:正如 Grimxn 在评论中指出的那样,更简洁的方法是使用本地函数:

func isParformPinch(recognizer:UIPinchGestureRecognizer, forTag tag:Int, protectedTag protact:Int){

        if protact == tag{
            return
         }

       // Declare your block: it's kind of a function-variable
       let myBlock = {
           if isPinching(forView: imageArray[tag],inView: recognizer.view!,  fromTransformation: recognizer.scale) {                                
             imageArray[tag].transform = imageArray[tag].transform.scaledBy(x: recognizer.scale, y: recognizer.scale)                     
           }
        }

       // Execute your block whenever you need to
       myBlock()    

       if recognizer.state == .ended  {
          // call again above condition
          myBlock()
       }
}