检测是否用物镜C向下滑动手势关闭了imagePickerController

问题描述 投票:0回答:1

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

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

[rootViewController presentViewController:picker animated:animated completion:NULL];

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

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

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

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

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

ios objective-c uiimagepickercontroller
1个回答
0
投票

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

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

这是一个简短的示例,涵盖了在不覆盖扩展名或类别中的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

这是一个很好的Swift版本:

class ViewController: UIViewController {
    @IBAction func showImagePicker() {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.presentationController?.delegate = self
        present(imagePicker, animated: true)
    }
}

extension ViewController: UIAdaptivePresentationControllerDelegate {
    func presentationControllerWillDismiss(_ presentationController: UIPresentationController) {
        print("The user began to swipe down to dismiss.")
    }

    func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
        print("The dismissal animation finished after the user swiped down.")

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

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        print("The user tapped the image picker's Cancel button.")
        dismiss(animated: true) {
            print("The dismissal animation finished after the user tapped Cancel.")
        }
    }

    func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
        print("The user selected an image from the image picker.")
        dismiss(animated: true){
            print("The dismissal animation finished after the user selected an image.")
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.