Permalink
Browse files

Added an mirrored left MultiFoldView

  • Loading branch information...
1 parent 83e9153 commit 2d1ceae765944cc2fba3d67724589b74f196dbaf Łukasz Przytuła committed Nov 17, 2012
@@ -31,7 +31,7 @@
*
*/
-
+@class MKMapView;
#import <UIKit/UIKit.h>
#import "FoldView.h"
@@ -47,7 +47,7 @@
@property (assign, nonatomic) UIView *contentView;
// init with the number of folds and pull factor
-- (id)initWithFrame:(CGRect)frame folds:(int)folds pullFactor:(float)pullFactor;
+- (id)initWithFrame:(CGRect)frame folds:(int)folds pullFactor:(float)pullFactor forLeftview:(BOOL)leftView;
// set the content of the view
- (void)setContent:(UIView *)contentView;
// get screenshot of content to overlay in folds
@@ -62,13 +62,13 @@
// unfold using a fraction 0 to 1
// 0 when it's completely folded
// 1 when it's completely unfolded
-// fraction is calculated based on parent offset
+// fraction is calculated based on parent offset
- (void)unfoldViewToFraction:(CGFloat)fraction;
// unfold foldView using fraction
- (void)unfoldView:(FoldView*)foldView toFraction:(CGFloat)fraction;
-// show/hide all folds
+// show/hide all folds
- (void)showFolds:(BOOL)show;
#pragma mark states
@@ -34,6 +34,13 @@
#import "MultiFoldView.h"
#import "UIView+Screenshot.h"
+#import <MapKit/MapKit.h>
+
+@interface MultiFoldView() {
+ BOOL isLeft;
+}
+
+@end
@implementation MultiFoldView
@synthesize numberOfFolds;
@@ -43,10 +50,11 @@ @implementation MultiFoldView
#define FOLDVIEW_TAG 1000
-- (id)initWithFrame:(CGRect)frame folds:(int)folds pullFactor:(float)factor
+- (id)initWithFrame:(CGRect)frame folds:(int)folds pullFactor:(float)factor forLeftview:(BOOL)leftView
{
self = [super initWithFrame:frame];
if (self) {
+ isLeft = leftView;
self.numberOfFolds = folds;
if (self.numberOfFolds==1)
{
@@ -56,7 +64,7 @@ - (id)initWithFrame:(CGRect)frame folds:(int)folds pullFactor:(float)factor
else self.pullFactor = factor;
float foldWidth = frame.size.width/self.numberOfFolds;
-
+
// create multiple FoldView next to each other
for (int i=0; i<self.numberOfFolds; i++)
{
@@ -85,36 +93,46 @@ - (void)setContent:(UIView *)contentView
- (void)drawScreenshotOnFolds
{
-
+
UIImage *image = [self.contentView screenshot];
// get screenshot of content view, and splice the image to overlay in different folds
float foldWidth = image.size.width/self.numberOfFolds;
+
for (int i=0; i<self.numberOfFolds; i++)
{
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(foldWidth*i*image.scale, 0, foldWidth*image.scale, image.size.height*image.scale));
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CFRelease(imageRef);
- FoldView *foldView = (FoldView*)[self viewWithTag:FOLDVIEW_TAG+i];
+ FoldView *foldView;
+ if (isLeft) {
+ foldView = (FoldView*)[self viewWithTag:FOLDVIEW_TAG + (self.numberOfFolds - 1) - i];
+ } else {
+ foldView = (FoldView*)[self viewWithTag:FOLDVIEW_TAG+i];
+ }
[foldView setImage:croppedImage];
}
/*
- [self takeScreenshot:^(UIImage *image) {
- float foldWidth = image.size.width/self.numberOfFolds;
- for (int i=0; i<self.numberOfFolds; i++)
- {
- CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(foldWidth*i*image.scale, 0, foldWidth*image.scale, image.size.height*image.scale));
- UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
- CFRelease(imageRef);
- FoldView *foldView = (FoldView*)[self viewWithTag:FOLDVIEW_TAG+i];
- [foldView setImage:croppedImage];
- }
- }];*/
+ [self takeScreenshot:^(UIImage *image) {
+ float foldWidth = image.size.width/self.numberOfFolds;
+ for (int i=0; i<self.numberOfFolds; i++)
+ {
+ CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(foldWidth*i*image.scale, 0, foldWidth*image.scale, image.size.height*image.scale));
+ UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
+ CFRelease(imageRef);
+ FoldView *foldView = (FoldView*)[self viewWithTag:FOLDVIEW_TAG+i];
+ [foldView setImage:croppedImage];
+ }
+ }];*/
}
// set fold states based on offset value
- (void)calculateFoldStateFromOffset:(float)offset
{
- CGFloat fraction = -1*offset/self.frame.size.width;
+ CGFloat fraction;
+ if (offset < 0)
+ fraction = -1*offset/self.frame.size.width;
+ else
+ fraction = offset/self.frame.size.width;
if (_state==FoldStateClosed && fraction>0)
{
_state = FoldStateTransition;
@@ -144,50 +162,89 @@ - (void)calculateFoldStateFromOffset:(float)offset
// use the parent offset to calculate fraction
- (void)unfoldWithParentOffset:(float)offset
{
+ float leftOffset = offset;
[self calculateFoldStateFromOffset:offset];
float foldWidth = self.frame.size.width/self.numberOfFolds;
- if (offset<-1*(foldWidth+self.pullFactor*foldWidth))
- {
- offset = -1*(foldWidth+self.pullFactor*foldWidth);
+ CGFloat fraction;
+ if (offset < 0) {
+ if (offset<-1*(foldWidth+self.pullFactor*foldWidth))
+ {
+ offset = -1*(foldWidth+self.pullFactor*foldWidth);
+ }
+ fraction = offset /(-1*(foldWidth+self.pullFactor*foldWidth));
+ } else {
+ if (offset > (foldWidth+self.pullFactor*foldWidth))
+ {
+ offset = (foldWidth+self.pullFactor*foldWidth);
+ }
+ fraction = offset /(foldWidth+self.pullFactor*foldWidth);
}
- CGFloat fraction = offset /(-1*(foldWidth+self.pullFactor*foldWidth));
-
if (fraction < 0) fraction = 0;
if (fraction > 1) fraction = 1;
- [self unfoldViewToFraction:fraction];
+ if (isLeft) {
+ [self unfoldViewToFraction:fraction withOffset:leftOffset];
+ } else {
+ [self unfoldViewToFraction:fraction];
+ }
}
- (void)unfoldViewToFraction:(CGFloat)fraction
{
// start the cascading effect of unfolding
// with the first foldView with index FOLDVIEW_TAG+0
FoldView *firstFoldView = (FoldView*)[self viewWithTag:FOLDVIEW_TAG];
- [self unfoldView:firstFoldView toFraction:fraction];
+ [self unfoldView:firstFoldView toFraction:fraction withOffset:0];
}
-- (void)unfoldView:(FoldView*)foldView toFraction:(CGFloat)fraction
+- (void)unfoldViewToFraction:(CGFloat)fraction withOffset:(float)offset
+{
+ // start the cascading effect of unfolding
+ // with the first foldView with index FOLDVIEW_TAG+0
+ FoldView *firstFoldView = (FoldView*)[self viewWithTag:FOLDVIEW_TAG];
+ [self unfoldView:firstFoldView toFraction:fraction withOffset:offset];
+}
+
+- (void)unfoldView:(FoldView*)foldView toFraction:(CGFloat)fraction withOffset:(float)offset
{
// unfold the subfold
[foldView unfoldViewToFraction:fraction];
+ if (isLeft) {
+ [foldView setFrame:CGRectMake(offset - 2*foldView.rightView.frame.size.width, 0, foldView.frame.size.width, foldView.frame.size.height)];
+ }
+
// check if there is another subfold beside this fold
int index = [foldView tag] - FOLDVIEW_TAG;
if (index < self.numberOfFolds-1)
{
FoldView *nextFoldView = (FoldView*)[self viewWithTag:FOLDVIEW_TAG+index+1];
// set the origin of the next foldView
- [nextFoldView setFrame:CGRectMake(foldView.frame.origin.x + 2*foldView.leftView.frame.size.width,0,nextFoldView.frame.size.width,nextFoldView.frame.size.height)];
-
+ if (isLeft) {
+ [nextFoldView setFrame:CGRectMake(foldView.frame.origin.x - 2*nextFoldView.rightView.frame.size.width,0,nextFoldView.frame.size.width,nextFoldView.frame.size.height)];
+ } else {
+ [nextFoldView setFrame:CGRectMake(foldView.frame.origin.x + 2*foldView.leftView.frame.size.width,0,nextFoldView.frame.size.width,nextFoldView.frame.size.height)];
+ }
+
float foldWidth = self.frame.size.width/self.numberOfFolds;
// calculate the offset between the right edge of the last subfold, and the edge of the screen
// use this offset to readjust the fraction
- float x = self.superview.frame.origin.x+foldView.frame.origin.x+2*foldView.leftView.frame.size.width;
+ float x;
+ if (isLeft) {
+ // x = (foldView.frame.origin.x - 2 * foldView.leftView.frame.size.width);
+ x = (foldView.frame.origin.x + (fraction * foldView.frame.size.width)) - 2*foldView.rightView.frame.size.width;
+ } else{
+ x = self.superview.frame.origin.x+foldView.frame.origin.x+2*foldView.leftView.frame.size.width;
+ }
+
CGFloat adjustedFraction = 0;
+ if (isLeft) {
+ x = -x;
+ }
if (index+1==self.numberOfFolds-1)
{
- // if this is the last fold, do not use the pull factor
+ // if this is the last fold, do not use the pull factor
// so that the right edge of this subfold aligns with the right edge of the screen
adjustedFraction = (-1*x)/(foldWidth);
}
@@ -201,7 +258,7 @@ - (void)unfoldView:(FoldView*)foldView toFraction:(CGFloat)fraction
// unfold this foldView with the fraction
// by calling the same function
// this drills in to the next subfold in a cascading effect depending on the number of available folds
- [self unfoldView:nextFoldView toFraction:adjustedFraction];
+ [self unfoldView:nextFoldView toFraction:adjustedFraction withOffset:foldView.frame.origin.x];
}
}
@@ -234,6 +291,8 @@ - (void)foldDidClosed
// when fold is about to be opened, make sure content view is hidden, and show fold
- (void)foldWillOpen
{
+ [self.contentView setHidden:NO];
+ [self drawScreenshotOnFolds];
[self.contentView setHidden:YES];
[self showFolds:YES];
}
@@ -47,7 +47,7 @@ - (void)setLeftViewController:(UIViewController *)leftViewController width:(floa
_leftViewController = leftViewController;
[self.leftViewController.view setFrame:CGRectMake(0,0,width,[self.view bounds].size.height)];
- [self.paperFoldView setLeftFoldContentView:self.leftViewController.view];
+ [self.paperFoldView setLeftFoldContentView:self.leftViewController.view leftViewFoldCount:3 leftViewPullFactor:0.9];
}
- (void)paperFoldView:(id)paperFoldView didFoldAutomatically:(BOOL)automated toState:(PaperFoldState)paperFoldState
@@ -65,7 +65,7 @@ typedef void (^CompletionBlock)();
// manual animation with NSTimer is required to sync the offset of the contentView, with the folding of views
@property (nonatomic, strong) NSTimer *animationTimer;
// the fold view on the left
-@property (nonatomic, strong) FoldView *leftFoldView;
+@property (nonatomic, strong) MultiFoldView *leftFoldView;
// the multiple fold view on the right
@property (nonatomic, strong) MultiFoldView *rightFoldView;
// state of the current fold
@@ -87,7 +87,8 @@ typedef void (^CompletionBlock)();
// set the left fold content view
// and set the left fold container view frame
-- (void)setLeftFoldContentView:(UIView*)view;
+- (void)setLeftFoldContentView:(UIView*)view __attribute__ ((deprecated));
+- (void)setLeftFoldContentView:(UIView*)view leftViewFoldCount:(int)leftViewFoldCount leftViewPullFactor:(float)leftViewPullFactor;
- (void)setCenterContentView:(UIView*)view;
@@ -90,24 +90,30 @@ - (void)setCenterContentView:(UIView*)view
- (void)setLeftFoldContentView:(UIView*)view
{
+ [self setLeftFoldContentView:view leftViewFoldCount:1 leftViewPullFactor:0.9];
+}
+
+- (void)setLeftFoldContentView:(UIView*)view leftViewFoldCount:(int)leftViewFoldCount leftViewPullFactor:(float)leftViewPullFactor
+{
if (self.leftFoldView) [self.leftFoldView removeFromSuperview];
- self.leftFoldView = [[FoldView alloc] initWithFrame:CGRectMake(0,0,view.frame.size.width,self.frame.size.height)];
- [self.leftFoldView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
+ self.leftFoldView = [[MultiFoldView alloc] initWithFrame:CGRectMake(0,0,view.frame.size.width,self.frame.size.height) folds:leftViewFoldCount pullFactor:leftViewPullFactor forLeftview:YES];
+ [self.leftFoldView setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight];
[self insertSubview:self.leftFoldView belowSubview:self.contentView];
[self.leftFoldView setContent:view];
- [view setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
+ [view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
[self setPaperFoldState:PaperFoldStateDefault];
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(-1,0,1,self.frame.size.height)];
[line setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[self.contentView addSubview:line];
+ [line setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight];
[line setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:0.5]];
}
- (void)setRightFoldContentView:(UIView*)view rightViewFoldCount:(int)rightViewFoldCount rightViewPullFactor:(float)rightViewPullFactor
{
- self.rightFoldView = [[MultiFoldView alloc] initWithFrame:CGRectMake(self.frame.size.width,0,view.frame.size.width,self.frame.size.height) folds:rightViewFoldCount pullFactor:rightViewPullFactor];
+ self.rightFoldView = [[MultiFoldView alloc] initWithFrame:CGRectMake(self.frame.size.width,0,view.frame.size.width,self.frame.size.height) folds:rightViewFoldCount pullFactor:rightViewPullFactor forLeftview:NO];
[self.rightFoldView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight];
[self.contentView insertSubview:self.rightFoldView atIndex:0];
[self.rightFoldView setContent:view];
@@ -34,7 +34,7 @@
#import <UIKit/UIKit.h>
-typedef void (^CompletionBlock)(UIImage *image);
+//typedef void (^CompletionBlock)(UIImage *image);
@interface UIView (Screenshot)
- (UIImage*)screenshot;

0 comments on commit 2d1ceae

Please sign in to comment.