您的位置:首页 > 移动开发 > IOS开发

iOS为分类添加属性

2016-03-10 20:34 471 查看
#import "UIImage+Extend.h"
#import <objc/runtime.h>
@implementation UIImage (Extend)

static char imageX;
static char imageY;
static char directions;
- (void)setX:(CGFloat)x {
// 使用objc_setAssociatedObject函数能够为分类添加属性
objc_setAssociatedObject(self, &imageX, [NSString stringWithFormat:@"%f",x], OBJC_ASSOCIATION_COPY);
}

- (CGFloat)x {
return [objc_getAssociatedObject(self, &imageX) floatValue];
}

- (void)setY:(CGFloat)y {
// 使用objc_setAssociatedObject函数能够为分类添加属性
objc_setAssociatedObject(self, &imageY, [NSString stringWithFormat:@"%f",y], OBJC_ASSOCIATION_COPY);
}

- (CGFloat)y {
return [objc_getAssociatedObject(self, &imageY) floatValue];
}

- (void)setDirection:(CZImageDirection)direction {
// 使用objc_setAssociatedObject函数能够为分类添加属性
objc_setAssociatedObject(self, &directions, [NSString stringWithFormat:@"%d",direction], OBJC_ASSOCIATION_COPY);
}

- (CZImageDirection)direction {
return [objc_getAssociatedObject(self, &directions) boolValue];
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: