-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMHMainViewController.m
104 lines (79 loc) · 2.78 KB
/
MHMainViewController.m
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
//
// Created by Florian on 02/05/14.
//
// To change the template use AppCode | Preferences | File Templates.
//
#import "MHMainViewController.h"
#import "MHPaneBehavior.h"
#import "MHCardView.h"
CGPoint CGRectCenter(CGRect rect){
return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
}
@interface MHMainViewController () <MHCardViewDelegate>
@property (nonatomic, readwrite) MHPaneState paneState;
@property (nonatomic) UIDynamicAnimator *animator;
@property (nonatomic, strong) MHPaneBehavior *paneBehavior;
-(void)togglePaneState;
@end
@implementation MHMainViewController
-(void)viewDidLoad{
[super viewDidLoad];
[self setup];
}
- (void)setup
{
self.paneState = MHPaneStateClosed;
self.pane.delegate = self;
self.cardClosedFrame = self.view.frame;
self.cardOpenFrame = self.view.superview.bounds;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(togglePaneState)];
doubleTap.numberOfTapsRequired = 2;
[self.pane addGestureRecognizer:doubleTap];
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
__weak typeof(self) me = self;
self.paneBehavior.action = ^{
[[NSNotificationCenter defaultCenter] postNotificationName:MHCardDidDragNotificationName object:me];
};
}
- (void)animatePaneWithInitialVelocity:(CGPoint)initialVelocity
{
if (!self.paneBehavior) {
MHPaneBehavior *behavior = [[MHPaneBehavior alloc] initWithItem:self.pane];
self.paneBehavior = behavior;
}
self.paneBehavior.targetPoint = self.targetPoint;
self.paneBehavior.velocity = initialVelocity;
[self.animator addBehavior:self.paneBehavior];
}
- (CGPoint)targetPoint
{
return self.paneState == MHPaneStateClosed ? CGRectCenter(self.cardClosedFrame) : CGRectCenter(self.cardOpenFrame);
}
-(void)setPaneState:(MHPaneState)state withInitialVelocity:(CGPoint)velocity{
self.paneState = state;
[self animatePaneWithInitialVelocity:velocity];
}
#pragma mark MHCardViewDelegate
- (void)cardView:(MHCardView *)view draggingEndedWithVelocity:(CGPoint)velocity lastTouchLocationInSuperview:(CGPoint)touch
{
MHPaneState targetState;
if(velocity.y > 0){
targetState = MHPaneStateClosed;
}
else if(velocity.y < 0){
targetState = MHPaneStateOpen;
}
else{
targetState = touch.y >= self.view.frame.size.height / 8 ? MHPaneStateClosed : MHPaneStateOpen;
}
[self setPaneState:targetState withInitialVelocity:velocity];
}
- (void)MHCardViewBeganDragging:(MHCardView *)view
{
[self.animator removeAllBehaviors];
}
#pragma mark Actions
-(void)togglePaneState{
[self setPaneState:self.paneState == MHPaneStateOpen ? MHPaneStateClosed : MHPaneStateOpen withInitialVelocity:self.paneBehavior.velocity];
}
@end