Created
May 13, 2012 03:34
-
-
Save C4Code/2675841 to your computer and use it in GitHub Desktop.
using a category on C4Shape to access the touches of a shape via the notification center
This file contains hidden or 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
// | |
// C4Shape+touchSetAccess.h | |
// accessTouches | |
// | |
// Created by Travis Kirton on 12-05-12. | |
// Copyright (c) 2012 POSTFL. All rights reserved. | |
// | |
#import "C4Shape.h" | |
@interface C4Shape (touchSetAccess) | |
-(void)postNotification:(NSString *)notification userInfo:(NSDictionary *)userInfo; | |
@end |
This file contains hidden or 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
// | |
// C4Shape+touchSetAccess.m | |
// accessTouches | |
// | |
// Created by Travis Kirton on 12-05-12. | |
// Copyright (c) 2012 POSTFL. All rights reserved. | |
// | |
#import "C4Shape+touchSetAccess.h" | |
@implementation C4Shape (touchSetAccess) | |
-(void)postNotification:(NSString *)notification userInfo:(NSDictionary *)userInfo { | |
[[NSNotificationCenter defaultCenter] postNotificationName:notification object:self userInfo:userInfo]; | |
} | |
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | |
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:touches forKey:@"touches"]; | |
[self postNotification:@"touchesBegan" userInfo:userInfo]; | |
[super touchesBegan:touches withEvent:event]; | |
[self touchesBegan]; | |
} | |
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { | |
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:touches forKey:@"touches"]; | |
[self postNotification:@"touchesMoved" userInfo:userInfo]; | |
[super touchesMoved:touches withEvent:event]; | |
[self touchesMoved]; | |
} | |
@end |
This file contains hidden or 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
// | |
// C4WorkSpace.m | |
// accessTouches | |
// | |
// Created by Travis Kirton on 12-05-12. | |
// Copyright (c) 2012 POSTFL. All rights reserved. | |
// | |
#import "C4WorkSpace.h" | |
@interface C4WorkSpace () | |
-(void)accessTouchSet:(NSNotification *)notification; | |
@end | |
@implementation C4WorkSpace { | |
C4Shape *s; | |
} | |
-(void)setup { | |
s.animationDuration = 0.0f; | |
s = [C4Shape rect:CGRectMake(0, 0, 100, 100)]; | |
[self.canvas addShape:s]; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(accessTouchSet:) | |
name:@"touchesMoved" | |
object:nil]; | |
} | |
-(void)accessTouchSet:(NSNotification *)notification { | |
NSDictionary *d = [notification userInfo]; | |
NSSet *touchSet = (NSSet *)[d objectForKey:@"touches"]; | |
UITouch *t = (UITouch *)[touchSet anyObject]; | |
CGPoint previousLocation = [t previousLocationInView:self.canvas]; | |
CGPoint currentLocation = [t locationInView:self.canvas]; | |
CGPoint displacement = CGPointMake(currentLocation.x - previousLocation.x, currentLocation.y - previousLocation.y); | |
C4Shape *currentShape = (C4Shape *)[notification object]; | |
CGPoint newCenter = currentShape.center; | |
newCenter.x += displacement.x; | |
newCenter.y += displacement.y; | |
currentShape.center = newCenter; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment