Skip to content
Browse files

Added additional diagonal direction support

  • Loading branch information...
1 parent 3a1b48d commit 33fc2a0145156df9b81ae4526818b3a595e17d32 @coreyfloyd coreyfloyd committed
Showing with 191 additions and 30 deletions.
  1. +19 −0 Classes/FTAnimation+UIView.m
  2. +96 −1 Classes/FTAnimationManager.m
  3. +2 −2 Examples/BackInOut.m
  4. +6 −2 Examples/SlideInOut.m
  5. +68 −25 Headers/FTUtils/FTAnimationManager.h
View
19 Classes/FTAnimation+UIView.m
@@ -56,6 +56,25 @@ - (void)slideOutTo:(FTAnimationDirection)direction duration:(NSTimeInterval)dura
[self slideOutTo:direction duration:duration delegate:delegate startSelector:nil stopSelector:nil];
}
+#pragma mark -
+
+- (void)slideInFrom:(FTAnimationDirection)direction inView:(UIView*)enclosingView duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector {
+ CAAnimation *slideInAnim = [[FTAnimationManager sharedManager] slideInAnimationFor:self direction:direction inView:(UIView*)enclosingView
+ duration:duration delegate:delegate
+ startSelector:startSelector stopSelector:stopSelector];
+ [self.layer addAnimation:slideInAnim forKey:kFTAnimationSlideIn];
+}
+
+- (void)slideOutTo:(FTAnimationDirection)direction inView:(UIView*)enclosingView duration:(NSTimeInterval)duration
+ delegate:(id)delegate startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector {
+ CAAnimation *slideOutAnim = [[FTAnimationManager sharedManager] slideOutAnimationFor:self direction:direction inView:(UIView*)enclosingView
+ duration:duration delegate:delegate
+ startSelector:startSelector stopSelector:stopSelector];
+ [self.layer addAnimation:slideOutAnim forKey:kFTAnimationSlideOut];
+}
+
+
#pragma mark Back In/Out Animations
- (void)backOutTo:(FTAnimationDirection)direction withFade:(BOOL)fade duration:(NSTimeInterval)duration delegate:(id)delegate
View
97 Classes/FTAnimationManager.m
@@ -194,6 +194,32 @@ - (CAAnimation *)slideOutAnimationFor:(UIView *)view direction:(FTAnimationDirec
name:kFTAnimationSlideOut type:kFTAnimationTypeOut];
}
+
+#pragma mark -
+
+- (CAAnimation *)slideInAnimationFor:(UIView *)view direction:(FTAnimationDirection)direction inView:(UIView*)enclosingView
+ duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector {
+ CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
+ animation.fromValue = [NSValue valueWithCGPoint:FTAnimationOutOfViewCenterPoint(enclosingView.bounds, view.frame, view.center, direction)];
+ animation.toValue = [NSValue valueWithCGPoint:view.center];
+ return [self animationGroupFor:[NSArray arrayWithObject:animation] withView:view duration:duration
+ delegate:delegate startSelector:startSelector stopSelector:stopSelector
+ name:kFTAnimationSlideIn type:kFTAnimationTypeIn];
+}
+
+- (CAAnimation *)slideOutAnimationFor:(UIView *)view direction:(FTAnimationDirection)direction inView:(UIView*)enclosingView
+ duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector{
+ CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
+ animation.fromValue = [NSValue valueWithCGPoint:view.center];
+ animation.toValue = [NSValue valueWithCGPoint:FTAnimationOutOfViewCenterPoint(view.superview.bounds, view.frame, view.center, direction)];
+ return [self animationGroupFor:[NSArray arrayWithObject:animation] withView:view duration:duration
+ delegate:delegate startSelector:startSelector stopSelector:stopSelector
+ name:kFTAnimationSlideOut type:kFTAnimationTypeOut];
+}
+
+
#pragma mark -
#pragma mark Bounce Animation Builders
@@ -201,9 +227,18 @@ - (CGPoint)overshootPointFor:(CGPoint)point withDirection:(FTAnimationDirection)
CGPoint overshootPoint;
if(direction == kFTAnimationTop || direction == kFTAnimationBottom) {
overshootPoint = CGPointMake(point.x, point.y + ((direction == kFTAnimationBottom ? -1 : 1) * threshold));
- } else {
+ } else if (direction == kFTAnimationLeft || direction == kFTAnimationRight){
overshootPoint = CGPointMake(point.x + ((direction == kFTAnimationRight ? -1 : 1) * threshold), point.y);
+ } else if (direction == kFTAnimationTopLeft){
+ overshootPoint = CGPointMake(point.x + threshold, point.y + threshold);
+ } else if (direction == kFTAnimationTopRight){
+ overshootPoint = CGPointMake(point.x - threshold, point.y + threshold);
+ } else if (direction == kFTAnimationBottomLeft){
+ overshootPoint = CGPointMake(point.x + threshold, point.y - threshold);
+ } else if (direction == kFTAnimationBottomRight){
+ overshootPoint = CGPointMake(point.x - threshold, point.y - threshold);
}
+
return overshootPoint;
}
@@ -263,6 +298,66 @@ - (CAAnimation *)backInAnimationFor:(UIView *)view withFade:(BOOL)fade direction
name:kFTAnimationBackIn type:kFTAnimationTypeIn];
}
+
+#pragma mark -
+
+- (CAAnimation *)backOutAnimationFor:(UIView *)view withFade:(BOOL)fade direction:(FTAnimationDirection)direction inView:(UIView*)enclosingView
+ duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector {
+ CGPoint path[3] = {
+ view.center,
+ [self overshootPointFor:view.center withDirection:direction threshold:overshootThreshold_],
+ FTAnimationOutOfViewCenterPoint(enclosingView.bounds, view.frame, view.center, direction)
+ };
+
+ CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
+ CGMutablePathRef thePath = CGPathCreateMutable();
+ CGPathAddLines(thePath, NULL, path, 3);
+ animation.path = thePath;
+ CGPathRelease(thePath);
+ NSArray *animations;
+ if(fade) {
+ CAAnimation *fade = [self fadeAnimationFor:view duration:duration * .5f delegate:nil startSelector:nil stopSelector:nil fadeOut:YES];
+ fade.beginTime = duration * .5f;
+ fade.fillMode = kCAFillModeForwards;
+ animations = [NSArray arrayWithObjects:animation, fade, nil];
+ } else {
+ animations = [NSArray arrayWithObject:animation];
+ }
+ return [self animationGroupFor:animations withView:view duration:duration
+ delegate:delegate startSelector:startSelector stopSelector:stopSelector
+ name:kFTAnimationBackOut type:kFTAnimationTypeOut];
+}
+
+
+- (CAAnimation *)backInAnimationFor:(UIView *)view withFade:(BOOL)fade direction:(FTAnimationDirection)direction inView:(UIView*)enclosingView
+ duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector {
+ CGPoint path[3] = {
+ FTAnimationOutOfViewCenterPoint(enclosingView.bounds, view.frame, view.center, direction),
+ [self overshootPointFor:view.center withDirection:direction threshold:(overshootThreshold_ * 1.15)],
+ view.center
+ };
+
+ CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
+ CGMutablePathRef thePath = CGPathCreateMutable();
+ CGPathAddLines(thePath, NULL, path, 3);
+ animation.path = thePath;
+ CGPathRelease(thePath);
+ NSArray *animations;
+ if(fade) {
+ CAAnimation *fade = [self fadeAnimationFor:view duration:duration * .5f delegate:nil startSelector:nil stopSelector:nil fadeOut:NO];
+ fade.fillMode = kCAFillModeForwards;
+
+ animations = [NSArray arrayWithObjects:animation, fade, nil];
+ } else {
+ animations = [NSArray arrayWithObject:animation];
+ }
+ return [self animationGroupFor:animations withView:view duration:duration
+ delegate:delegate startSelector:startSelector stopSelector:stopSelector
+ name:kFTAnimationBackIn type:kFTAnimationTypeIn];
+}
+
#pragma mark -
#pragma mark Fade Animation Builders
View
4 Examples/BackInOut.m
@@ -37,9 +37,9 @@ - (void)viewDidLoad {
- (void)performAnimation:(id)sender {
if(self.viewToAnimate.hidden) {
- [self.viewToAnimate backInFrom:self.directionControl.selectedSegmentIndex withFade:NO duration:.4 delegate:nil];
+ [self.viewToAnimate backInFrom:self.directionControl.selectedSegmentIndex+4 withFade:NO duration:.4 delegate:nil];
} else {
- [self.viewToAnimate backOutTo:self.directionControl.selectedSegmentIndex withFade:NO duration:.4 delegate:nil];
+ [self.viewToAnimate backOutTo:self.directionControl.selectedSegmentIndex+4 withFade:NO duration:.4 delegate:nil];
}
}
View
8 Examples/SlideInOut.m
@@ -24,6 +24,7 @@ of this software and associated documentation files (the "Software"), to deal
#import "SlideInOut.h"
#import "FTAnimation.h"
+#import "FTAnimation+UIView.h"
@implementation SlideInOut
@@ -37,9 +38,12 @@ - (void)viewDidLoad {
- (void)performAnimation:(id)sender {
if(self.viewToAnimate.hidden) {
- [self.viewToAnimate slideInFrom:self.directionControl.selectedSegmentIndex duration:.4 delegate:nil];
+ //[self.viewToAnimate slideInFrom:self.directionControl.selectedSegmentIndex duration:.4 delegate:nil];
+ [self.viewToAnimate slideInFrom:self.directionControl.selectedSegmentIndex+4 inView:self.viewToAnimate.superview duration:0.4 delegate:nil startSelector:nil stopSelector:nil];
} else {
- [self.viewToAnimate slideOutTo:self.directionControl.selectedSegmentIndex duration:.4 delegate:nil];
+ //[self.viewToAnimate slideOutTo:self.directionControl.selectedSegmentIndex duration:.4 delegate:nil];
+ [self.viewToAnimate slideOutTo:self.directionControl.selectedSegmentIndex+4 inView:self.viewToAnimate.superview duration:0.4 delegate:nil startSelector:nil stopSelector:nil];
+
}
}
View
93 Headers/FTUtils/FTAnimationManager.h
@@ -34,7 +34,11 @@ typedef enum _FTAnimationDirection {
kFTAnimationTop = 0,
kFTAnimationRight,
kFTAnimationBottom,
- kFTAnimationLeft
+ kFTAnimationLeft,
+ kFTAnimationTopLeft,
+ kFTAnimationTopRight,
+ kFTAnimationBottomLeft,
+ kFTAnimationBottomRight
} FTAnimationDirection;
#pragma mark String Constants
@@ -62,31 +66,61 @@ extern NSString *const kFTAnimationTargetViewKey;
#pragma mark Inline Functions
+
+static inline CGPoint FTAnimationOutOfViewCenterPoint(CGRect enclosingViewFrame, CGRect viewFrame, CGPoint viewCenter, FTAnimationDirection direction) {
+ switch (direction) {
+ case kFTAnimationBottom: {
+ CGFloat extraOffset = viewFrame.size.height / 2;
+ return CGPointMake(viewCenter.x, enclosingViewFrame.size.height + extraOffset);
+ break;
+ }
+ case kFTAnimationTop: {
+ CGFloat extraOffset = viewFrame.size.height / 2;
+ return CGPointMake(viewCenter.x, enclosingViewFrame.origin.y - extraOffset);
+ break;
+ }
+ case kFTAnimationLeft: {
+ CGFloat extraOffset = viewFrame.size.width / 2;
+ return CGPointMake(enclosingViewFrame.origin.x - extraOffset, viewCenter.y);
+ break;
+ }
+ case kFTAnimationRight: {
+ CGFloat extraOffset = viewFrame.size.width / 2;
+ return CGPointMake(enclosingViewFrame.size.width + extraOffset, viewCenter.y);
+ break;
+ }
+ case kFTAnimationBottomLeft: {
+ CGFloat extraOffsetHeight = viewFrame.size.height / 2;
+ CGFloat extraOffsetWidth = viewFrame.size.width / 2;
+ return CGPointMake(enclosingViewFrame.origin.x - extraOffsetWidth, enclosingViewFrame.size.height + extraOffsetHeight);
+ break;
+ }
+ case kFTAnimationTopLeft: {
+ CGFloat extraOffsetHeight = viewFrame.size.height / 2;
+ CGFloat extraOffsetWidth = viewFrame.size.width / 2;
+ return CGPointMake(enclosingViewFrame.origin.x - extraOffsetWidth, enclosingViewFrame.origin.y - extraOffsetHeight);
+ break;
+ }
+ case kFTAnimationBottomRight: {
+ CGFloat extraOffsetHeight = viewFrame.size.height / 2;
+ CGFloat extraOffsetWidth = viewFrame.size.width / 2;
+ return CGPointMake(enclosingViewFrame.size.width + extraOffsetWidth, enclosingViewFrame.size.height + extraOffsetHeight);
+ break;
+ }
+ case kFTAnimationTopRight: {
+ CGFloat extraOffsetHeight = viewFrame.size.height / 2;
+ CGFloat extraOffsetWidth = viewFrame.size.width / 2;
+ return CGPointMake(enclosingViewFrame.size.width + extraOffsetWidth, enclosingViewFrame.origin.y - extraOffsetHeight);
+ break;
+ }
+
+ }
+ return CGPointZero;
+}
+
static inline CGPoint FTAnimationOffscreenCenterPoint(CGRect viewFrame, CGPoint viewCenter, FTAnimationDirection direction) {
- CGRect screenRect = [[UIScreen mainScreen] bounds];
- switch (direction) {
- case kFTAnimationBottom: {
- CGFloat extraOffset = viewFrame.size.height / 2;
- return CGPointMake(viewCenter.x, screenRect.size.height + extraOffset);
- break;
- }
- case kFTAnimationTop: {
- CGFloat extraOffset = viewFrame.size.height / 2;
- return CGPointMake(viewCenter.x, screenRect.origin.y - extraOffset);
- break;
- }
- case kFTAnimationLeft: {
- CGFloat extraOffset = viewFrame.size.width / 2;
- return CGPointMake(screenRect.origin.x - extraOffset, viewCenter.y);
- break;
- }
- case kFTAnimationRight: {
- CGFloat extraOffset = viewFrame.size.width / 2;
- return CGPointMake(screenRect.size.width + extraOffset, viewCenter.y);
- break;
- }
- }
- return CGPointZero;
+
+ return FTAnimationOutOfViewCenterPoint([[UIScreen mainScreen] bounds], viewFrame, viewCenter, direction);
}
/*!
@@ -128,6 +162,15 @@ static inline CGPoint FTAnimationOffscreenCenterPoint(CGRect viewFrame, CGPoint
duration:(NSTimeInterval)duration delegate:(id)delegate
startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
+- (CAAnimation *)slideInAnimationFor:(UIView *)view direction:(FTAnimationDirection)direction inView:(UIView*)enclosingView
+ duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
+
+- (CAAnimation *)slideOutAnimationFor:(UIView *)view direction:(FTAnimationDirection)direction inView:(UIView*)enclosingView
+ duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
+
+
- (CAAnimation *)backOutAnimationFor:(UIView *)view withFade:(BOOL)fade direction:(FTAnimationDirection)direction
duration:(NSTimeInterval)duration delegate:(id)delegate
startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;

0 comments on commit 33fc2a0

Please sign in to comment.
Something went wrong with that request. Please try again.