iOS开发实现UIImageView的分类
本文实例为大家分享了iOS实现UIImageView的分类代码,供大家参考,具体内容如下
一.Objective-C版
.h文件
#import#import #import /** *这个分类为UIImageView添加一些有用的方法 */ @interfaceUIImageView(WLKit) /** *创建一个UIImageView * *@paramimageUIImageView的图片 *@paramrectUIImageView的坐标 * *@return返回一个UIImageView */ +(instancetype_Nonnull)imageViewWithImage:(UIImage*_Nonnull)image frame:(CGRect)rect; /** *创建一个UIImageView * *@paramimageUIImageView的图片 *@paramsizeUIImageView的大小 *@paramcenterUIImageView的中心 * *@return返回一个UIImageView */ +(instancetype_Nonnull)imageViewWithImage:(UIImage*_Nonnull)image size:(CGSize)size center:(CGPoint)center; /** *创建一个UIImageView * *@paramimageUIImageView的图片 *@paramcenterUIImageView的中心 * *@returnReturnsthecreatedUIImageView */ +(instancetype_Nonnull)imageViewWithImage:(UIImage*_Nonnull)image center:(CGPoint)center; /** *CreateanUIImageViewwithanimageanduseitasatemplatewiththegivencolor * *@paramimageUIImageViewimage *@paramtintColorUIImageViewtintcolor * *@returnReturnsthecreatedUIImageView */ +(instancetype_Nonnull)imageViewWithImageAsTemplate:(UIImage*_Nonnull)image tintColor:(UIColor*_Nonnull)tintColor; /** *Createadropshadoweffect * *@paramcolorShadow'scolor *@paramradiusShadow'sradius *@paramoffsetShadow'soffset *@paramopacityShadow'sopacity */ -(void)setImageShadowColor:(UIColor*_Nonnull)color radius:(CGFloat)radius offset:(CGSize)offset opacity:(CGFloat)opacity; /** *MaskthecurrentUIImageViewwithanUIImage * *@paramimageThemaskUIImage */ -(void)setMaskImage:(UIImage*_Nonnull)image; @end
.m文件
#import"UIImageView+WLKit.h" @implementationUIImageView(WLKit) +(instancetype_Nonnull)imageViewWithImage:(UIImage*_Nonnull)imageframe:(CGRect)rect { UIImageView*_image=[[UIImageViewalloc]init]; [_imagesetFrame:rect]; [_imagesetImage:image]; return_image; } +(instancetype_Nonnull)imageViewWithImage:(UIImage*_Nonnull)imagesize:(CGSize)sizecenter:(CGPoint)center { UIImageView*_image=[[UIImageViewalloc]init]; [_imagesetFrame:CGRectMake(0,0,size.width,size.height)]; [_imagesetImage:image]; [_imagesetCenter:center]; return_image; } +(instancetype_Nonnull)imageViewWithImage:(UIImage*_Nonnull)imagecenter:(CGPoint)center { UIImageView*_image=[[UIImageViewalloc]init]; [_imagesetFrame:CGRectMake(0,0,image.size.width,image.size.height)]; [_imagesetImage:image]; [_imagesetCenter:center]; return_image; } +(instancetype_Nonnull)imageViewWithImageAsTemplate:(UIImage*_Nonnull)imagetintColor:(UIColor*_Nonnull)tintColor { UIImageView*_image=[[UIImageViewalloc]init]; image=[imageimageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [_imagesetImage:image]; [_imagesetTintColor:tintColor]; return_image; } -(void)setImageShadowColor:(UIColor*_Nonnull)colorradius:(CGFloat)radiusoffset:(CGSize)offsetopacity:(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=[CALayerlayer]; mask.contents=(id)[imageCGImage]; 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.superviewisKindOfClass:[UITableViewclass]]){ 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以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。