#define kAnimationKey @"transitionViewAnimation"
#define HORIZ_SWIPE_DRAG_MIN 12
#define VERT_SWIPE_DRAG_MAX 4
// The following swipe code derives from Apple Sample Code
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
startTouchPosition = [touch locationInView:self];
dirString = NULL;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = touches.anyObject;
CGPoint currentTouchPosition = [touch locationInView:self];
if (fabsf(startTouchPosition.x - currentTouchPosition.x) >=
HORIZ_SWIPE_DRAG_MIN &&
fabsf(startTouchPosition.y - currentTouchPosition.y) <=
VERT_SWIPE_DRAG_MAX)
{
// Horizontal Swipe
if (startTouchPosition.x < currentTouchPosition.x) {
dirString = kCATransitionFromLeft;
}
else
dirString = kCATransitionFromRight;
}
else if (fabsf(startTouchPosition.y - currentTouchPosition.y) >=
HORIZ_SWIPE_DRAG_MIN &&
fabsf(startTouchPosition.x - currentTouchPosition.x) <=
VERT_SWIPE_DRAG_MAX)
{
// Vertical Swipe
if (startTouchPosition.y < currentTouchPosition.y)
dirString = kCATransitionFromBottom;
else
dirString = kCATransitionFromTop;
} else
{
// Process a non-swipe event.
// dirString = NULL;
}
}
- (CATransition *) getAnimation:(NSString *) direction
{
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
// [animation setType:@"oglFlip"];
[animation setType:kCATransitionPush];
[animation setSubtype:direction];
[animation setDuration:1.0f];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
return animation;
}
- (void) MoveLabel:(NSString *) direction
{
// First create a CATransition object to describe the transition
CATransition *transition = [CATransition animation];
// Animate over 3/4 of a second
transition.duration = 0.75;
// using the ease in/out timing function
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// Now to set the type of transition. Since we need to choose at random, we'll setup a couple of arrays to help us.
transition.type = kCATransitionPush;
transition.subtype = direction;
// Finally, to avoid overlapping transitions we assign ourselves as the delegate for the animation and wait for the
// -animationDidStop:finished: message. When it comes in, we will flag that we are no longer transitioning.
transitioning = YES;
transition.delegate = self;
// Next add it to the containerView's layer. This will perform the transition based on how we change its contents.
[self.layer addAnimation:transition forKey:nil];
[self switchPage];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (dirString)
{
//CATransition *animation = [self getAnimation:dirString];
//[self exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
//[[self layer] addAnimation:animation forKey:kAnimationKey];
[self MoveLabel:dirString];
}
}