代码如下:
@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 。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.