Skip to content

Instantly share code, notes, and snippets.

@mzarra
Created December 12, 2008 01:16
Show Gist options
  • Save mzarra/34982 to your computer and use it in GitHub Desktop.
Save mzarra/34982 to your computer and use it in GitHub Desktop.
Secret Code Sample
@interface TouchableView : UIView
{
CGPoint touchBeganPoint;
CGPoint lastUpdatedPoint;
NSArray *colorArray;
NSInteger colorIndex;
}
@end
#import "TouchableView.h"
#define MAX_TAP_DELTA 1.0f
@implementation TouchableView
- (id)initWithFrame:(CGRect)frame
{
if (![super initWithFrame:frame]) return nil;
colorArray = [[NSArray alloc] initWithObjects:[UIColor greenColor],
[UIColor blueColor], [UIColor redColor], nil];
colorIndex = 0;
[self setBackgroundColor:[colorArray objectAtIndex:colorIndex]];
touchBeganPoint = CGPointZero;
[self setUserInteractionEnabled:YES];
[self setMultipleTouchEnabled:YES];
return self;
}
- (void)dealloc
{
[colorArray release], colorArray = nil;
[super dealloc];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%@:%s touches %u", [self class], _cmd, [touches count]);
UITouch *beginTouch = [touches anyObject];
touchBeganPoint = [beginTouch locationInView:[self window]];
lastUpdatedPoint = [beginTouch locationInView:[self window]];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%@:%s touches %u", [self class], _cmd, [touches count]);
touchBeganPoint = CGPointZero;
lastUpdatedPoint = CGPointZero;
}
- (void)throbAnimationDidStop:(NSString*)animationID
finished:(NSNumber*)finished
context:(void*)context
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.15];
[self setTransform:CGAffineTransformMakeScale(1.1, 1.1)];
[UIView commitAnimations];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%@:%s touches %u", [self class], _cmd, [touches count]);
CGPoint touchPoint = [[touches anyObject] locationInView:[self window]];
float deltaX = touchPoint.x - touchBeganPoint.x;
float deltaY = touchPoint.y - touchBeganPoint.y;
touchBeganPoint = CGPointZero;
lastUpdatedPoint = CGPointZero;
if (deltaX > MAX_TAP_DELTA && deltaY > MAX_TAP_DELTA) {
return;
}
++colorIndex;
if (colorIndex >= [colorArray count]) colorIndex = 0;
CGColorRef color = [[colorArray objectAtIndex:colorIndex] CGColor];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.15f];
[UIView setAnimationDidStopSelector:@selector(throbAnimationDidStop:finished:context:)];
[self setTransform:CGAffineTransformMakeScale(1.4, 1.4)];
[[self layer] setBackgroundColor:color];
[UIView commitAnimations];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%@:%s touches %u", [self class], _cmd, [touches count]);
[UIView beginAnimations:nil context:NULL];
CGPoint touchPoint = [[touches anyObject] locationInView:[self window]];
float deltaX = touchPoint.x - lastUpdatedPoint.x;
float deltaY = touchPoint.y - lastUpdatedPoint.y;
float newCenterX = [[self layer] position].x + deltaX;
float newCenterY = [[self layer] position].y + deltaY;
CGPoint center = CGPointMake(newCenterX, newCenterY);
[[self layer] setPosition:center];
lastUpdatedPoint = touchPoint;
[UIView commitAnimations];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment