Last active
December 17, 2015 02:49
-
-
Save dabing1022/5539075 to your computer and use it in GitHub Desktop.
实现触摸
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark - touch delegate | |
- (CGPoint)locationFromTouch:(UITouch *)touch | |
{ | |
CGPoint touchLocation = [touch locationInView:[touch view]]; | |
return [[CCDirector sharedDirector]convertToGL:touchLocation]; | |
} | |
- (BOOL)containsTouchLocation:(UITouch *)touch | |
{ | |
CGRect selfRect = CGRectMake(self.position.x, self.position.y, self.contentSize.width, self.contentSize.height); | |
return CGRectContainsPoint(selfRect, [self locationFromTouch:touch]); | |
} | |
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event | |
{ | |
if([self containsTouchLocation:touch]){ | |
return YES; | |
} | |
return NO; | |
} | |
- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event | |
{ | |
CCLOG(@"ccTouchCancelled"); | |
} | |
- (CGPoint)locationFromTouch:(UITouch *)touch | |
{ | |
CGPoint touchLocation = [touch locationInView:[touch view]]; | |
return [[CCDirector sharedDirector]convertToGL:touchLocation]; | |
} | |
- (BOOL)containsTouchLocation:(UITouch *)touch node:(CCNode *)node | |
{ | |
return CGRectContainsPoint([node boundingBox], [self locationFromTouch:touch]); | |
} | |
#pragma mark - onEnter/onExit | |
- (void)onEnter | |
{ | |
[[[CCDirector sharedDirector]touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; | |
[super onEnter]; | |
} | |
- (void)onExit | |
{ | |
[[[CCDirector sharedDirector]touchDispatcher]removeDelegate:self]; | |
[super onExit]; | |
} |
Author
dabing1022
commented
May 8, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment