使用 Objective C 检测 imagePickerController 是否通过向下滑动手势关闭

vud*_*906 0 objective-c uiimagepickercontroller ios

我试图检测通过滑动的 imagePickerController 何时通过向下滑动手势或通过取消关闭。

imagePicker 通过此方法加载(https://developer.apple.com/documentation/uikit/uiviewcontroller/1621380-presentviewcontroller

[rootViewController presentViewController:picker animated:animated completion:NULL];
Run Code Online (Sandbox Code Playgroud)

我们可以通过实现此方法简单地检测 pickerController 是否通过取消关闭(https://developer.apple.com/documentation/uikit/uiimagepickercontrollerdelegate/1619133-imagepickercontrollerdidcancel

但是,我还想检测它是否通过向下滑动关闭(对于 iPhone X,...,我们可以向下滑动以关闭以模式方式显示的视图)

使用 Swift,我可以用以下代码检测它:

extension UIImagePickerController {
    open override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        // detecting
    }
}

Run Code Online (Sandbox Code Playgroud)

我想知道在 Objective C 中是否有等效的方法(因为我正在从事的项目是用 Objective C 编写的)?或欢迎任何其他建议:D

Tyl*_*ler 7

首先,你应该覆盖viewDidDisappear:(也不应该在 Objective-C 的类别中重写)。覆盖扩展/类别中的某些内容的结果是未定义的行为 - 它可能有效,也可能无效。永远不应该依赖它。

相反,将图像选择器的委托分配presentationController给某个类,然后让该类实现该presentationControllerDidDismiss:方法。当用户在所有动画完成后成功关闭图像选择器时,将调用此方法。(并请注意,这不是如果以编程方式关闭图像选择器,则

这是一个简短的示例,涵盖了关闭图像选择器的所有情况,而无需viewDidDisappear:在扩展程序或类别中进行覆盖:

@interface ViewController() <UIAdaptivePresentationControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@end

@implementation ViewController

- (IBAction)showImagePicker {
    UIImagePickerController *imagePicker = [UIImagePickerController new];
    imagePicker.delegate = self;
    imagePicker.presentationController.delegate = self;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark - UIAdaptivePresentationControllerDelegate

- (void)presentationControllerWillDismiss:(UIPresentationController *)presentationController {
    NSLog(@"The user began to swipe down to dismiss.");
}

- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController {
    NSLog(@"The dismissal animation finished after the user swiped down.");

    // This is probably where you want to put your code that you want to call.
}

#pragma mark - UIImagePickerControllerDelegate, UINavigationControllerDelegate

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    NSLog(@"The user tapped the image picker's Cancel button.");
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"The dismissal animation finished after the user tapped Cancel.");
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
    NSLog(@"The user selected an image from the image picker.");
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"The dismissal animation finished after the user selected an image.");
    }];
}

@end
Run Code Online (Sandbox Code Playgroud)

这里有一个 Swift 版本:

@interface ViewController() <UIAdaptivePresentationControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@end

@implementation ViewController

- (IBAction)showImagePicker {
    UIImagePickerController *imagePicker = [UIImagePickerController new];
    imagePicker.delegate = self;
    imagePicker.presentationController.delegate = self;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark - UIAdaptivePresentationControllerDelegate

- (void)presentationControllerWillDismiss:(UIPresentationController *)presentationController {
    NSLog(@"The user began to swipe down to dismiss.");
}

- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController {
    NSLog(@"The dismissal animation finished after the user swiped down.");

    // This is probably where you want to put your code that you want to call.
}

#pragma mark - UIImagePickerControllerDelegate, UINavigationControllerDelegate

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    NSLog(@"The user tapped the image picker's Cancel button.");
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"The dismissal animation finished after the user tapped Cancel.");
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
    NSLog(@"The user selected an image from the image picker.");
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"The dismissal animation finished after the user selected an image.");
    }];
}

@end
Run Code Online (Sandbox Code Playgroud)