服务器之家

服务器之家 > 正文

iOS开发实现UIImageView的分类

时间:2021-05-20 16:41     来源/作者:夕阳下的守望者

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

一.Objective-C版

.h文件

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

.m文件

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

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

原文链接:https://blog.csdn.net/wgl_happy/article/details/77868919

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部