代码如下:
@interface UIView (Rotate )
@property (nonatomic, strong ) NSString * animating;
- (void ) startSpin;
- (void ) stopSpin;
@end
//--------------------------------------------
#import "UIView+Rotate.h"
#import <objc/runtime.h>
#define ANIMATING_TAG @"1"
#define ANIMATING_STOP_TAG @"0"
@implementation UIView (Rotate )
static void * mAnimating = &mAnimating;
-(NSString *)animating{
return objc_getAssociatedObject (self,mAnimating );
}
-(void )setAnimating:(NSString *)aAnimating {
objc_setAssociatedObject (self, mAnimating, aAnimating, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
}
- (void ) spinWithOptions: (UIViewAnimationOptions ) options{
[UIView animateWithDuration: 0.5f
delay: 0.0f
options: options
animations: ^{
self.transform = CGAffineTransformRotate (self.transform, M_PI / 2 );
}
completion: ^(BOOL finished ) {
if (finished ) {
if ([self checkIsAnimating]) {
// if flag still set, keep spinning with constant speed
[self spinWithOptions: UIViewAnimationOptionCurveLinear];
} else if (options != UIViewAnimationOptionCurveEaseOut ) {
// one last spin, with deceleration
[self spinWithOptions: UIViewAnimationOptionCurveEaseOut];
}
}
}];
}
- (void ) startSpin{
if (![self checkIsAnimating]) {
[self setAnimating:ANIMATING_TAG];
[self spinWithOptions: UIViewAnimationOptionCurveEaseIn];
}
}
- (void ) stopSpin {
[self setAnimating:ANIMATING_STOP_TAG];
}
- (BOOL )checkIsAnimating{
if (!self.animating ) {
return NO;
}
if ([self.animating isEqualToString:ANIMATING_TAG]) {
return YES;
}else{
return NO;
}
}
@end
想给 UIView 增加一个 animating 的 String , 在增加的方法里面调用。 但是不管用 self.animating 或者 [self setAnimating:ANIMATING_STOP_TAG];
animating 的 get 方法没事, 但是 set 方法一直没有走.导致 animating 都一直为 nil 。
1
cxq OP 贴的缩进太乱 大家将就看吧
|
2
cxq OP 搞定了 因为属性名原因 换成 animating 换成 rotateAnimating 就可以了。可能重名。
|