Skip to content

Instantly share code, notes, and snippets.

@dabing1022
Last active December 17, 2015 02:49
Show Gist options
  • Save dabing1022/5539075 to your computer and use it in GitHub Desktop.
Save dabing1022/5539075 to your computer and use it in GitHub Desktop.
实现触摸
#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];
}
@dabing1022
Copy link
Author

- (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]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment