V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
cxq
V2EX  ›  iDev

objc Runtime 关联对象为什么不走 set 方法?

  •  
  •   cxq · 2015-09-09 09:38:32 +08:00 · 2256 次点击
    这是一个创建于 3398 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下:

    @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 。

    2 条回复    2015-09-09 10:38:15 +08:00
    cxq
        1
    cxq  
    OP
       2015-09-09 09:49:58 +08:00
    贴的缩进太乱 大家将就看吧
    cxq
        2
    cxq  
    OP
       2015-09-09 10:38:15 +08:00
    搞定了 因为属性名原因 换成 animating 换成 rotateAnimating 就可以了。可能重名。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2777 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 06:41 · PVG 14:41 · LAX 22:41 · JFK 01:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.