Permalink
Browse files

Merged coreyfloyd's changes (finally).

* FTAnimationDirection now supports screen corners
* Views can now be animated relative to their superviews
  • Loading branch information...
2 parents 9311db2 + 8da43a7 commit 30231a8dd57a2745642fd5b1c089e0655c7ece99 @neror committed Oct 13, 2010
@@ -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
@@ -83,6 +102,25 @@ - (void)backInFrom:(FTAnimationDirection)direction withFade:(BOOL)fade duration:
}
#pragma mark -
+
+- (void)backOutTo:(FTAnimationDirection)direction inView:(UIView*)enclosingView withFade:(BOOL)fade duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector {
+ CAAnimation *backOutAnim = [[FTAnimationManager sharedManager] backOutAnimationFor:self withFade:fade direction:direction inView:enclosingView
+ duration:duration delegate:delegate
+ startSelector:startSelector stopSelector:stopSelector];
+ [self.layer addAnimation:backOutAnim forKey:kFTAnimationBackOut];
+}
+
+- (void)backInFrom:(FTAnimationDirection)direction inView:(UIView*)enclosingView withFade:(BOOL)fade duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector {
+ CAAnimation *backInAnim = [[FTAnimationManager sharedManager] backInAnimationFor:self withFade:fade direction:direction inView:enclosingView
+ duration:duration delegate:delegate
+ startSelector:startSelector stopSelector:stopSelector];
+ [self.layer addAnimation:backInAnim forKey:kFTAnimationBackIn];
+}
+
+
+#pragma mark -
#pragma mark Fade Animations
- (void)fadeIn:(NSTimeInterval)duration delegate:(id)delegate startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector {
@@ -194,16 +194,51 @@ - (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
- (CGPoint)overshootPointFor:(CGPoint)point withDirection:(FTAnimationDirection)direction threshold:(CGFloat)threshold {
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
@@ -37,9 +37,12 @@ - (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];
+ [self.viewToAnimate backInFrom:self.directionControl.selectedSegmentIndex+4 inView:self.viewToAnimate.superview withFade:NO duration:0.4 delegate:nil startSelector:nil stopSelector: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];
+ [self.viewToAnimate backOutTo:self.directionControl.selectedSegmentIndex+4 inView:self.viewToAnimate.superview withFade:NO duration:0.4 delegate:nil startSelector:nil stopSelector:nil];
+
}
}
View
@@ -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];
+
}
}
@@ -33,13 +33,25 @@
- (void)slideOutTo:(FTAnimationDirection)direction duration:(NSTimeInterval)duration delegate:(id)delegate
startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
+//use the enclosing view
+- (void)slideInFrom:(FTAnimationDirection)direction inView:(UIView*)enclosingView duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
+- (void)slideOutTo:(FTAnimationDirection)direction inView:(UIView*)enclosingView duration:(NSTimeInterval)duration
+ delegate:(id)delegate startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
+
- (void)backOutTo:(FTAnimationDirection)direction withFade:(BOOL)fade duration:(NSTimeInterval)duration delegate:(id)delegate;
- (void)backOutTo:(FTAnimationDirection)direction withFade:(BOOL)fade duration:(NSTimeInterval)duration delegate:(id)delegate
startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
- (void)backInFrom:(FTAnimationDirection)direction withFade:(BOOL)fade duration:(NSTimeInterval)duration delegate:(id)delegate;
- (void)backInFrom:(FTAnimationDirection)direction withFade:(BOOL)fade duration:(NSTimeInterval)duration delegate:(id)delegate
startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
+//Use the enclosing view
+- (void)backOutTo:(FTAnimationDirection)direction inView:(UIView*)enclosingView withFade:(BOOL)fade duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
+- (void)backInFrom:(FTAnimationDirection)direction inView:(UIView*)enclosingView withFade:(BOOL)fade duration:(NSTimeInterval)duration delegate:(id)delegate
+ startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
+
- (void)fadeIn:(NSTimeInterval)duration delegate:(id)delegate;
- (void)fadeIn:(NSTimeInterval)duration delegate:(id)delegate startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
- (void)fadeOut:(NSTimeInterval)duration delegate:(id)delegate;
@@ -34,7 +34,11 @@ typedef enum _FTAnimationDirection {
kFTAnimationTop = 0,
kFTAnimationRight,
kFTAnimationBottom,
- kFTAnimationLeft
+ kFTAnimationLeft,
+ kFTAnimationTopLeft,
+ kFTAnimationTopRight,
+ kFTAnimationBottomLeft,
+ kFTAnimationBottomRight
} FTAnimationDirection;
#pragma mark String Constants
@@ -62,6 +66,56 @@ 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];
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
@@ -90,8 +144,10 @@ static inline CGPoint FTAnimationOffscreenCenterPoint(CGRect viewFrame, CGPoint
return CGPointMake(screenRect.size.width + extraOffset, viewCenter.y);
break;
}
+ default:
+ break;
}
- return CGPointZero;
+ return FTAnimationOutOfViewCenterPoint([[UIScreen mainScreen] bounds], viewFrame, viewCenter, direction);
}
/*!
@@ -133,13 +189,29 @@ 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;
- (CAAnimation *)backInAnimationFor:(UIView *)view withFade:(BOOL)fade direction:(FTAnimationDirection)direction
duration:(NSTimeInterval)duration delegate:(id)delegate
startSelector:(SEL)startSelector stopSelector:(SEL)stopSelector;
+- (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;
+- (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;
+
- (CAAnimation *)fadeAnimationFor:(UIView *)view duration:(NSTimeInterval)duration
delegate:(id)delegate startSelector:(SEL)startSelector
stopSelector:(SEL)stopSelector fadeOut:(BOOL)fadeOut;

0 comments on commit 30231a8

Please sign in to comment.