欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发实现UIImageView的分类

程序员文章站 2022-07-22 22:29:23
本文实例为大家分享了ios实现uiimageview的分类代码,供大家参考,具体内容如下 一.objective-c版 .h文件 #import

本文实例为大家分享了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

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