UIPresentationController

本文介绍如何在iOS应用中实现自定义视图控制器之间的过渡动画效果,包括使用自定义的`MyPresentationConytollrt`来控制模态视图的显示与隐藏过程,并通过`TestViewController`演示具体的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <UIKit/UIKit.h>
 
@interface ViewController : UIViewController
 
 
@end
————————————————————————————————————————————————————————————————————————————————————————————
#import "ViewController.h"
#import "TestViewController.h"
 
@interface ViewController ()
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor  = [UIColor brownColor];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    btn.backgroundColor = [UIColor yellowColor];
    [btn addTarget:self action:@selector(btnnnn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
- (void)btnnnn{
    TestViewController *testVC = [[TestViewController alloc] init];
    [self presentViewController:testVC animated:NO completion:^{}];
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
————————————————————————————————————————————————————————————————————————————————————————————
第二个视图MyPresentationConytollrt继承UIPresentationController  完成视图改变的代码就在这里面面面
————————————————————————————————————————————————————————————————————————————————————————————
#import <UIKit/UIKit.h>
 
@interface MyPresentationConytollrt : UIPresentationController
@property (nonatomic, strong) UIView *dimmingView;
@property (nonatomic,strong) void(^hidssView)();
@end
————————————————————————————————————————————————————————————————————————————————————————————
 
#import "MyPresentationConytollrt.h"
 
@interface MyPresentationConytollrt ()
 
@end
 
@implementation MyPresentationConytollrt
 
- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController
{
    if ((self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController])) {
        _dimmingView = [[UIView alloc] init];
        _dimmingView.backgroundColor = [UIColor clearColor];
        _dimmingView.alpha = 0;
        UITapGestureRecognizer *hidssTouch = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidssViewTouch)];
        [_dimmingView addGestureRecognizer:hidssTouch];
    }
    return self;
}
 
//点击透明部分视图消失时间
-(void)hidssViewTouch{
    if (self.hidssView) {
        self.hidssView();
    }
}
 
//页面进入的时候调用
- (void)presentationTransitionWillBegin
{
    _dimmingView.frame = self.containerView.bounds;
    [self.containerView addSubview:_dimmingView];
    [self.containerView addSubview:self.presentedView];
     
    id<UIViewControllerTransitionCoordinator> coordinator = self.presentingViewController.transitionCoordinator;
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        _dimmingView.alpha = 1;
    } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
         
    }];
}
 
//页面消失的时候调用
- (void)presentationTransitionDidEnd:(BOOL)completed
{
    if (!completed) {
        [_dimmingView removeFromSuperview];
    }
}
 
- (void)dismissalTransitionWillBegin
{
    id<UIViewControllerTransitionCoordinator> coordinator = self.presentingViewController.transitionCoordinator;
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        _dimmingView.alpha = 0.0;
    } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
         
    }];
}
 
- (void)dismissalTransitionDidEnd:(BOOL)completed
{
    if (completed) {
        [_dimmingView removeFromSuperview];
    }
}
 
//改变页面的frame
- (CGRect)frameOfPresentedViewInContainerView
{
    CGRect frame = self.containerView.bounds;
    frame.origin.x = CGRectGetWidth([UIScreen mainScreen].bounds);
    frame = CGRectInset(frame, 0, 0);
    return frame;
}
 
@end
————————————————————————————————————————————————————————————————————————————————————————————
第三个视图TestViewController 弹出的视图
————————————————————————————————————————————————————————————————————————————————————————————
#import <UIKit/UIKit.h>
 
@interface TestViewController : UIViewController<UIViewControllerTransitioningDelegate>
 
@end
————————————————————————————————————————————————————————————————————————————————————————————
 
#import "TestViewController.h"
#import "MyPresentationConytollrt.h"
 
@interface TestViewController ()
 
@end
 
@implementation TestViewController
- (instancetype)init
{
    if ((self = [super init])) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.transitioningDelegate = self;
    }
    return self;
}
 
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor darkGrayColor];
     
    UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
    close.frame = CGRectMake(0, 0, 100, 30);
    close.center = self.view.center;
    [close setTitle:@"退出" forState:UIControlStateNormal];
    [close addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:close];
}
 
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = CGRectMake(50, 0, CGRectGetWidth([UIScreen mainScreen].bounds) - 50, CGRectGetHeight([UIScreen mainScreen].bounds));
        self.view.frame = rect;
    }];
}
 
- (void)closeAction:(UIButton *)sender
{
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = CGRectMake(CGRectGetWidth([UIScreen mainScreen].bounds), 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
        self.view.frame = rect;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
        }];
    }];
     
}
 
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
    if (presented == self) {
        MyPresentationConytollrt *presen = [[MyPresentationConytollrt alloc] initWithPresentedViewController:presented presentingViewController:presenting];
        [presen setHidssView:^{
            [UIView animateWithDuration:0.2 animations:^{
                CGRect rect = CGRectMake(CGRectGetWidth([UIScreen mainScreen].bounds), 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
                self.view.frame = rect;
            } completion:^(BOOL finished) {
                [self dismissViewControllerAnimated:NO completion:^{
                     
                }];
            }];
        }];
        return presen;
    }
    return nil;
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值