JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-iphone
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#import "UIBouncingView.h"
 
#import "CAAnimation+Blocks.h"
#import "Utils.h"
 
static NSString *const kBounceAnimation = @"bounce";
static NSString *const kAppearAnimation = @"appear";
static NSString *const kDisappearAnimation = @"disappear";
 
@implementation UIBouncingView
 
INIT_WITH_COMMON_CF {
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(settingsUpdate:)
                                               name:kLinphoneSettingsUpdate
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(applicationWillEnterForeground:)
                                               name:UIApplicationWillEnterForegroundNotification
                                             object:nil];
    return self;
}
 
- (void)dealloc {
    [NSNotificationCenter.defaultCenter removeObserver:self];
}
 
- (void)settingsUpdate:(NSNotification *)notif {
    if (ANIMATED == false) {
        [self stopAnimating:NO];
    } else {
        if (![self isHidden]) {
            self.hidden = YES;
            [self startAnimating:YES];
        }
    }
}
 
- (void)applicationWillEnterForeground:(NSNotification *)notif {
    // Force the animations
    if (self.isHidden) {
        self.hidden = NO;
        [self stopAnimating:NO];
    } else {
        self.hidden = YES;
        [self startAnimating:NO];
    }
}
 
#pragma mark - Animation
 
- (void)appearAnimation:(NSString *)animationID target:(UIView *)target completion:(void (^)(BOOL finished))completion {
    CABasicAnimation *appear = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    appear.duration = 0.4;
    appear.fromValue = [NSNumber numberWithDouble:0.0f];
    appear.toValue = [NSNumber numberWithDouble:1.0f];
    appear.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    appear.fillMode = kCAFillModeForwards;
    appear.removedOnCompletion = NO;
    [appear setCompletion:completion];
    [target.layer addAnimation:appear forKey:animationID];
}
 
- (void)disappearAnimation:(NSString *)animationID
                    target:(UIView *)target
                completion:(void (^)(BOOL finished))completion {
    CABasicAnimation *disappear = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    disappear.duration = 0.4;
    disappear.fromValue = [NSNumber numberWithDouble:1.0f];
    disappear.toValue = [NSNumber numberWithDouble:0.0f];
    disappear.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    disappear.fillMode = kCAFillModeForwards;
    disappear.removedOnCompletion = NO;
    [disappear setCompletion:completion];
    [target.layer addAnimation:disappear forKey:animationID];
}
 
- (void)startBounceAnimation:(NSString *)animationID target:(UIView *)target {
    CABasicAnimation *bounce = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    bounce.duration = 0.3;
    bounce.fromValue = [NSNumber numberWithDouble:0.0f];
    bounce.toValue = [NSNumber numberWithDouble:8.0f];
    bounce.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    bounce.autoreverses = TRUE;
    bounce.repeatCount = HUGE_VALF;
    [target.layer addAnimation:bounce forKey:animationID];
}
 
- (void)stopBounceAnimation:(NSString *)animationID target:(UIView *)target {
    [target.layer removeAnimationForKey:animationID];
}
 
- (void)startAnimating:(BOOL)animated {
    animated = NO;
    if (!self.hidden) {
        return;
    }
 
    [self setHidden:FALSE];
    if (ANIMATED) {
        if (animated) {
            [self appearAnimation:kAppearAnimation
                           target:self
                       completion:^(BOOL finished) {
                         [self startBounceAnimation:kBounceAnimation target:self];
                         if (finished) {
                             [self.layer removeAnimationForKey:kAppearAnimation];
                         }
                       }];
        } else {
            [self startBounceAnimation:kBounceAnimation target:self];
        }
    }
}
 
- (void)stopAnimating:(BOOL)animated {
    animated = NO;
    if (self.hidden) {
        return;
    }
 
    [self stopBounceAnimation:kBounceAnimation target:self];
    if (animated) {
        [self disappearAnimation:kDisappearAnimation
                          target:self
                      completion:^(BOOL finished) {
                        [self setHidden:TRUE];
                        if (finished) {
                            [self.layer removeAnimationForKey:kDisappearAnimation];
                        }
                      }];
    } else {
        [self setHidden:TRUE];
    }
}
@end