ios – 模式转换样式,如邮件应用程序

前端之家收集整理的这篇文章主要介绍了ios – 模式转换样式,如邮件应用程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试实现模态呈现效果,其中所呈现的视图仅部分覆盖父视图,如下图所示.

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

在API中是否有这种转型的支持

我研究了所有可用的Modal Presentation Styles,在我看来,我不想支持我想要的转换,唯一的方法就是编写它.

解决方法

我遇到了同样的问题.我也去了模式演示风格的路线,并不断击中墙壁(具体让它在iPhone而不是iPad上工作).

经过一些挖掘,我能够得到它的工作.这是我如何做到的:

要开始,我们需要一个视图控制器,我们将呈现(模态的)将其视图的背景颜色设置为透明,并将导航控制器视图的框架设置为一些偏移.

ModalViewController.h

  1. @import UIKit;
  2.  
  3. @class ModalViewController;
  4.  
  5. @protocol ModalViewControllerDelegate <NSObject>
  6.  
  7. - (void)modalViewControllerDidCancel:(ModalViewController *)modalViewController;
  8.  
  9. @end
  10.  
  11. @interface ModalViewController : UIViewController
  12. @property (weak,nonatomic) id<ModalViewControllerDelegate> delegate;
  13.  
  14. - (instancetype)initWithRootViewController:(UIViewController *)rootViewController;
  15. @end

ModalViewController.m

  1. static const CGFloat kTopOffset = 50.0f;
  2.  
  3. @implementation ModalViewController {
  4. UINavigationController *_navController;
  5. }
  6.  
  7. - (instancetype)initWithRootViewController:(UIViewController *)rootViewController
  8. {
  9. self = [super initWithNibName:nil bundle:nil];
  10. if (self) {
  11. rootViewController.navigationItem.leftBarButtonItem = [self cancelButton];
  12. _navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
  13. self.view.backgroundColor = [UIColor clearColor];
  14. [self.view addSubview:_navController.view];
  15.  
  16. // this is important (prevents black overlay)
  17. self.modalPresentationStyle = UIModalPresentationOverFullScreen;
  18. }
  19.  
  20. return self;
  21. }
  22.  
  23. - (void)viewDidLoad
  24. {
  25. [super viewDidLoad];
  26. CGRect bounds = self.view.bounds;
  27. _navController.view.frame = CGRectMake(0,kTopOffset,CGRectGetWidth(bounds),CGRectGetHeight(bounds) - kTopOffset);
  28. }
  29.  
  30. - (UIBarButtonItem *)cancelButton
  31. {
  32. return [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonClicked:)];
  33. }
  34.  
  35. - (void)cancelButtonClicked:(id)sender
  36. {
  37. [_delegate modalViewControllerDidCancel:self];
  38. }
  39.  
  40. @end

接下来,我们需要设置演示控制器来运行以下动画:

>缩小自己
淡出一点点
>使用presentViewController呈现模态视图控制器:动画:完成

这就是我所做的

PresentingViewController.m

  1. static const CGFloat kTransitionScale = 0.9f;
  2. static const CGFloat kTransitionAlpha = 0.6f;
  3. static const NSTimeInterval kTransitionDuration = 0.5;
  4.  
  5. @interface PresentingViewController <ModalViewControllerDelegate>
  6. @end
  7.  
  8. @implementation PresentingViewController
  9. ...
  10. ...
  11.  
  12. - (void)showModalViewController
  13. {
  14. self.navigationController.view.layer.shouldRasterize = YES;
  15. self.navigationController.view.layer.rasterizationScale = [UIScreen mainScreen].scale;
  16.  
  17. UIViewController *controller = // init some view controller
  18. ModalViewController *container = [[ModalViewController alloc] initWithRootViewController:controller];
  19. container.delegate = self;
  20.  
  21. __weak UIViewController *weakSelf = self;
  22. [UIView animateWithDuration:kTransitionDuration animations:^{
  23. weakSelf.navigationController.view.transform = CGAffineTransformMakeScale(kTransitionScale,kTransitionScale);
  24. weakSelf.navigationController.view.alpha = kTransitionAlpha;
  25. [weakSelf presentViewController:container animated:YES completion:nil];
  26. } completion:^(BOOL finished) {
  27. weakSelf.navigationController.view.layer.shouldRasterize = NO;
  28. }];
  29. }
  30.  
  31. #pragma mark - ModalViewControllerDelegate
  32.  
  33. - (void)modalViewControllerDidCancel:(ModalViewController *)modalViewController
  34. {
  35. __weak UIViewController *weakSelf = self;
  36. [UIView animateWithDuration:kTransitionDuration animations:^{
  37. weakSelf.navigationController.view.alpha = 1;
  38. weakSelf.navigationController.view.transform = CGAffineTransformIdentity;
  39. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  40. }];
  41. }
  42. @end

猜你在找的iOS相关文章