本文实例为大家分享了iOS实现UIImageView的分类代码,供大家参考,具体内容如下 一.Objective-C版 .h文件 #import <Foundation/F
本文实例为大家分享了iOS实现UIImageView的分类代码,供大家参考,具体内容如下
一.Objective-C版
.h文件
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface UIImageView (WLKit)
+ (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image
frame:(CGRect)rect;
+ (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image
size:(CGSize)size
center:(CGPoint)center;
+ (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image
center:(CGPoint)center;
+ (instancetype _Nonnull)imageViewWithImageAsTemplate:(UIImage *_Nonnull)image
tintColor:(UIColor *_Nonnull)tintColor;
- (void)setImageShadowColor:(UIColor *_Nonnull)color
radius:(CGFloat)radius
offset:(CGSize)offset
opacity:(CGFloat)opacity;
- (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
--结束END--
本文标题: iOS开发实现UIImageView的分类
本文链接: https://lsjlt.com/news/30225.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0