FullscreenImageViewController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "FullscreenImageViewController.h"
  21. #define bgColor [UIColor colorWithRed:0.93 green:0.93 blue:0.96 alpha:1.0]
  22. @interface FullscreenImageViewController () <UINavigationControllerDelegate>
  23. @property BOOL controlsHidden;
  24. @property UIImageView *imageView;
  25. @property UIImage *image;
  26. @property CGSize imageSize;
  27. @end
  28. @implementation FullscreenImageViewController
  29. + (instancetype)controllerForImage:(UIImage *)image {
  30. FullscreenImageViewController *controller = [FullscreenImageViewController new];
  31. controller.image = image;
  32. controller.imageSize = image.size;
  33. return controller;
  34. }
  35. - (UIImageView *)createImageView {
  36. UIImageView *imageView = [[UIImageView alloc] init];
  37. imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  38. imageView.userInteractionEnabled = YES;
  39. imageView.frame = [self imageRect];
  40. imageView.image = _image;
  41. return imageView;
  42. }
  43. - (void)viewDidLoad {
  44. self.view.backgroundColor = bgColor;
  45. _imageView = [self createImageView];
  46. [self.view addSubview:_imageView];
  47. UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toogleControls)];
  48. [self.view addGestureRecognizer:tapRecognizer];
  49. self.view.userInteractionEnabled = YES;
  50. [self.view setBackgroundColor:[Colors backgroundDark]];
  51. if (@available(iOS 11.0, *)) {
  52. _imageView.accessibilityIgnoresInvertColors = true;
  53. }
  54. }
  55. - (void)viewWillAppear:(BOOL)animated {
  56. _imageView.image = _image;
  57. }
  58. - (void)viewWillDisappear:(BOOL)animated {
  59. [super viewWillDisappear:animated];
  60. UINavigationController *nav = self.navigationController;
  61. [self fadeInControls:nav];
  62. }
  63. -(void)viewDidLayoutSubviews {
  64. _imageView.frame = [self imageRect];
  65. }
  66. - (CGRect)imageRect {
  67. CGSize boundsSize = self.view.bounds.size;
  68. CGFloat zoomScale = [self getZoomScale];
  69. CGFloat w = zoomScale * _imageSize.width;
  70. CGFloat h = zoomScale * _imageSize.height;
  71. CGFloat dx = (boundsSize.width - w) / 2.0;
  72. CGFloat dy = (boundsSize.height - h) / 2.0;
  73. return CGRectMake(dx, dy, w, h);
  74. }
  75. - (CGFloat)getZoomScale
  76. {
  77. CGSize boundsSize = self.view.bounds.size;
  78. CGFloat xScale = boundsSize.width / _imageSize.width;
  79. CGFloat yScale = boundsSize.height / _imageSize.height;
  80. if (boundsSize.width > boundsSize.height) {
  81. return yScale;
  82. }
  83. return xScale;
  84. }
  85. #pragma mark - Toogle controls
  86. - (void)toogleControls
  87. {
  88. UINavigationController *nav = self.navigationController;
  89. if (_controlsHidden) {
  90. [self fadeInControls:nav];
  91. } else {
  92. [self fadeAwayControls:nav];
  93. }
  94. }
  95. - (void)fadeInControls:(UINavigationController *)nav
  96. {
  97. _controlsHidden = NO;
  98. [nav.navigationBar setAlpha:0.0f];
  99. [UIView animateWithDuration:0.2 animations:^{
  100. [nav.navigationBar setAlpha:1.0f];
  101. self.view.backgroundColor = [Colors backgroundDark];
  102. [self setNeedsStatusBarAppearanceUpdate];
  103. if (self.tabBarController) {
  104. [self.tabBarController.tabBar setAlpha:1.0f];
  105. }
  106. }];
  107. }
  108. - (void)fadeAwayControls:(UINavigationController *)nav
  109. {
  110. _controlsHidden = YES;
  111. [self.navigationController.interactivePopGestureRecognizer setDelegate:nil];
  112. [UIView animateWithDuration:0.2 animations:^{
  113. [nav.navigationBar setAlpha:0.0f];
  114. [nav.toolbar setAlpha:0.0f];
  115. [self setNeedsStatusBarAppearanceUpdate];
  116. self.view.backgroundColor = [UIColor blackColor];
  117. if (self.tabBarController) {
  118. [self.tabBarController.tabBar setAlpha:0.0f];
  119. }
  120. }];
  121. }
  122. @end