Skip to content

Commit

Permalink
tap or drag to turn page forward or backward.
Browse files Browse the repository at this point in the history
  • Loading branch information
brow committed Apr 20, 2010
1 parent 7c69562 commit e8d0ac1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Classes/LeavesView.h
Expand Up @@ -30,6 +30,10 @@
NSUInteger numberOfPages;
id<LeavesViewDataSource> dataSource;
id<LeavesViewDelegate> delegate;

CGPoint touchBeganPoint;
BOOL touchIsActive;
CGRect nextPageRect, prevPageRect;
}

@property (assign) id<LeavesViewDataSource> dataSource;
Expand Down
72 changes: 69 additions & 3 deletions Classes/LeavesView.m
Expand Up @@ -8,13 +8,16 @@

#import "LeavesView.h"

#define DRAG_THRESHOLD 2.5

@interface LeavesView ()

@property (assign) CGFloat leafEdge;
@property (assign) NSUInteger currentPageIndex;

@end

CGFloat distance(CGPoint a, CGPoint b);

@implementation LeavesView

Expand Down Expand Up @@ -153,10 +156,31 @@ - (void) setLayerFrames {
bottomPage.bounds.size.height);
}

- (void) prevPage {
self.currentPageIndex = self.currentPageIndex - 1;
}

- (void) nextPage {
self.currentPageIndex = self.currentPageIndex + 1;
}

- (BOOL) hasPrevPage {
return self.currentPageIndex > 0;
}

- (BOOL) hasNextPage {
return self.currentPageIndex < numberOfPages - 1;
}

- (BOOL) touchedNextPage {
return CGRectContainsPoint(nextPageRect, touchBeganPoint);
}

- (BOOL) touchedPrevPage {
return CGRectContainsPoint(prevPageRect, touchBeganPoint);
}


#pragma mark properties

- (void) setLeafEdge:(CGFloat)aLeafEdge {
Expand Down Expand Up @@ -191,7 +215,29 @@ - (void) setCurrentPageIndex:(NSUInteger)aCurrentPageIndex {

#pragma mark UIView methods

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [event.allTouches anyObject];
touchBeganPoint = [touch locationInView:self];

if ([self touchedPrevPage] && [self hasPrevPage]) {
[self prevPage];
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
self.leafEdge = 0.0;
[CATransaction commit];
touchIsActive = YES;
}
else if ([self touchedNextPage] && [self hasNextPage])
touchIsActive = YES;

else
touchIsActive = NO;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (!touchIsActive)
return;
UITouch *touch = [event.allTouches anyObject];
CGPoint touchPoint = [touch locationInView:self];

Expand All @@ -204,19 +250,24 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (!touchIsActive)
return;
UITouch *touch = [event.allTouches anyObject];
CGPoint touchPoint = [touch locationInView:self];
BOOL dragged = distance(touchPoint, touchBeganPoint) > DRAG_THRESHOLD;

[CATransaction begin];
float duration;
if (self.leafEdge < 0.5) {
if ((dragged && self.leafEdge < 0.5) || (!dragged && [self touchedNextPage])) {
self.leafEdge = 0;
duration = leafEdge;
[self performSelector:@selector(nextPage)
withObject:nil
afterDelay:duration+0.2];
afterDelay:duration + 0.2];
}
else {
self.leafEdge = 1.0;
duration = 1 - leafEdge;

}
[CATransaction setValue:[NSNumber numberWithFloat:duration]
forKey:kCATransactionAnimationDuration];
Expand All @@ -231,6 +282,21 @@ - (void) layoutSubviews {
forKey:kCATransactionDisableActions];
[self setLayerFrames];
[CATransaction commit];


CGFloat touchRectsWidth = self.bounds.size.width / 7;
nextPageRect = CGRectMake(self.bounds.size.width - touchRectsWidth,
0,
touchRectsWidth,
self.bounds.size.height);
prevPageRect = CGRectMake(0,
0,
touchRectsWidth,
self.bounds.size.height);
}

@end

CGFloat distance(CGPoint a, CGPoint b) {
return sqrtf(powf(a.x-b.x, 2) + powf(a.y-b.y, 2));
}

0 comments on commit e8d0ac1

Please sign in to comment.