From daf07a0459a73032f4f4295d8a3ce812ecaf9ad4 Mon Sep 17 00:00:00 2001 From: makowildcat Date: Thu, 10 Apr 2014 12:45:03 +0200 Subject: [PATCH] Added animation. --- Flikrkr/PictureViewController.m | 2 +- Flikrkr/ReaderView.h | 2 +- Flikrkr/ReaderView.m | 47 ++++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Flikrkr/PictureViewController.m b/Flikrkr/PictureViewController.m index c86c994..07c2cbf 100644 --- a/Flikrkr/PictureViewController.m +++ b/Flikrkr/PictureViewController.m @@ -35,7 +35,7 @@ - (void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; - [self.readerView displayPageAtIndex:0]; + [self.readerView displayPageAtIndex:0 animated:NO]; } - (int) numberOfPages { diff --git a/Flikrkr/ReaderView.h b/Flikrkr/ReaderView.h index bca40ae..4d91dff 100644 --- a/Flikrkr/ReaderView.h +++ b/Flikrkr/ReaderView.h @@ -19,6 +19,6 @@ @property (nonatomic, weak) id delegate; -- (void) displayPageAtIndex:(int)index; +- (void) displayPageAtIndex:(int)index animated:(BOOL) animated; @end \ No newline at end of file diff --git a/Flikrkr/ReaderView.m b/Flikrkr/ReaderView.m index f83da92..aefe138 100644 --- a/Flikrkr/ReaderView.m +++ b/Flikrkr/ReaderView.m @@ -33,12 +33,47 @@ // Drawing code } */ --(void)displayPageAtIndex:(int)index +-(void)displayPageAtIndex:(int)index animated:(BOOL)animated { if (index >= 0 && index < [self.delegate numberOfPages]) { - [self.subviews.lastObject removeFromSuperview]; - UIView * view = [self.delegate pageAtIndex:index]; - [self addSubview:view]; + if (!animated) { + [self.subviews.lastObject removeFromSuperview]; + UIView * view = [self.delegate pageAtIndex:index]; + [self addSubview:view]; + } + else { + UIView * oldView = [self.subviews lastObject]; + UIView * newView = [self.delegate pageAtIndex:index]; + [self addSubview:newView]; + CGPoint center = newView.center; + CGPoint left = CGPointMake(center.x - self.bounds.size.width, center.y); + CGPoint right = CGPointMake(center.x + self.bounds.size.width, center.y); + if (index < self.currentIndex) { + // page précédente + newView.center = left; + [UIView animateWithDuration:.3 + animations:^{ + newView.center = center; + oldView.center = right; + } + completion:^(BOOL finished) { + [oldView removeFromSuperview]; + }]; + } + else { + // page suivante + newView.center = right; + [UIView animateWithDuration:.3 + animations:^{ + newView.center = center; + oldView.center = left; + } + completion:^(BOOL finished) { + [oldView removeFromSuperview]; + }]; + + } + } self.currentIndex = index; } } @@ -61,10 +96,10 @@ - (void) previousPage { - [self displayPageAtIndex:self.currentIndex-1]; + [self displayPageAtIndex:self.currentIndex-1 animated:YES]; } - (void) nextPage { - [self displayPageAtIndex:self.currentIndex+1]; + [self displayPageAtIndex:self.currentIndex+1 animated:YES]; } @end