在Swift

时间:2015-12-30 09:23:32

标签: ios swift uikit

我的目标是不在所有控制器中编写滑动检测代码。所以我所做的就是创造课程:" Swipes"像这样:

class Swipes{

private var leftController: String
private var rightController: String
private var target: UIViewController

init(leftController: String, rightController: String, target: UIViewController){
    self.leftController = leftController
    self.rightController = rightController
    self.target = target
}

func initLeftRightSwipes(){
    let swipeRight = UISwipeGestureRecognizer(target: target, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    target.view.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: target, action: "respondToSwipeGesture:")
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    target.view.addGestureRecognizer(swipeLeft)
}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    print("swipe detected")
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {


        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.Right:
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier(rightController) 
            target.presentViewController(vc, animated: false, completion: nil)

        case UISwipeGestureRecognizerDirection.Down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.Left:
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier(leftController)
            target.presentViewController(vc, animated: false, completion: nil)

        case UISwipeGestureRecognizerDirection.Up:
            print("Swiped up")
        default:
            break
        }
    }
  }
}

在我的视图控制器中:

let swipe = Swipes.init(leftController: "popularID", rightController: "newID", target: self)
swipe.initLeftRightSwipes()

然而,这不起作用。我收到unrecognized selector sent to instance错误,因为选择器指向一个不在控制器中的函数。它可以处理控制器外的滑动吗?我一直在研究,但一无所获。

我还尝试过将UISwipeGestureRecognizer中的目标设置为self,但将view.addGestureRecognizer中的视图设置为当前控制器的视图。那也失败了。

非常感谢任何帮助或指导。

1 个答案:

答案 0 :(得分:1)

创建一个父SwipeViewController,如:

class SwipeViewController: UIViewController{

    private var leftController: String?
    private var rightController: String?

    func addSwipes(leftController: String, rightController: String){

        self.leftController = leftController
        self.rightController = rightController

        let swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeRight.direction = UISwipeGestureRecognizerDirection.Right
        self.view.addGestureRecognizer(swipeRight)

        let swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
        self.view.addGestureRecognizer(swipeLeft)
    }

    func respondToSwipeGesture(gesture: UIGestureRecognizer) {
        print("swipe detected")
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {


            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let vc = storyboard.instantiateViewControllerWithIdentifier(rightController!)
                self.presentViewController(vc, animated: false, completion: nil)

            case UISwipeGestureRecognizerDirection.Down:
                print("Swiped down")
            case UISwipeGestureRecognizerDirection.Left:
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let vc = storyboard.instantiateViewControllerWithIdentifier(leftController!)
                self.presentViewController(vc, animated: false, completion: nil)

            case UISwipeGestureRecognizerDirection.Up:
                print("Swiped up")
            default:
                break
            }
        }
    }
}

现在,在View Controller中,您需要在SwipeViewController的子类中滑动make:

class ViewController: SwipeViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

     //Here simply call addSwipes Function:

       self.addSwipes("view1", rightController: "view2")
    }