el.*_*ero 1 class objective-c cocos2d-iphone
我宣布:
CCSprite + DisableTouch.h文件
#import "cocos2d.h"
@interface CCSprite (DisableTouch) <CCTargetedTouchDelegate> {
}
-(void)disableTouch;
-(void)enableTouch;
@end
Run Code Online (Sandbox Code Playgroud)
并为CCSprite + DisableTouch.m文件
#import "CCSprite+DisableTouch.h"
@implementation CCSprite (DisableTouch)
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}
-(void)disableTouch
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1000 swallowsTouches:YES];
}
-(void)enableTouch
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
}
@end
Run Code Online (Sandbox Code Playgroud)
为什么我得到:Expected identifier or '(' before '{' token错误?我该怎么解决这个问题?
错误是通过在类别中添加{&}字符,因为类别不能包含iVar.
如果您需要具有类别的iVar,请考虑使用关联对象:
#import <objc/runtime.h>
...
objc_setAssociatedObject(self, @"iVar", @"value", OBJC_ASSOCIATION_ASSIGN);
...
id iVar = objc_getAssociatedObject(self, @"iVar");
Run Code Online (Sandbox Code Playgroud)
有关关联对象的更多信息,请查看此处.