Skip to content

Instantly share code, notes, and snippets.

@C4Code
Created May 13, 2012 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save C4Code/2675841 to your computer and use it in GitHub Desktop.
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
//
// 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
//
// 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
//
// 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