wxr
2020-06-16 f6fd8acd7c53c44187e70b4709443318a628f4b5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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)performActionAnimation {
    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)dismissActionAnimation {
    self.shouldBounce = NO;
    [UIView animateWithDuration:(self.actionAnimationOut / 2) delay:self.actionDelayOut options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [self.layer removeAllAnimations];
    }];
}
 
@end