欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS开发实现UIImageView的分类

时间:2021-06-09 08:05:29|栏目:iOS代码|点击:

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

一.Objective-C版

.h文件

#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文件

#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

上一篇:最新ios面试试题以及解决思路分析

栏    目:iOS代码

下一篇:ios app重提提交审核流程

本文标题:iOS开发实现UIImageView的分类

本文地址:http://www.codeinn.net/misctech/138145.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有