iOS开发知识点iOS常用

iOS15 UISheetPresentationControl

2021-07-26  本文已影响0人  frola_

代码


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.selectImageView];
    [self.view addSubview:self.imagePickerButton];
    [self.view addSubview:self.customVCButton];
}

- (void)openImagePicker:(UIButton *)button {
    [self openViewController:self.imagePicker button:button];
    
}

- (void)openCustomViewController:(UIButton *)button{
    [self openViewController:self.tableViewVC button:button];
}

- (void)openViewController:(UIViewController *)vc button:(UIButton *)button{
    if (!_isOpen) {
        
        UIPopoverPresentationController *popover = vc.popoverPresentationController;
        UISheetPresentationController *sheet = popover.adaptiveSheetPresentationController;
        sheet.detents = @[UISheetPresentationControllerDetent.mediumDetent,
                          UISheetPresentationControllerDetent.largeDetent ];
        sheet.smallestUndimmedDetentIdentifier = UISheetPresentationControllerDetentIdentifierMedium;
        sheet.prefersScrollingExpandsWhenScrolledToEdge = false;
        sheet.prefersEdgeAttachedInCompactHeight = true;
        sheet.widthFollowsPreferredContentSizeWhenEdgeAttached = true;
        
        [self presentViewController:vc animated:YES completion:^{
            [button setTitle:@"close" forState:UIControlStateNormal];
        }];
    } else {
        [vc dismissViewControllerAnimated:NO completion:^{
            [button setTitle:@"open" forState:UIControlStateNormal];
        }];
    }
    
    _isOpen = !_isOpen;
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [_customVCButton setTitle:@"open" forState:UIControlStateNormal];
    [_imagePickerButton setTitle:@"open" forState:UIControlStateNormal];
    _isOpen = false;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController {
    [_customVCButton setTitle:@"open" forState:UIControlStateNormal];
    [_imagePickerButton setTitle:@"open" forState:UIControlStateNormal];
    _isOpen = false;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.selectImageView setImage:image];
}

- (UIImagePickerController *)imagePicker {
    if(!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.delegate = self;
        _imagePicker.modalPresentationStyle = UIModalPresentationPopover;

    }
    return _imagePicker;
}

- (TableViewViewController *)tableViewVC {
    if (!_tableViewVC) {
        _tableViewVC = [[TableViewViewController alloc] init];
        _tableViewVC.modalPresentationStyle = UIModalPresentationPopover;
    }
    return _tableViewVC;
}

- (UIButton *)imagePickerButton {
    if (!_imagePickerButton) {
        _imagePickerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 84, self.view.frame.size.width/2, 40)];
        _imagePickerButton.backgroundColor = [UIColor clearColor];
        _imagePickerButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_imagePickerButton addTarget:self action:@selector(openImagePicker:) forControlEvents:UIControlEventTouchUpInside];
        [_imagePickerButton setTitle:_isOpen ? @"lose" : @"open" forState:UIControlStateNormal];
        [_imagePickerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _imagePickerButton;
}

- (UIButton *)customVCButton {
    if (!_customVCButton) {
        
        _customVCButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, 84, self.view.frame.size.width/2, 40)];
        _customVCButton.backgroundColor = [UIColor clearColor];
        _customVCButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_customVCButton addTarget:self action:@selector(openCustomViewController:) forControlEvents:UIControlEventTouchUpInside];
        [_customVCButton setTitle:_isOpen ? @"close" : @"open" forState:UIControlStateNormal];
        [_customVCButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _customVCButton;
}

- (UIImageView *)selectImageView {
    if(!_selectImageView) {
        double height = MIN(self.view.frame.size.height /2, self.view.frame.size.width)  - 100 - 40;
        _selectImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 84 + 40, height, height)];
        _selectImageView.layer.cornerRadius = 20;
        _selectImageView.layer.masksToBounds = YES;
    }
    return _selectImageView;
}

效果

效果图.png
上一篇下一篇

猜你喜欢

热点阅读