1

我在UIView监听屏幕上手势的过渡方法时遇到了一些问题。

发生的事情是,如果我向左滑动或向右滑动,它会向我的@selector 方法发送左右滑动信号......这意味着我无法区分滑动。

这是我有问题的代码。我尝试了一些不同的方法,但似乎无法正确处理。

- (void) setupSwipeGestureRecognizer {
        UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreen:)];
        swipeGesture.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight);
        [self.view addGestureRecognizer:swipeGesture];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"Prototype";
    //Initalizse the swipe gestuer listener
    [self setupSwipeGestureRecognizer];

    //alloc and init
    self.detailViewA = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]]; 
    self.detailViewB = [[DetailViewControllerB alloc]initWithNibName:@"DetailViewControllerB" bundle:[NSBundle mainBundle]];

    // set detail View as first view
    [self.view addSubview:self.detailViewA.view];

    // set up other views
    [self.detailViewB.view setAlpha:1.0f];

    // Add the view controllers view as a subview
    [self.view addSubview:self.detailViewB.view];

    // set these views off screen (right)
    [self.detailViewB.view setFrame:CGRectMake(320, 0, self.view.frame.size.width, self.view.frame.size.height)];    
}


- (void)swipedScreen:(UISwipeGestureRecognizer*)gesture
{
    if (gesture.direction = UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Left");
    }
   if (gesture.direction = UISwipeGestureRecognizerDirectionRight){
       NSLog(@"Right");
   }

}
4

4 回答 4

3

类似的问题在这里这里

swipedScreen:您的方法的参数是类型UISwipeGestureRecognizer,即导致调用回调的识别器。它不是指用户做出的任何实际手势。在您的情况下,您direction将此识别器的属性设置为(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)- 这不会改变。

您必须创建两个识别器,每个方向一个。

于 2012-05-30T08:52:35.450 回答
2

试试这个代码:

(void)swipedScreen:(UISwipeGestureRecognizer*)gesture {
    if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Left");  
    }
    if(gesture.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Right");
    } 
}
于 2012-05-30T08:36:35.110 回答
1

Swift 3 版本代码:

func setupSwipeGestureRecognizer() {

        //For left swipe
        let swipeGestureLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedScreen))
        swipeGestureLeft.direction = .left
        self.view.addGestureRecognizer(swipeGestureLeft)

        //For right swipe
        let swipeGestureRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedScreen))
        swipeGestureRight.direction = .right
        self.view.addGestureRecognizer(swipeGestureRight)

    }

override func viewDidLoad() {
        super.viewDidLoad()
     setupSwipeGestureRecognizer() 
}

func swipedScreen(gesture: UISwipeGestureRecognizer) {

        if gesture.direction == .left {
            print("Left")
        } else if gesture.direction == .right {
            print("Right")
        }
    }
于 2017-03-16T19:25:58.890 回答
0

您没有正确比较您的 if 条件。它应该有 == 作为比较器操作符。您正在使用 = 这使您的条件始终为真。

于 2012-05-30T08:24:23.237 回答