-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMHPaneBehavior.m
64 lines (51 loc) · 1.79 KB
/
MHPaneBehavior.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
//
// Created by Florian on 02/05/14.
//
// To change the template use AppCode | Preferences | File Templates.
//
#import "MHPaneBehavior.h"
#import "MHCardView.h"
@interface MHPaneBehavior ()
@property (nonatomic, strong) UIAttachmentBehavior *attachmentBehavior;
@property (nonatomic, strong) UIDynamicItemBehavior *itemBehavior;
@property (nonatomic, strong) id<UIDynamicItem> item;
@end
@implementation MHPaneBehavior
-(instancetype)initWithItem:(id <UIDynamicItem>)item{
self = [super init];
if (self) {
self.item = item;
[self setup];
}
return self;
}
-(void)setup{
UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.item attachedToAnchor:CGPointZero];
attachmentBehavior.frequency = 3.5;
attachmentBehavior.damping = .4;
attachmentBehavior.length = 0;
[self addChildBehavior:attachmentBehavior];
self.attachmentBehavior = attachmentBehavior;
UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.item]];
itemBehavior.density = 100;
itemBehavior.resistance = 10;
[self addChildBehavior:itemBehavior];
self.itemBehavior = itemBehavior;
}
-(void)setTargetPoint:(CGPoint)targetPoint{
_targetPoint = targetPoint;
self.attachmentBehavior.anchorPoint = targetPoint;
}
- (void)setVelocity:(CGPoint)velocity
{
_velocity = velocity;
CGPoint currentVelocity = [self.itemBehavior linearVelocityForItem:self.item];
CGPoint velocityDelta = CGPointMake(velocity.x - currentVelocity.x, velocity.y - currentVelocity.y);
[self.itemBehavior addLinearVelocity:velocityDelta forItem:self.item];
}
-(void)setAction:(void (^)(void))action{
[super setAction:action];
self.attachmentBehavior.action = action;
self.itemBehavior.action = action;
}
@end