1 #import "ScrollView.h"
2 #import @implementation ScrollView
3 + (Class)layerClass
4 {
5 return [CAScrollLayer class];
6 }
7
8 - (void)setUp
9 {
10 //enable clipping
11 self.layer.masksToBounds = YES;
12
13 //attach pan gesture recognizer
14 UIPanGestureRecognizer *recognizer = nil;
15 recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
16 [self addGestureRecognizer:recognizer];
17 }
18
19 - (id)initWithFrame:(CGRect)frame
20 {
21 //this is called when view is created in code
22 if ((self = [super initWithFrame:frame])) {
23 [self setUp];
24 }
25 return self;
26 }
27
28 - (void)awakeFromNib {
29 //this is called when view is created from a nib
30 [self setUp];
31 }
32
33 - (void)pan:(UIPanGestureRecognizer *)recognizer
34 {
35 //get the offset by subtracting the pan gesture
36 //translation from the current bounds origin
37 CGPoint offset = self.bounds.origin;
38 offset.x -= [recognizer translationInView:self].x;
39 offset.y -= [recognizer translationInView:self].y;
40
41 //scroll the layer
42 [(CAScrollLayer *)self.layer scrollToPoint:offset];
43
44 //reset the pan gesture translation
45 [recognizer setTranslation:CGPointZero inView:self];
46 }
47 @end