代码之家  ›  专栏  ›  技术社区  ›  Deep Shah

自定义按钮在AVPlayer iOS 11中不起作用,但在iOS 11下却能完美工作?

  •  1
  • Deep Shah  · 技术社区  · 7 年前

    我知道这可能是重复的问题,但已经存在的问题没有解决方案,所以,我再次要求可能开始悬赏问题。

    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    
    // create a player view controller
    avcontroller = [[AVPlayerViewController alloc]init];
    avcontroller.player = player;
    [player play];
    
    
    NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
    [noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
                            object:nil
                             queue:nil
                        usingBlock:^(NSNotification *note) {
    
                            NSLog(@"Video end");
                            self.avcontroller.player = nil;
                            [self.avcontroller.view removeFromSuperview];
                            [self.view addSubview:delegate.tabViewControllerObj.tabBarController.view];
                        }];
    
    _skip =[UIButton buttonWithType:UIButtonTypeCustom];
    [_skip addTarget:self action:@selector(btnSkipTapped)
     forControlEvents:UIControlEventTouchUpInside];
    [_skip setTitle:@"Skip" forState:UIControlStateNormal];
    _skip.layer.borderWidth = 2.0f;
    _skip.layer.cornerRadius = 12; 
    _skip.clipsToBounds = YES;
    _skip.layer.borderColor = [UIColor whiteColor].CGColor;
    
    // show the view controller
    [self addChildViewController:avcontroller];
    [avcontroller.view addSubview:_skip];
    [avcontroller.view bringSubviewToFront:_skip];
    [self.view addSubview:avcontroller.view];
    avcontroller.view.frame = self.view.frame;
    
    }
    
    -(void)viewDidLayoutSubviews{
      [super viewDidLayoutSubviews];
    
     _skip.frame = CGRectMake((self.view.frame.size.width-140),(self.view.frame.size.height-150),100.0,35.0);
    }
    

    这是按钮点击功能:

    - (IBAction)btnSkipTapped{
    
    NSLog(@"Skip button clicked");
    self.avcontroller.player = nil;
    [self.avcontroller.view removeFromSuperview];
    [self.view addSubview:delegate.tabViewControllerObj.tabBarController.view];
    }
    

    此代码将完美地显示按钮,但点击按钮时,它并没有调用btnSkipTapped函数。 任何帮助都将不胜感激!

    1 回复  |  直到 7 年前
        1
  •  3
  •   Nirav Kotecha    7 年前

    @DeepShah您在中添加了跳过按钮 avcontroller.view 所以它的显示正确,但你不能点击它,因为 AVPlayerViewControllerContentView 覆盖跳过按钮。

    见下图:

    image

    因此,您必须在中添加跳过按钮 self.view 及其作品。

    [self.view addSubview:_skip];
    [self.view bringSubviewToFront:_skip];