我用一个NSString *
类型属性添加到UIButton
,但是今天,我想一个BOOL
type属性isScrolling
添加到UIScrollView
指示滚动视图是否以同样的方式滚动但表现有些不对劲,这里是我的代码:我可以使用类别和运行时将一个BOOL类型属性“isScrolling”添加到UIScrollView吗?
#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
有什么办法对付thses错误,我们可以使用类别和运行时,实现添加BOOL
属性UIScrollView
的目标指示whethe滚动视图滚动?
希望有人能给我一些建议,非常感谢。
后的代码,因为它是。请不要截图。 –
objc_getAssociatedObject用于对象类型。 BOOL不是对象类型。这就是为什么你得到了错误 –
@Lkrishna是的,我编辑过。 –