【问题标题】:Place Swipe Gesture over UIImagePickerController将滑动手势放在 UIImagePickerController 上
【发布时间】:2014-04-26 15:46:02
【问题描述】:

我希望在使用摄像机时能够向右滑动以隐藏摄像机控件。视图显示得很好,但是当我滑动时出现错误。

我收到以下错误:

VideoStream[13065:60b] -[UILayoutContainerView toggleControlsWithGesture]:发送到实例的无法识别的选择器 0x1701a1960 2014-04-26 11:37:28.639 VideoStream[13065:60b] * 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[UILayoutContainerView toggleControlsWithGesture]:

代码如下:

- (BOOL)startCameraControllerFromViewController:(UIViewController *)controller usingDelegate:(id)delegate
{
    if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
        || (delegate == nil)
        || (controller == nil)) {
        return NO;
    }

    _cameraUI = [[UIImagePickerController alloc] init];
    _cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
    _cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
    _cameraUI.allowsEditing = NO;
    _cameraUI.delegate = delegate;

    [controller presentViewController:_cameraUI animated:YES completion:nil];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] 
                                            initWithTarget:_cameraUI.view 
                                                    action:@selector(toggleControlsWithGesture)];
    swipeRight.delegate = self;

    //And assuming the "Up" direction in your screenshot is no accident
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [_cameraUI.view addGestureRecognizer:swipeRight];

    return YES;
}

这是刷卡码:

- (void)toggleControlsWithGesture
{
    NSLog(@"BOOM");
    if (_showsControls == YES) {
        _cameraUI.showsCameraControls = NO;
    } else {
        _cameraUI.showsCameraControls = YES;
    }
}

非常感谢您提供的任何帮助。

【问题讨论】:

  • ...-initWithTarget:_cameraUI.view ... 应该是initWithTarget:selfUISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(toggleControlsWithGesture)];

标签: ios objective-c cocoa-touch uiimagepickercontroller uiswipegesturerecognizer


【解决方案1】:

代替:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]
                                        initWithTarget:_cameraUI.view 
                                                action:@selector(toggleControlsWithGesture)];

做:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]
                                        initWithTarget:self
                                                action:@selector(toggleControlsWithGesture)];

解释:

对于您的UISwipeGestureRecognizer 对象,您当前正在执行-initWithTarget:_cameraUI.view

这意味着将响应指定操作方法的目标将是_cameraUI.view _cameraUI.view 不提供任何-toggleControlsWithGesture 方法,因此会出现错误。
-toggleControlsWithGesture 方法将在此类中找到,因此您需要将目标指定为 self


示例:考虑在 XYZClass.m 中,我们执行以下操作:

ABCClass *abcObject = [ABCClass alloc] init];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:abcObject 
                                       action:@selector(doSomething)];

这基本上是说abcObject 将通过触发doSomething 方法来响应事件。
但是...要发生这种情况,ABCClass 应该定义doSomething 方法。

在您的情况下,_cameraUI 属于 UIImagePickerController 类,它的 view 可能是属性或另一个类对象。无论哪种情况,它都没有为您的目的创建的-toggleControlsWithGesture 方法。

【讨论】:

  • 完美运行。谢谢!并感谢您的出色解释!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-15
  • 2013-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多