ios - 在 UIImagePickerController 上放置滑动手势

标签 ios objective-c cocoa-touch uiimagepickercontroller uiswipegesturerecognizer

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

我收到以下错误:

VideoStream[13065:60b] -[UILayoutContainerView toggleControlsWithGesture]: unrecognized selector sent to instance 0x1701a1960 2014-04-26 11:37:28.639 VideoStream[13065:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[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;
    }
}

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

最佳答案

代替:

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 方法。

关于ios - 在 UIImagePickerController 上放置滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313132/

相关文章:

objective-c - UITableView 使用 Storyboard 深入了解 UITableView 的详细信息

javascript - Objective-C 从网页获取图像

iphone - 使用适用于 iOS 的 OpenGL ES 绘制正方形

Objective-C 和 Windows

objective-c - Objective-C - 如何从 NSDate 获取工作日?

ios - iOS编程中如何定义弱属性的setter方法?

cocoa-touch - -[UITableView scrollToRowAtIndexPath :] scrolls, 但返回到第一行

IOS - 使用自定义 UI TableViewCell 作为 TableFooter

ios - 为什么我不能摆脱 libstdc++?

iOS/Xcode : How to simulate network connection between two iOS simulators