react-native-navigation的迁移库

VICMAImageView.m 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // VIAnimatableImageView.m
  3. // VIAnimatableImageViewDemo
  4. //
  5. // Created by Vito on 12/30/14.
  6. // Copyright (c) 2014 Vito. All rights reserved.
  7. // project can be found at: https://github.com/vitoziv/VICMAImageView
  8. #import "VICMAImageView.h"
  9. @interface VICMAImageView ()
  10. @property (nonatomic, strong) UIImageView *imageView;
  11. @end
  12. @implementation VICMAImageView
  13. - (void)commonInit
  14. {
  15. self.clipsToBounds = YES;
  16. self.contentMode = UIViewContentModeScaleAspectFit;
  17. if (!_imageView) {
  18. UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
  19. imageView.contentMode = UIViewContentModeScaleToFill;
  20. [self addSubview:imageView];
  21. _imageView = imageView;
  22. }
  23. }
  24. - (instancetype)initWithImage:(UIImage *)image
  25. {
  26. self = [super init];
  27. if (self) {
  28. [self commonInit];
  29. _imageView.image = image;
  30. }
  31. return self;
  32. }
  33. - (instancetype)initWithFrame:(CGRect)frame
  34. {
  35. self = [super initWithFrame:frame];
  36. if (self) {
  37. [self commonInit];
  38. }
  39. return self;
  40. }
  41. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  42. {
  43. self = [super initWithCoder:aDecoder];
  44. if (self) {
  45. [self commonInit];
  46. }
  47. return self;
  48. }
  49. - (CGSize)presentationImageSize
  50. {
  51. return self.imageView.bounds.size;
  52. }
  53. - (void)setImage:(UIImage *)image
  54. {
  55. self.imageView.image = image;
  56. [self updateView];
  57. }
  58. - (UIImage *)image
  59. {
  60. return self.imageView.image;
  61. }
  62. - (void)setFrame:(CGRect)frame
  63. {
  64. [super setFrame:frame];
  65. [self updateView];
  66. }
  67. - (void)setContentMode:(UIViewContentMode)contentMode
  68. {
  69. [super setContentMode:contentMode];
  70. [self updateView];
  71. }
  72. - (void)updateView
  73. {
  74. if (self.bounds.size.width == 0 || self.bounds.size.height == 0 ||
  75. self.image.size.width == 0 || self.image.size.height == 0) {
  76. return;
  77. }
  78. switch (self.contentMode) {
  79. case UIViewContentModeScaleAspectFit:
  80. [self updateViewToAspectFit];
  81. break;
  82. case UIViewContentModeScaleAspectFill:
  83. [self updateViewToAspectFill];
  84. break;
  85. case UIViewContentModeScaleToFill:
  86. [self updateViewToScaleToFill];
  87. break;
  88. case UIViewContentModeCenter:
  89. [self updateViewToCenter];
  90. break;
  91. case UIViewContentModeBottom:
  92. [self updateViewToBottom];
  93. break;
  94. case UIViewContentModeBottomLeft:
  95. [self updateViewToBottomLeft];
  96. break;
  97. case UIViewContentModeBottomRight:
  98. [self updateViewToBottomRight];
  99. break;
  100. case UIViewContentModeLeft:
  101. [self updateViewToLeft];
  102. break;
  103. case UIViewContentModeRight:
  104. [self updateViewToRight];
  105. break;
  106. case UIViewContentModeTop:
  107. [self updateViewToTop];
  108. break;
  109. case UIViewContentModeTopLeft:
  110. [self updateViewToTopLeft];
  111. break;
  112. case UIViewContentModeTopRight:
  113. [self updateViewToTopRight];
  114. break;
  115. case UIViewContentModeRedraw:
  116. [self updateViewToScaleToFill];
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. - (void)updateViewToAspectFit
  123. {
  124. CGSize imageSize = CGSizeMake(self.imageView.image.size.width / self.imageView.image.scale,
  125. self.imageView.image.size.height / self.imageView.image.scale);
  126. CGFloat widthRatio = imageSize.width / self.bounds.size.width;
  127. CGFloat heightRatio = imageSize.height / self.bounds.size.height;
  128. if (widthRatio > heightRatio) {
  129. imageSize = CGSizeMake(imageSize.width / widthRatio, imageSize.height / widthRatio);
  130. } else {
  131. imageSize = CGSizeMake(imageSize.width / heightRatio, imageSize.height / heightRatio);
  132. }
  133. self.imageView.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height);
  134. self.imageView.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
  135. }
  136. - (void)updateViewToAspectFill
  137. {
  138. CGSize imageSize = CGSizeMake(self.imageView.image.size.width / self.imageView.image.scale,
  139. self.imageView.image.size.height / self.imageView.image.scale);
  140. CGFloat widthRatio = imageSize.width / self.bounds.size.width;
  141. CGFloat heightRatio = imageSize.height / self.bounds.size.height;
  142. if (widthRatio > heightRatio) {
  143. imageSize = CGSizeMake(imageSize.width / heightRatio, imageSize.height / heightRatio);
  144. } else {
  145. imageSize = CGSizeMake(imageSize.width / widthRatio, imageSize.height / widthRatio);
  146. }
  147. self.imageView.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height);
  148. [self centerImageViewToSuperView];
  149. }
  150. - (void)updateViewToScaleToFill
  151. {
  152. self.imageView.bounds = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
  153. [self centerImageViewToSuperView];
  154. }
  155. - (void)updateViewToCenter
  156. {
  157. [self fitImageViewSizeToImageSize];
  158. [self centerImageViewToSuperView];
  159. }
  160. - (void)updateViewToBottom
  161. {
  162. [self fitImageViewSizeToImageSize];
  163. self.imageView.center = CGPointMake(self.bounds.size.width / 2,
  164. self.bounds.size.height - self.image.size.height / 2);
  165. }
  166. - (void)updateViewToBottomLeft
  167. {
  168. [self fitImageViewSizeToImageSize];
  169. self.imageView.center = CGPointMake(self.image.size.width / 2,
  170. self.bounds.size.height - self.image.size.height / 2);
  171. }
  172. - (void)updateViewToBottomRight
  173. {
  174. [self fitImageViewSizeToImageSize];
  175. self.imageView.center = CGPointMake(self.bounds.size.width - self.image.size.width / 2,
  176. self.bounds.size.height - self.image.size.height / 2);
  177. }
  178. - (void)updateViewToLeft
  179. {
  180. [self fitImageViewSizeToImageSize];
  181. self.imageView.center = CGPointMake(self.image.size.width / 2,
  182. self.bounds.size.height / 2);
  183. }
  184. - (void)updateViewToRight
  185. {
  186. [self fitImageViewSizeToImageSize];
  187. self.imageView.center = CGPointMake(self.bounds.size.width - self.image.size.width / 2,
  188. self.bounds.size.height / 2);
  189. }
  190. - (void)updateViewToTop
  191. {
  192. [self fitImageViewSizeToImageSize];
  193. self.imageView.center = CGPointMake(self.bounds.size.width / 2,
  194. self.image.size.height / 2);
  195. }
  196. - (void)updateViewToTopLeft
  197. {
  198. [self fitImageViewSizeToImageSize];
  199. self.imageView.center = CGPointMake(self.image.size.width / 2,
  200. self.image.size.height / 2);
  201. }
  202. - (void)updateViewToTopRight
  203. {
  204. [self fitImageViewSizeToImageSize];
  205. self.imageView.center = CGPointMake(self.bounds.size.width - self.image.size.width / 2,
  206. self.image.size.height / 2);
  207. }
  208. #pragma mark - Helper
  209. - (void)fitImageViewSizeToImageSize
  210. {
  211. CGSize imageSize = CGSizeMake(self.imageView.image.size.width / self.imageView.image.scale,
  212. self.imageView.image.size.height / self.imageView.image.scale);
  213. self.imageView.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height);
  214. }
  215. - (void)centerImageViewToSuperView
  216. {
  217. self.imageView.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
  218. }
  219. @end