@protocol GestureUIDelegate
-(void) singleTapGesture:(UITouch*)touch;
-(void) doubleTapGesture:(UITouch*)touch;
-(void) twoFingerTouchGestureAt:(UITouch*)touch1 and:(UITouch*)touch2;
-(void) touchAndHoldGesture:(UITouch*)touch;
@end
UIGestureView.h
#import "GestureUIDelegate.h"
@interface UIBase : UIView
{
id gestureDelegate; //bug with blogger. it won't show that id implements the GestureUIDelegate protocol
BOOL movable;
BOOL resizable;
BOOL userMoving;
BOOL userResizing;
CGPoint lastPoint;
int resizeHandleSize;
double touchDelay;
double tapDelay;
@private
UIImageView* border;
NSTimer* touchTimer;
UITouch* heldTouch;
float minWidth;
float minHeight;
NSTimer* tapTimer;
UITouch* tapTouch;
}
#pragma mark Properties
@property (nonatomic, retain) id gestureDelegate;
@property BOOL movable;
@property BOOL resizable;
@property float minWidth;
@property float minHeight;
@property float x;
@property float y;
@property float width;
@property float height;
#pragma mark Public Methods
-(void) initializeDefaults;
#pragma mark Custom Events
-(void) singleTapGesture:(UITouch*)touch;
-(void) doubleTapGesture:(UITouch*)touch;
-(void) twoFingerTouchGestureAt:(UITouch*)touch1 and:(UITouch*)touch2;
-(void) touchAndHoldGesture:(UITouch*)touch;
#pragma mark Private Methods
-(BOOL) touchInWindow:(UITouch*) touch;
-(BOOL) touchInBottomRightCorner:(UITouch*) touch;
-(void) startTouchAndHold:(UITouch*)touch;
-(void) cancelTouchAndHold;
-(void) startSingleTouch:(UITouch*)touch;
-(void) cancelSingleTouch;
@end
UIGestureView.m
#import "UIGestureView.h"
@implementation UIBase
@synthesize resizable;
@synthesize movable;
@synthesize gestureDelegate;
@dynamic x;
@dynamic y;
@dynamic width;
@dynamic height;
@dynamic minWidth;
@dynamic minHeight;
-(id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(!self)
return nil;
[self initializeDefaults];
return self;
}
-(void) initializeDefaults
{
self.minWidth = 50;
self.minHeight = 50;
resizeHandleSize = 20;
self.backgroundColor = [UIColor redColor];
self.multipleTouchEnabled = YES;
touchDelay = 0.5;
tapDelay = 0.2;
//self.resizable = YES;
//self.movable = YES;
}
#pragma mark Touch Events
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self superview] bringSubviewToFront:self];
UITouch* touch = [touches anyObject];
if(touch.tapCount == 2)
{
[self cancelSingleTouch];
[self doubleTapGesture:touch];
}
else if(touch.tapCount == 1)
{
if([touches count] == 1)
{
lastPoint = [touch locationInView:self.superview];
BOOL touchInWindow = [self touchInWindow:touch];
if(touchInWindow)
{
userResizing = [self touchInBottomRightCorner:touch] && resizable;
if(!userResizing)
{
[self startTouchAndHold:touch];
userMoving = touchInWindow && movable;
}
}
}
}
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self cancelTouchAndHold];
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self.superview];
float xChange = point.x - lastPoint.x;
float yChange = point.y - lastPoint.y;
if(userResizing)
{
float width = self.frame.size.width + xChange;
float height = self.frame.size.height + yChange;
if (width >= self.minWidth && height >= self.minHeight)
{
self.width = width;
self.height = height;
[self setNeedsDisplay];
}
}
else if(userMoving)
{
float oldX = self.frame.origin.x;
float oldY = self.frame.origin.y;
float x = oldX + xChange;
float y = oldY + yChange;
self.x = x;
self.y = y;
}
lastPoint = point;
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self cancelTouchAndHold];
if([touches count] >= 2)
{
UITouch* touch1 = [[touches allObjects] objectAtIndex:0];
UITouch* touch2 = [[touches allObjects] objectAtIndex:1];
[self twoFingerTouchGestureAt:touch1 and:touch2];
}
else
{
UITouch* touch = [touches anyObject];
if([touch tapCount]== 1)
[self startSingleTouch:touch];
}
userResizing = NO;
userMoving = NO;
}
#pragma mark Private Methods
-(void) singleTapGestureSucceeded
{
UITouch* touch = tapTouch;
[self cancelSingleTouch];
[self singleTapGesture:touch];
}
-(void) startSingleTouch:(UITouch*)touch
{
tapTouch = touch;
tapTimer = [[NSTimer scheduledTimerWithTimeInterval:tapDelay target:self selector:@selector(singleTapGestureSucceeded) userInfo:nil repeats:NO] retain];
}
-(void) cancelSingleTouch
{
if(tapTimer)
{
[tapTimer invalidate];
[tapTimer release];
}
tapTouch = nil;
tapTimer = nil;
}
-(void) startTouchAndHold:(UITouch*)touch
{
heldTouch = touch;
touchTimer = [[NSTimer scheduledTimerWithTimeInterval:touchDelay target:self selector:@selector(touchHasBeenHeld) userInfo:nil repeats:NO] retain];
}
-(void) cancelTouchAndHold
{
if(touchTimer)
{
[touchTimer invalidate];
[touchTimer release];
}
touchTimer = nil;
heldTouch = nil;
}
-(void) touchHasBeenHeld
{
userMoving = NO;
UITouch* touch = heldTouch;
[self cancelTouchAndHold];
[self touchAndHoldGesture:touch];
}
-(BOOL) touchInWindow:(UITouch*) touch
{
CGPoint location = [touch locationInView:self];
int x = location.x;
int y = location.y;
int width = self.frame.size.width;
int height = self.frame.size.height;
return (x > 0 && x < width) && (y > 0 && y < height);
}
-(BOOL) touchInBottomRightCorner:(UITouch*) touch
{
CGPoint location = [touch locationInView:self];
int x = location.x;
int y = location.y;
int width = self.frame.size.width;
int height = self.frame.size.height;
return (x > width - resizeHandleSize && x < width) && (y > height - resizeHandleSize && y < height);
}
#pragma mark Gestures
-(void) singleTapGesture:(UITouch*)touch
{
[gestureDelegate singleTapGesture:touch];
}
-(void) doubleTapGesture:(UITouch*)touch
{
[gestureDelegate doubleTapGesture:touch];
}
-(void) twoFingerTouchGestureAt:(UITouch*)touch1 and:(UITouch*)touch2
{
[gestureDelegate twoFingerTouchGestureAt:touch1 and:touch2];
}
-(void) touchAndHoldGesture:(UITouch*)touch
{
[gestureDelegate touchAndHoldGesture:touch];
}
#pragma mark Properties
-(float) x
{
return self.frame.origin.x;
}
-(void) setX:(float) x
{
CGRect newFrame = CGRectMake(x, self.y, self.width, self.height);
self.frame = newFrame;
}
-(float) y
{
return self.frame.origin.y;
}
-(void) setY:(float) y
{
CGRect newFrame = CGRectMake(self.x, y, self.width, self.height);
self.frame = newFrame;
}
-(float) width
{
return self.frame.size.width;
}
-(void) setWidth:(float) width
{
CGRect newFrame = CGRectMake(self.x, self.y, width, self.height);
self.frame = newFrame;
}
-(float) height
{
return self.frame.size.height;
}
-(void) setHeight:(float) height
{
CGRect newFrame = CGRectMake(self.x, self.y, self.width, height);
self.frame = newFrame;
}
-(float) minWidth
{
return minWidth;
}
-(void) setMinWidth:(float)width
{
minWidth = width;
}
-(float) minHeight
{
return minHeight;
}
-(void) setMinHeight:(float)height
{
minHeight = height;
}
@end
No comments:
Post a Comment