Created
May 11, 2015 11:34
-
-
Save sosoyososo/0555c9ffeb8e7479218a to your computer and use it in GitHub Desktop.
shap view with a image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface UIView(ImageMask) | |
- (void)setMaskImage:(UIImage *)maskImage; | |
@end | |
@implementation UIView(ImageMask) | |
- (void)setMaskImage:(UIImage *)maskImage { | |
objc_setAssociatedObject(self, @selector(setMaskImage:), maskImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
UIImageView *view = [[UIImageView alloc]initWithImage:maskImage]; | |
view.frame = self.bounds; | |
self.layer.mask = view.layer; | |
self.layer.masksToBounds = YES; | |
[[self class] setMethod:@selector(blankSetFrame:) withIMP:@selector(setFrame:)]; | |
[[self class] setMethod:@selector(setFrame:) withIMP:@selector(insertSetFrame:)]; | |
} | |
- (UIImage *)maskImage { | |
return objc_getAssociatedObject(self, @selector(setMaskImage:)); | |
} | |
- (void)updateMask { | |
if ([self maskImage]) { | |
UIImageView *view = [[UIImageView alloc]initWithImage:[self maskImage]]; | |
view.frame = self.bounds; | |
self.layer.mask = view.layer; | |
self.layer.masksToBounds = YES; | |
} | |
} | |
- (void)blankSetFrame:(CGRect)frame { | |
} | |
- (void)insertSetFrame:(CGRect)frame { | |
[self blankSetFrame:frame]; | |
[self updateMask]; | |
} | |
@end | |
@interface NSObject (categoryProperty) | |
+ (void)setMethod:(SEL)selector withIMP:(SEL)other; | |
@end | |
@implementation NSObject (categoryProperty) | |
+ (void)setMethod:(SEL)selector withIMP:(SEL)other { | |
Method md = class_getClassMethod([self class], selector); | |
BOOL isClass = YES; | |
if (!md) { | |
md = class_getInstanceMethod([self class], selector); | |
isClass = NO; | |
} | |
IMP imp = isClass?[[self class] methodForSelector:other]:[[self class] instanceMethodForSelector:other];//判断是不是class方法很重要,否则会失败 | |
if (md && imp) { | |
method_setImplementation(md, imp); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment