【发布时间】:2017-06-20 04:32:48
【问题描述】:
我以前在UIButton 中添加了一个NSString *type 属性,但是今天我想在UIScrollView 中添加一个BOOL 类型属性isScrolling 来指示scrollView 是否以相同的方式滚动,但是显示有问题,这是我的代码:
#import <UIKit/UIKit.h>
@interface UIScrollView (Util)
@property (nonatomic,assign) BOOL isScrolling;
@end
#import <objc/objc-runtime.h>
@interface UIScrollView ()<UIScrollViewDelegate>
@end
@implementation UIScrollView (Util)
static void *strKey = &strKey;
- (void)setIsScrolling:(BOOL)isScrolling{
objc_setAssociatedObject(self, & strKey, isScrolling, OBJC_ASSOCIATION_ASSIGN);
}
- (BOOL)isScrolling{
return objc_getAssociatedObject(self, &strKey);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
self.isScrolling = YES;
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
self.isScrolling = NO;
}
@end
有没有办法处理这些错误,我们可以使用类别和运行时来实现在UIScrollView 中添加BOOL 属性以指示scrollView 是否正在滚动的目标?
希望有人能给我一些建议,非常感谢。
【问题讨论】:
-
邮政编码原样。请不要截图。
-
objc_getAssociatedObject 用于对象类型。 BOOL 不是对象类型。这就是你得到错误的原因
-
@LalKrishna 是的,我已经编辑过了。
标签: ios objective-c uiscrollview runtime categories