我用一個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是的,我編輯過。 –