forked from sketch-hq/BCCollectionView
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BCCollectionView+Zoom.m
76 lines (57 loc) · 2.05 KB
/
BCCollectionView+Zoom.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
// Created by Pieter Omvlee on 02/02/2011.
// Copyright 2011 Bohemian Coding. All rights reserved.
#import "BCCollectionView+Zoom.h"
#import "BCCollectionViewLayoutManager.h"
@interface BCCollectionView ()
- (void)removeInvisibleViewControllers;
- (void)addMissingViewControllersToView;
@end
@implementation BCCollectionView (BCCollectionView_Zoom)
CGFloat lastPinchMagnification;
- (void)registerForZoomValueChangesInDefaultsForKey:(NSString *)key
{
self.zoomValueObserverKey = key;
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:key options:0 context:NULL];
}
- (void)zoomValueDidChange
{
[self softReloadDataWithCompletionBlock:^{
if ([delegate respondsToSelector:@selector(collectionViewDidZoom:)])
[delegate collectionViewDidZoom:self];
}];
}
- (void)beginGestureWithEvent:(NSEvent *)event
{
[self setAcceptsTouchEvents:YES];
}
- (void)endGestureWithEvent:(NSEvent *)event
{
[self setAcceptsTouchEvents:NO];
}
- (void)touchesMovedWithEvent:(NSEvent *)event
{
if (!zoomValueObserverKey)
return;
if (lastPinchMagnification != 0.0)
[self magnifyWithEvent:event];
}
- (void)magnifyWithEvent:(NSEvent *)event
{
if (!zoomValueObserverKey)
return;
CGFloat magnification = [event type] == NSEventTypeMagnify ? [event magnification] : lastPinchMagnification;
CGFloat zoomValue = [[NSUserDefaults standardUserDefaults] floatForKey:zoomValueObserverKey];
NSRange scalingRange = [delegate validScalingRangeForCollectionView:self];
float magnificationVelocity = [delegate magnificationVelocityForCollectionView:self];
zoomValue = zoomValue + (magnification * magnificationVelocity);
zoomValue = MAX(MIN(zoomValue, scalingRange.location + scalingRange.length), scalingRange.location);
[[NSUserDefaults standardUserDefaults] setFloat:zoomValue forKey:zoomValueObserverKey];
[self zoomValueDidChange];
[[[self enclosingScrollView] contentView] autoscroll:event];
lastPinchMagnification = magnification;
}
- (void)touchesEndedWithEvent:(NSEvent *)event
{
lastPinchMagnification = 0.0;
}
@end