Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamazz committed Jun 10, 2015
1 parent 940b0db commit 8734da5
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 195 deletions.
16 changes: 16 additions & 0 deletions AMPopTip/AMPopTip+Animation.h
@@ -0,0 +1,16 @@
//
// AMPopTip+Animation.h
// AMPopTip
//
// Created by Andrea Mazzini on 10/06/15.
// Copyright (c) 2015 Fancy Pixel. All rights reserved.
//

#import "AMPopTip.h"

@interface AMPopTip (Animation)

- (void)_startActionAnimation;
- (void)_stopActionAnimation;

@end
112 changes: 112 additions & 0 deletions AMPopTip/AMPopTip+Animation.m
@@ -0,0 +1,112 @@
//
// AMPopTip+Animation.m
// AMPopTip
//
// Created by Andrea Mazzini on 10/06/15.
// Copyright (c) 2015 Fancy Pixel. All rights reserved.
//

#import "AMPopTip+Animation.h"
#import <objc/runtime.h>

@implementation AMPopTip (Animation)

- (void)setShouldBounce:(BOOL)shouldBounce { objc_setAssociatedObject(self, @selector(shouldBounce), [NSNumber numberWithBool:shouldBounce], OBJC_ASSOCIATION_RETAIN);}
- (BOOL)shouldBounce { return [objc_getAssociatedObject(self, @selector(shouldBounce)) boolValue]; }

- (void)_startActionAnimation {
switch (self.actionAnimation) {
case AMPopTipActionAnimationBounce:
self.shouldBounce = YES;
[self bounceAnimation];
break;
case AMPopTipActionAnimationFloat:
[self floatAnimation];
break;
case AMPopTipActionAnimationPulse:
[self pulseAnimation];
break;
case AMPopTipActionAnimationNone:
return;
break;
default:
break;
}
}

- (void)floatAnimation {
CGFloat xOffset = 0;
CGFloat yOffset = 0;
switch (self.direction) {
case AMPopTipDirectionUp:
yOffset = -self.actionFloatOffset;
break;
case AMPopTipDirectionDown:
yOffset = self.actionFloatOffset;
break;
case AMPopTipDirectionLeft:
xOffset = -self.actionFloatOffset;
break;
case AMPopTipDirectionRight:
xOffset = self.actionFloatOffset;
break;
case AMPopTipDirectionNone:
yOffset = -self.actionFloatOffset;
break;
}

[UIView animateWithDuration:(self.actionAnimationIn / 2) delay:self.actionDelayIn options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction) animations:^{
self.transform = CGAffineTransformMakeTranslation(xOffset, yOffset);
} completion:nil];
}

- (void)bounceAnimation {
CGFloat xOffset = 0;
CGFloat yOffset = 0;
switch (self.direction) {
case AMPopTipDirectionUp:
yOffset = -self.actionBounceOffset;
break;
case AMPopTipDirectionDown:
yOffset = self.actionBounceOffset;
break;
case AMPopTipDirectionLeft:
xOffset = -self.actionBounceOffset;
break;
case AMPopTipDirectionRight:
xOffset = self.actionBounceOffset;
break;
case AMPopTipDirectionNone:
yOffset = -self.actionBounceOffset;
break;
}

[UIView animateWithDuration:(self.actionAnimationIn / 10) delay:self.actionDelayIn options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{
self.transform = CGAffineTransformMakeTranslation(xOffset, yOffset);
} completion:^(BOOL finished) {
[UIView animateWithDuration:(self.actionAnimationIn - self.actionAnimationIn / 10) delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:1 options:0 animations:^{
self.transform = CGAffineTransformIdentity;
} completion:^(BOOL done) {
if (self.shouldBounce && done) {
[self bounceAnimation];
}
}];
}];
}

- (void)pulseAnimation {
[UIView animateWithDuration:(self.actionAnimationIn / 2) delay:self.actionDelayIn options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction) animations:^{
self.transform = CGAffineTransformMakeScale(self.actionPulseOffset, self.actionPulseOffset);
} completion:nil];
}

