iOS开发实现UIImageView的分类

 更新时间:2019年01月23日 15:44:57   作者:夕阳下的守望者  
这篇文章主要为大家详细介绍了iOS开发实现UIImageView的分类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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如何定义名为任意的变量的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-05-05
  • iOS 定制多样式二维码

    iOS 定制多样式二维码

    最常见的二维码功能包括信息获取、网站跳转、电商交易、手机支付等等,其拥有密度小、信息容量大、容错能力强、成本低、制作难度低等优点。在移动开发中,二维码的地位也越来越重要,掌握二维码的基本操作是重要的本领之一。本文将讲解iOS定制二维码的步骤与方法。
    2017-03-03
  • CAMediaTiming ( 时间协议)详解及实例代码

    CAMediaTiming ( 时间协议)详解及实例代码

    这篇文章主要介绍了CAMediaTiming / 时间协议详解及实例代码的相关资料,这里附有实例代码,帮助大家学习参考,需要的朋友可以参考下
    2016-12-12
  • CocoaPods1.9.0 安装使用教程详解

    CocoaPods1.9.0 安装使用教程详解

    CocoaPods是OS X和iOS下的一个第三类库管理工具,这篇文章主要介绍了CocoaPods1.9.0 安装使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • iOS WKWebView无法处理URL Scheme和App Store链接的问题解决

    iOS WKWebView无法处理URL Scheme和App Store链接的问题解决

    这篇文章主要给大家介绍了关于iOS WKWebView无法处理URL Scheme和App Store链接的问题解决的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-03-03
  • iOS开发之使用Storyboard预览UI在不同屏幕上的运行效果

    iOS开发之使用Storyboard预览UI在不同屏幕上的运行效果

    使用Storyboard做开发效率非常高,为了防止在团队中发生冲突,采取的解决办法是负责UI开发的同事最好每人维护一个Storyboard, 公用的组件使用轻量级的xib或者纯代码来实现,下面小编就给大家介绍如何使用Storyboard预览UI在不同屏幕上的运行效果,需要的朋友可以参考下
    2015-08-08
  • ios常见加密解密方法(RSA、DES 、AES、MD5)

    ios常见加密解密方法(RSA、DES 、AES、MD5)

    本篇文章主要介绍了ios常见加密解密方法(RSA、DES 、AES、MD5),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • iOS使用pageViewController实现多视图滑动切换

    iOS使用pageViewController实现多视图滑动切换

    这篇文章主要为大家详细介绍了iOS使用pageViewController实现多视图滑动切换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • iOS客户端本地推送实现代码

    iOS客户端本地推送实现代码

    这篇文章主要介绍了iOS客户端本地推送实现代码,并确定程序中只有一个弹出框,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • iOS开发之级联界面(推荐界面)搭建原理

    iOS开发之级联界面(推荐界面)搭建原理

    这篇文章主要为大家详细介绍了iOS级联界面(推荐界面)搭建原理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08

最新评论