///-----------------------------------------------------------------------------
/// Associated Objects
/// http://nshipster.com/associated-objects
///-----------------------------------------------------------------------------
/**
Associated Objects—or Associative References, as they were originally known—are a feature of the Objective-C 2.0 runtime, introduced in OS X Snow Leopard (available in iOS 4). The term refers to the following three C functions declared in <objc/runtime.h>, which allow objects to associate arbitrary values for keys at runtime:
objc_setAssociatedObject
objc_getAssociatedObject
objc_removeAssociatedObjects
Why is this useful? It allows developers to add custom properties to existing classes in categories, which is an otherwise notable shortcoming for Objective-C.
http://nshipster.com/associated-objects
*/
@interface NSObject (AssociatedObject)
@property (nonatomic, strong) id associatedObject;
@end
@implementation NSObject (AssociatedObject)
@dynamic associatedObject;
/**
It is often recommended that they key be a static char—or better yet, the
pointer to one. Basically, an arbitrary value that is guaranteed to be constant,
unique, and scoped for use within getters and setters:
static char kAssociatedObjectKey;
objc_getAssociatedObject(self, &kAssociatedObjectKey);
http://nshipster.com/associated-objects
*/
- (void)setAssociatedObject:(id)object {
objc_setAssociatedObject(self, @selector(associatedObject), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)associatedObject {
return objc_getAssociatedObject(self, @selector(associatedObject));
}
@end
///-----------------------------------------------------------------------------
/// https://github.com/ltebean/LTNavigationBar
///-----------------------------------------------------------------------------
@implementation UINavigationBar (Awesome)
static char overlayKey;
- (UIView *)overlay
{
return objc_getAssociatedObject(self, &overlayKey);
}
- (void)setOverlay:(UIView *)overlay
{
objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)lt_setBackgroundColor:(UIColor *)backgroundColor
{
if (!self.overlay) {
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + 20)];
self.overlay.userInteractionEnabled = NO;
self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth; // Should not set `UIViewAutoresizingFlexibleHeight`
[[self.subviews firstObject] insertSubview:self.overlay atIndex:0];
}
self.overlay.backgroundColor = backgroundColor;
}
@end
///-----------------------------------------------------------------------------
/// https://github.com/forkingdog/FDFullscreenPopGesture
///-----------------------------------------------------------------------------
@interface UINavigationController (FDFullscreenPopGesture)
@property (nonatomic, strong, readonly) UIPanGestureRecognizer *fd_fullscreenPopGestureRecognizer;
@property (nonatomic, assign) BOOL fd_viewControllerBasedNavigationBarAppearanceEnabled;
@end
@implementation UINavigationController (FDFullscreenPopGesture)
- (UIPanGestureRecognizer *)fd_fullscreenPopGestureRecognizer
{
UIPanGestureRecognizer *panGestureRecognizer = objc_getAssociatedObject(self, _cmd);
if (!panGestureRecognizer) {
panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
panGestureRecognizer.maximumNumberOfTouches = 1;
objc_setAssociatedObject(self, _cmd, panGestureRecognizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return panGestureRecognizer;
}
- (BOOL)fd_viewControllerBasedNavigationBarAppearanceEnabled
{
NSNumber *number = objc_getAssociatedObject(self, _cmd);
if (number) {
return number.boolValue;
}
self.fd_viewControllerBasedNavigationBarAppearanceEnabled = YES;
return YES;
}
- (void)setFd_viewControllerBasedNavigationBarAppearanceEnabled:(BOOL)enabled
{
SEL key = @selector(fd_viewControllerBasedNavigationBarAppearanceEnabled);
objc_setAssociatedObject(self, key, @(enabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
///-----------------------------------------------------------------------------
/// https://github.com/forkingdog/FDFullscreenPopGesture
///-----------------------------------------------------------------------------
@interface UIViewController (FDFullscreenPopGesture)
@property (nonatomic, assign) BOOL fd_interactivePopDisabled;
@property (nonatomic, assign) BOOL fd_prefersNavigationBarHidden;
@property (nonatomic, assign) CGFloat fd_interactivePopMaxAllowedInitialDistanceToLeftEdge;
@end
@implementation UIViewController (FDFullscreenPopGesture)
- (BOOL)fd_interactivePopDisabled
{
return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (void)setFd_interactivePopDisabled:(BOOL)disabled
{
objc_setAssociatedObject(self, @selector(fd_interactivePopDisabled), @(disabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)fd_prefersNavigationBarHidden
{
return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (void)setFd_prefersNavigationBarHidden:(BOOL)hidden
{
objc_setAssociatedObject(self, @selector(fd_prefersNavigationBarHidden), @(hidden), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGFloat)fd_interactivePopMaxAllowedInitialDistanceToLeftEdge
{
#if CGFLOAT_IS_DOUBLE
return [objc_getAssociatedObject(self, _cmd) doubleValue];
#else
return [objc_getAssociatedObject(self, _cmd) floatValue];
#endif
}
- (void)setFd_interactivePopMaxAllowedInitialDistanceToLeftEdge:(CGFloat)distance
{
SEL key = @selector(fd_interactivePopMaxAllowedInitialDistanceToLeftEdge);
objc_setAssociatedObject(self, key, @(MAX(0, distance)), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
///-----------------------------------------------------------------------------
/// http://www.cnblogs.com/tangbinblog/p/3944316.html
/// category使用 objc_setAssociatedObject/objc_getAssociatedObject 實(shí)現(xiàn)添加屬性
///-----------------------------------------------------------------------------
@interface NSObject (CategoryWithProperty)
@property (nonatomic, strong) NSObject *property;
@end
@implementation NSObject (CategoryWithProperty)
- (NSObject *)property {
return objc_getAssociatedObject(self, @selector(property));
}
- (void)setProperty:(NSObject *)value {
objc_setAssociatedObject(self, @selector(property), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
Runtime-Code-AssociatedObject
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來粱栖,“玉大人话浇,你說我怎么就攤上這事脏毯∧志浚” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵食店,是天一觀的道長渣淤。 經(jīng)常有香客問我,道長吉嫩,這世上最難降的妖魔是什么价认? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮自娩,結(jié)果婚禮上用踩,老公的妹妹穿的比我還像新娘。我一直安慰自己忙迁,他們只是感情好脐彩,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著姊扔,像睡著了一般惠奸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上恰梢,一...
- 文/蒼蘭香墨 我猛地睜開眼拥坛,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了尘分?” 一聲冷哼從身側(cè)響起猜惋,我...
- 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎培愁,沒想到半個月后著摔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡定续,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年谍咆,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片私股。...
- 正文 年R本政府宣布峭状,位于F島的核電站克滴,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏优床。R本人自食惡果不足惜劝赔,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望胆敞。 院中可真熱鬧着帽,春花似錦、人聲如沸移层。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽幽钢。三九已至歉备,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間匪燕,已是汗流浹背蕾羊。 一陣腳步聲響...
- 正文 我出身青樓书闸,卻偏偏與公主長得像,于是被迫代替她去往敵國和親利凑。 傳聞我的和親對象是個殘疾皇子浆劲,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 如果你實(shí)現(xiàn)過自定義模型數(shù)據(jù)持久化的過程,那么你也肯定明白哀澈,如果一個模型有許多個屬性牌借,那么我們需要對每個屬性都實(shí)現(xiàn)一...
- 一.認(rèn)識一下runtime類 二.The Runtime 1.Objective-C:是一門簡單的語言适荣,95%是C...