gpt4 book ai didi

ios - 像邮件应用程序中的模态过渡样式

转载 作者:技术小花猫 更新时间:2023-10-29 10:17:47 28 4
gpt4 key购买 nike

我正在尝试实现模态呈现效果,其中呈现的 View 仅部分覆盖父 View ,如下图所示。

enter image description here

我知道我可以通过使用 UIPresentationController 实现自定义转换来实现这一点。我不想重新发明轮子,所以在我继续开发之前我想问一下。

是否有支持 API 中这种转换的构建?

我研究了所有可用的 Modal Presentation Styles在我看来,我想要进行的转换没有任何支持,实现它的唯一方法就是编写代码。

最佳答案

我遇到了完全相同的问题。我也沿着模态演示风格路线走,但一直碰壁(特别是让它在 iPhone 而不是 iPad 上运行)。

经过一番挖掘之后,我终于能够让它正常工作了。这是我的做法:

首先,我们需要一个将要呈现的 View Controller (模态视图 Controller )将其 View 的背景颜色设置为透明,并将导航 Controller View 的框架设置为某个偏移量。

ModalViewController.h

@import UIKit;

@class ModalViewController;

@protocol ModalViewControllerDelegate <NSObject>

- (void)modalViewControllerDidCancel:(ModalViewController *)modalViewController;

@end

@interface ModalViewController : UIViewController
@property (weak, nonatomic) id<ModalViewControllerDelegate> delegate;

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController;
@end

ModalViewController.m

static const CGFloat kTopOffset = 50.0f;

@implementation ModalViewController {
UINavigationController *_navController;
}

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
rootViewController.navigationItem.leftBarButtonItem = [self cancelButton];
_navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:_navController.view];

// this is important (prevents black overlay)
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
}

return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
CGRect bounds = self.view.bounds;
_navController.view.frame = CGRectMake(0, kTopOffset, CGRectGetWidth(bounds), CGRectGetHeight(bounds) - kTopOffset);
}

- (UIBarButtonItem *)cancelButton
{
return [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonClicked:)];
}

- (void)cancelButtonClicked:(id)sender
{
[_delegate modalViewControllerDidCancel:self];
}

@end

接下来,我们需要设置呈现 Controller 来运行以下动画:

  • 自行缩减
  • 稍微淡出一点
  • 使用 presentViewController:animated:completion 呈现模态视图 Controller

这是我做的

PresentingViewController.m

static const CGFloat kTransitionScale = 0.9f;
static const CGFloat kTransitionAlpha = 0.6f;
static const NSTimeInterval kTransitionDuration = 0.5;

@interface PresentingViewController <ModalViewControllerDelegate>
@end

@implementation PresentingViewController
...
...

- (void)showModalViewController
{
self.navigationController.view.layer.shouldRasterize = YES;
self.navigationController.view.layer.rasterizationScale = [UIScreen mainScreen].scale;

UIViewController *controller = // init some view controller
ModalViewController *container = [[ModalViewController alloc] initWithRootViewController:controller];
container.delegate = self;

__weak UIViewController *weakSelf = self;
[UIView animateWithDuration:kTransitionDuration animations:^{
weakSelf.navigationController.view.transform = CGAffineTransformMakeScale(kTransitionScale, kTransitionScale);
weakSelf.navigationController.view.alpha = kTransitionAlpha;
[weakSelf presentViewController:container animated:YES completion:nil];
} completion:^(BOOL finished) {
weakSelf.navigationController.view.layer.shouldRasterize = NO;
}];
}

#pragma mark - ModalViewControllerDelegate

- (void)modalViewControllerDidCancel:(ModalViewController *)modalViewController
{
__weak UIViewController *weakSelf = self;
[UIView animateWithDuration:kTransitionDuration animations:^{
weakSelf.navigationController.view.alpha = 1;
weakSelf.navigationController.view.transform = CGAffineTransformIdentity;
[weakSelf dismissViewControllerAnimated:YES completion:nil];
}];
}
@end

关于ios - 像邮件应用程序中的模态过渡样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26994655/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com