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
//
// CheckoutSelectionView.m
// Checkout
//
// Created by Shopify.
// Copyright (c) 2015 Shopify. All rights reserved.
//
#import "CheckoutSelectionView.h"
//Views
#import "PAYButton.h"
#define kSidePadding 15.0f
#define kButtonIconPadding 8.0f
#define kPaymentButtonHeight 70.0f
#define kAnimationOffset 80.0f
#define kDelay 0.10f
@implementation CheckoutSelectionView {
UIView *_background;
NSArray *_buttons;
}
- (instancetype)initWithFrame:(CGRect)frame buttons:(NSArray *)buttons
{
self = [super initWithFrame:frame];
if (self) {
_background = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
_background.alpha = 0.0f;
_background.frame = self.bounds;
_background.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self addSubview:_background];
_buttons = buttons;
for (UIButton *button in _buttons) {
[_background addSubview:button];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
}
}
return self;
}
- (CGSize)sizeThatFits:(CGSize)size
{
NSUInteger buttonCount = [_buttons count];
return CGSizeMake(size.width, kPaymentButtonHeight * buttonCount + kSidePadding * (buttonCount + 1));
}
- (void)layoutButtonsAsHidden
{
CGRect bounds = _background.bounds;
CGFloat offsetX = bounds.origin.x + roundf(bounds.size.width / 2) + kSidePadding;
CGFloat offsetY = bounds.origin.y + bounds.size.height - [_buttons count] * (kPaymentButtonHeight + kSidePadding);
CGFloat width = bounds.size.width - 2 * kSidePadding;
for (UIButton *button in _buttons) {
button.frame = CGRectMake(offsetX, offsetY, width, kPaymentButtonHeight);
button.alpha = 0.0f;
offsetY += kPaymentButtonHeight + kSidePadding;
}
}
- (void)showButtons:(CGFloat)animationDuration completion:(void (^)(BOOL complete))completion
{
[self layoutButtonsAsHidden];
[UIView animateWithDuration:animationDuration delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
_background.alpha = 1.0f;
} completion:nil];
[self animateButtonsToOffset:0.0f duration:animationDuration alpha:1.0f completion:completion];
}
- (void)hideButtons:(CGFloat)animationDuration completion:(void (^)(BOOL complete))completion
{
[UIView animateWithDuration:animationDuration delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
_background.alpha = 0.0f;
} completion:nil];
[self animateButtonsToOffset:kAnimationOffset duration:animationDuration alpha:0.0f completion:completion];
}
- (void)animateButtonsToOffset:(CGFloat)offset duration:(CGFloat)duration alpha:(CGFloat)alpha completion:(void (^)(BOOL complete))completion
{
CGRect bounds = self.bounds;
CGSize size = [self sizeThatFits:bounds.size];
CGFloat offsetY = bounds.origin.y + bounds.size.height - size.height;
CGFloat offsetX = bounds.origin.x + kSidePadding + offset;
CGFloat delay = 0.0f;
for (UIButton *button in _buttons) {
//Animate each button independently and call the completion handler when the last button has finished animating
[UIView animateWithDuration:duration delay:delay usingSpringWithDamping:0.8f initialSpringVelocity:0.2f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
button.frame = CGRectMake(offsetX, offsetY, button.frame.size.width, button.frame.size.height);
button.alpha = alpha;
} completion:button == [_buttons lastObject] ? completion : nil];
offsetY += kPaymentButtonHeight + kSidePadding;
delay += kDelay;
}
}
@end