iOS开发实现UIImageView的分类

前端之家收集整理的这篇文章主要介绍了iOS开发实现UIImageView的分类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了iOS实现UIImageView的分类代码,供大家参考,具体内容如下

一.Objective-C版

.h文件

  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>
  3. #import <QuartzCore/QuartzCore.h>
  4.  
  5. /**
  6. * 这个分类为UIImageView添加一些有用的方法
  7. */
  8. @interface UIImageView (WLKit)
  9.  
  10. /**
  11. * 创建一个UIImageView
  12. *
  13. * @param image UIImageView的图片
  14. * @param rect UIImageView的坐标
  15. *
  16. * @return 返回一个UIImageView
  17. */
  18. + (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image
  19. frame:(CGRect)rect;
  20.  
  21. /**
  22. * 创建一个UIImageView
  23. *
  24. * @param image UIImageView的图片
  25. * @param size UIImageView的大小
  26. * @param center UIImageView的中心
  27. *
  28. * @return 返回一个UIImageView
  29. */
  30. + (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image
  31. size:(CGSize)size
  32. center:(CGPoint)center;
  33.  
  34. /**
  35. * 创建一个UIImageView
  36. *
  37. * @param image UIImageView的图片
  38. * @param center UIImageView的中心
  39. *
  40. * @return Returns the created UIImageView
  41. */
  42. + (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image
  43. center:(CGPoint)center;
  44.  
  45. /**
  46. * Create an UIImageView with an image and use it as a template with the given color
  47. *
  48. * @param image UIImageView image
  49. * @param tintColor UIImageView tint color
  50. *
  51. * @return Returns the created UIImageView
  52. */
  53. + (instancetype _Nonnull)imageViewWithImageAsTemplate:(UIImage *_Nonnull)image
  54. tintColor:(UIColor *_Nonnull)tintColor;
  55.  
  56. /**
  57. * Create a drop shadow effect
  58. *
  59. * @param color Shadow's color
  60. * @param radius Shadow's radius
  61. * @param offset Shadow's offset
  62. * @param opacity Shadow's opacity
  63. */
  64. - (void)setImageShadowColor:(UIColor *_Nonnull)color
  65. radius:(CGFloat)radius
  66. offset:(CGSize)offset
  67. opacity:(CGFloat)opacity;
  68.  
  69. /**
  70. * Mask the current UIImageView with an UIImage
  71. *
  72. * @param image The mask UIImage
  73. */
  74. - (void)setMaskImage:(UIImage *_Nonnull)image;
  75.  
  76. @end

.m文件

  1. #import "UIImageView+WLKit.h"
  2.  
  3. @implementation UIImageView (WLKit)
  4.  
  5. + (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image frame:(CGRect)rect
  6. {
  7. UIImageView *_image = [[UIImageView alloc] init];
  8. [_image setFrame:rect];
  9. [_image setImage:image];
  10. return _image;
  11. }
  12.  
  13. + (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image size:(CGSize)size center:(CGPoint)center
  14. {
  15. UIImageView *_image = [[UIImageView alloc] init];
  16. [_image setFrame:CGRectMake(0,size.width,size.height)];
  17. [_image setImage:image];
  18. [_image setCenter:center];
  19. return _image;
  20. }
  21.  
  22. + (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image center:(CGPoint)center
  23. {
  24. UIImageView *_image = [[UIImageView alloc] init];
  25. [_image setFrame:CGRectMake(0,image.size.width,image.size.height)];
  26. [_image setImage:image];
  27. [_image setCenter:center];
  28. return _image;
  29. }
  30.  
  31. + (instancetype _Nonnull)imageViewWithImageAsTemplate:(UIImage *_Nonnull)image tintColor:(UIColor *_Nonnull)tintColor
  32. {
  33. UIImageView *_image = [[UIImageView alloc] init];
  34. image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  35. [_image setImage:image];
  36. [_image setTintColor:tintColor];
  37. return _image;
  38. }
  39.  
  40. - (void)setImageShadowColor:(UIColor *_Nonnull)color radius:(CGFloat)radius offset:(CGSize)offset opacity:(CGFloat)opacity
  41. {
  42. self.layer.shadowColor = color.CGColor;
  43. self.layer.shadowRadius = radius;
  44. self.layer.shadowOffset = offset;
  45. self.layer.shadowOpacity = opacity;
  46. self.clipsToBounds = NO;
  47. }
  48.  
  49. - (void)setMaskImage:(UIImage *_Nonnull)image
  50. {
  51. CALayer *mask = [CALayer layer];
  52. mask.contents = (id)[image CGImage];
  53. mask.frame = CGRectMake(0,self.frame.size.width,self.frame.size.height);
  54. self.layer.mask = mask;
  55. self.layer.masksToBounds = YES;
  56. }
  57.  
  58. - (void)setAlpha:(CGFloat)alpha
  59. {
  60. if ([self.superview isKindOfClass:[UITableView class]]) {
  61. if (self.superview.tag == 836913) {
  62. if (alpha == 0 && self.autoresizingMask == UIViewAutoresizingFlexibleLeftMargin) {
  63. if (self.frame.size.width < 10 && self.frame.size.height > self.frame.size.width) {
  64. UIScrollView *sc = (UIScrollView*)self.superview;
  65. if (sc.frame.size.height < sc.contentSize.height) {
  66. [super setAlpha:0.5];
  67. return;
  68. }
  69. }
  70. }
  71. }
  72.  
  73. if (self.superview.tag == 836914) {
  74. if (alpha == 0 && self.autoresizingMask == UIViewAutoresizingFlexibleTopMargin) {
  75. if (self.frame.size.height < 10 && self.frame.size.height < self.frame.size.width) {
  76. UIScrollView *sc = (UIScrollView*)self.superview;
  77. if (sc.frame.size.width < sc.contentSize.width) {
  78. return;
  79. }
  80. }
  81. }
  82. }
  83. }
  84.  
  85. [super setAlpha:alpha];
  86. }
  87. @end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

猜你在找的iOS相关文章