- (void)_stopActionAnimation {
self.shouldBounce = NO;
[UIView animateWithDuration:(self.actionAnimationOut / 2) delay:self.actionDelayOut options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[self.layer removeAllAnimations];
}];
}

@end
15 changes: 15 additions & 0 deletions AMPopTip/AMPopTip+Entrance.h
@@ -0,0 +1,15 @@
//
// AMPopTip+Entrance.h
// AMPopTip
//
// Created by Andrea Mazzini on 10/06/15.
// Copyright (c) 2015 Fancy Pixel. All rights reserved.
//

#import "AMPopTip.h"

@interface AMPopTip (Entrance)

- (void)performEntranceAnimation:(void (^)())completion;

@end
87 changes: 87 additions & 0 deletions AMPopTip/AMPopTip+Entrance.m
@@ -0,0 +1,87 @@
//
// AMPopTip+Entrance.m
// AMPopTip
//
// Created by Andrea Mazzini on 10/06/15.
// Copyright (c) 2015 Fancy Pixel. All rights reserved.
//

#import "AMPopTip+Entrance.h"

@implementation AMPopTip (Entrance)

- (void)performEntranceAnimation:(void (^)())completion {
switch (self.entranceAnimation) {
case AMPopTipEntranceAnimationScale: {
[self entranceScale:completion];
break;
}
case AMPopTipEntranceAnimationTransition: {
[self entranceTransition:completion];
break;
}
case AMPopTipEntranceAnimationCustom: {
[self.containerView addSubview:self];
if (self.entranceAnimationHandler) {
self.entranceAnimationHandler(^{
completion();
});
}
}
case AMPopTipEntranceAnimationNone: {
[self.containerView addSubview:self];
completion();
break;
}
default:
break;
}
}

- (void)entranceTransition:(void (^)())completion {
self.transform = CGAffineTransformMakeScale(0.6, 0.6);
switch (self.direction) {
case AMPopTipDirectionUp:
self.transform = CGAffineTransformTranslate(self.transform, 0, -self.fromFrame.origin.y);
break;
case AMPopTipDirectionDown:
self.transform = CGAffineTransformTranslate(self.transform, 0, (self.containerView.frame.size.height - self.fromFrame.origin.y));
break;
case AMPopTipDirectionLeft:
self.transform = CGAffineTransformTranslate(self.transform, -self.fromFrame.origin.x, 0);
break;
case AMPopTipDirectionRight:
self.transform = CGAffineTransformTranslate(self.transform, (self.containerView.frame.size.width - self.fromFrame.origin.x), 0);
break;
case AMPopTipDirectionNone:
self.transform = CGAffineTransformTranslate(self.transform, 0, (self.containerView.frame.size.height - self.fromFrame.origin.y));
break;

default:
break;
}
[self.containerView addSubview:self];

[UIView animateWithDuration:self.animationIn delay:self.delayIn usingSpringWithDamping:0.6 initialSpringVelocity:1.5 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
self.transform = CGAffineTransformIdentity;
} completion:^(BOOL completed){
if (completed) {
completion();
}
}];
}

- (void)entranceScale:(void (^)())completion {
self.transform = CGAffineTransformMakeScale(0, 0);
[self.containerView addSubview:self];

[UIView animateWithDuration:self.animationIn delay:self.delayIn usingSpringWithDamping:0.6 initialSpringVelocity:1.5 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
self.transform = CGAffineTransformIdentity;
} completion:^(BOOL completed){
if (completed) {
completion();
}
}];
}

@end
12 changes: 12 additions & 0 deletions AMPopTip/AMPopTip.h
Expand Up @@ -342,4 +342,16 @@ typedef NS_ENUM(NSInteger, AMPopTipActionAnimation) {
*/
@property (nonatomic, readonly) CGPoint arrowPosition;

/** Container View
*
* A read only reference to the view containing the poptip
*/
@property (nonatomic, weak, readonly) UIView *containerView;

/** Direction
*
* The direction from which the poptip is shown. Read only.
*/
@property (nonatomic, assign, readonly) AMPopTipDirection direction;

@end

0 comments on commit 8734da5

Please sign in to comment.