UISwipeGestureRecognizer 不执行方法

UISwipeGestureRecognizer Not Performing Method

我很难让滑动手势识别器在我的应用程序上运行。这是所有内容的层次结构。

应用程序的根视图是一个 UINavigationController,其中 class ViewController 对于第一个视图可见。我有一个 UIButton,它会触发一个循环播放的电影,直到我点击它两次,这将使导航控制器将我制作的名为 PUPPETS1 的新 ViewController 推送到屏幕上。这个 VC 有自己的 xib。 xib 有一个 UIImageView。我想要发生的事情是在屏幕上向上滑动后开始播放电影,但这从未发生过,并且控制台从不显示来自第二个 VC 方法的 NSLog。

- (void)loopVideo {

    NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"warpspeed" withExtension:@"mov"];
    UIView *patternView = [[UIView alloc] initWithFrame:self.view.bounds];
    patternView.backgroundColor = [UIColor blackColor];
    [self.moviePlayer2.backgroundView addSubview:patternView];
    self.moviePlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
    [self.moviePlayer2 setControlStyle:MPMovieControlStyleDefault];

    self.moviePlayer2.controlStyle = MPMovieControlStyleNone;
    self.moviePlayer2.scalingMode = MPMovieScalingModeAspectFill;
    self.moviePlayer2.movieSourceType = MPMovieSourceTypeFile;
    [self.moviePlayer2 setAllowsAirPlay:YES];
    self.moviePlayer2.view.frame = self.view.frame;


    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(puppetOne)];
    tapGesture.numberOfTapsRequired = 2;
    tapGesture.numberOfTouchesRequired = 1;

    UIView *aView = [[UIView alloc] initWithFrame:self.moviePlayer2.backgroundView.bounds];
    [aView addGestureRecognizer:tapGesture];

    [self.view.window addSubview:aView];
    [self.view addSubview:self.moviePlayer2.view];
    self.moviePlayer2.repeatMode = MPMovieRepeatModeOne;
    [self.moviePlayer2 play];

}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;

}

第2个VC中的PUPPETS1:

- (void)viewDidLoad {
    [super viewWillAppear:YES];
    UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(playPuppets)];

    swipeRec.direction = UISwipeGestureRecognizerDirectionUp;


     UIView *aView2 = [[UIView alloc] initWithFrame:self.view.bounds];
    [aView2 addGestureRecognizer:swipeRec];
    [self.view addSubview:aView2];
    // Do any additional setup after loading the view from its nib.
}




-(void)playPuppets {
    NSLog(@"PLAYING");
    NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"SundayPuppets" withExtension:@"m4v"];

    //filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    AVPlayerViewController *playerViewController = [AVPlayerViewController new];
    playerViewController.player = player;
    //[playerViewController.player play];//Used to Play On start
    [self presentViewController:playerViewController animated:YES completion:nil];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;

}

您要将 gr 附加到的视图的框架取决于它的父视图的边界,而这些边界尚未在 viewDidLoad 中初始化。 将设置移动到布局完成后(并在第一次布局更改时将其调整为 运行 一次),即

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    UIView *swipeView = [self.view viewWithTag:999];
    // only do this if we haven't done it already
    if (!swipeView) {
        // now that self.view.bounds is initialized...
        swipeView = [[UIView alloc] initWithFrame:self.view.bounds];
        swipeView.tag = 999;

        // the rest of your OP setup code is fine, and goes here
        UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(playPuppets)];
        swipeRec.direction = UISwipeGestureRecognizerDirectionUp;
        [swipeView addGestureRecognizer:swipeRec];

        [self.view addSubview:swipeView];
        NSLog(@"%@", swipeView);
    }
}

编辑 上面的代码正确附加了手势识别视图,在第二个视图控制器中提供了修复。事实证明,另一个问题是之前的视图控制器没有正确删除(已弃用)MPMoviePlayer,导致触摸在推送的 vc 上不起作用。整个重做 ViewController.m 可以在下面的聊天链接中找到,但是触摸问题的修复在这里...

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 

    [self.moviePlayer stop]; 
    [self.moviePlayer.view removeFromSuperview]; 
    self.moviePlayer = nil; 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
}