手势识别器不工作

时间:2016-01-20 18:09:04

标签: ios uigesturerecognizer

我已将左右滑动手势识别器添加到图像视图,但只有右侧滑动有效。

    let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeLeft")
            swipeLeftRecognizer.direction = .Left
            let swipeRightRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeRight")
            swipeLeftRecognizer.direction = .Right
            if itemImages.count > 1 {
                imageView.addGestureRecognizer(swipeLeftRecognizer)
                imageView.addGestureRecognizer(swipeRightRecognizer)

                for recognizer in imageView.gestureRecognizers! {
                    recognizer.delegate = self
                }

func swipeRight() {
        if currentIndex == 0 {
            itemImages.last!.getDataInBackgroundWithBlock( {
                (data, error) -> Void in
                self.imageView.image = UIImage(data: data!)
            })

            let transition = CATransition()
            transition.duration = 1.0
            transition.subtype = kCATransitionFromLeft
            transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            transition.type = kCATransitionReveal
            imageView.layer.addAnimation(transition, forKey: nil)
            currentIndex = itemImages.count - 1
        } else {
            itemImages[currentIndex - 1].getDataInBackgroundWithBlock( {
                (data, error) -> Void in
                self.imageView.image = UIImage(data: data!)
            })

            let transition = CATransition()
            transition.duration = 1.0
            transition.subtype = kCATransitionFromLeft
            transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            transition.type = kCATransitionReveal
            imageView.layer.addAnimation(transition, forKey: nil)
            currentIndex--
        }
    }

    func swipeLeft() {
        let index = itemImages.count - 1
        if currentIndex == index {
            itemImages[0].getDataInBackgroundWithBlock( {
                (data, error) -> Void in
                self.imageView.image = UIImage(data: data!)
            })

            let transition = CATransition()
            transition.duration = 1.0
            transition.subtype = kCATransitionFromLeft
            transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            transition.type = kCATransitionReveal
            imageView.layer.addAnimation(transition, forKey: nil)
            currentIndex = 0
        } else {
            itemImages[currentIndex + 1].getDataInBackgroundWithBlock( {
                (data, error) -> Void in
                self.imageView.image = UIImage(data: data!)
            })

            let transition = CATransition()
            transition.duration = 1.0
            transition.subtype = kCATransitionFromLeft
            transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            transition.type = kCATransitionReveal
            imageView.layer.addAnimation(transition, forKey: nil)
            currentIndex++
        }
    }

1 个答案:

答案 0 :(得分:1)

 let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeLeft")
        swipeLeftRecognizer.direction = .Left
        let swipeRightRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeRight")
        swipeRightRecognizer.direction = .Right

///你两次使用' swipeLeftRecognizer.direction'。 现在你的leftSwite工作正确

或者你可以将swipeGesture从故事板转移到你的" swipeLeft"和" swipeRight"方法并且不要在

之前写任何代码