Skip to content

Commit

Permalink
Merge pull request #21 from krzak/master
Browse files Browse the repository at this point in the history
expose more parameters and allow a custom number of gestures for the sliding.
  • Loading branch information
steipete committed Feb 27, 2012
2 parents 822c69c + fb0fecd commit 1dcbed1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 25 deletions.
9 changes: 9 additions & 0 deletions PSStackedView/PSSVContainerView.h
Expand Up @@ -49,4 +49,13 @@ enum {
/// darken down the view if it's not fully visible
@property(nonatomic, assign) CGFloat darkRatio;

/// shadow width
@property(nonatomic, assign) CGFloat shadowWidth;

/// shadow alpha
@property(nonatomic, assign) CGFloat shadowAlpha;

/// corner radius
@property(nonatomic, assign) CGFloat cornerRadius;

@end
20 changes: 9 additions & 11 deletions PSStackedView/PSSVContainerView.m
Expand Up @@ -10,10 +10,6 @@
#import "PSStackedViewGlobal.h"
#import "UIView+PSSizes.h"

#define kPSSVCornerRadius 6.f
#define kPSSVShadowWidth 60.f
#define kPSSVShadowAlpha 0.2f

@interface PSSVContainerView ()
@property(nonatomic, assign) CGFloat originalWidth;
@property(nonatomic, strong) CAGradientLayer *leftShadowLayer;
Expand All @@ -31,7 +27,9 @@ @implementation PSSVContainerView
@synthesize innerShadowLayer = innerShadowLayer_;
@synthesize rightShadowLayer = rightShadowLayer_;
@synthesize transparentView = transparentView_;

@synthesize shadowWidth = shadowWidth_;
@synthesize shadowAlpha = shadowAlpha_;
@synthesize cornerRadius = cornerRadius_;
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark private
Expand All @@ -41,7 +39,7 @@ - (CAGradientLayer *)shadowAsInverse:(BOOL)inverse {
CAGradientLayer *newShadow = [[CAGradientLayer alloc] init];
newShadow.startPoint = CGPointMake(0, 0.5);
newShadow.endPoint = CGPointMake(1.0, 0.5);
CGColorRef darkColor = (CGColorRef)CFRetain([UIColor colorWithWhite:0.0f alpha:kPSSVShadowAlpha].CGColor);
CGColorRef darkColor = (CGColorRef)CFRetain([UIColor colorWithWhite:0.0f alpha:shadowAlpha_].CGColor);
CGColorRef lightColor = (CGColorRef)CFRetain([UIColor clearColor].CGColor);
newShadow.colors = [NSArray arrayWithObjects:
(__bridge id)(inverse ? lightColor : darkColor),
Expand Down Expand Up @@ -147,7 +145,7 @@ - (void)addMaskToCorners:(UIRectCorner)corners; {
// Create the path (with only the top-left corner rounded)
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:frame
byRoundingCorners:corners
cornerRadii:CGSizeMake(kPSSVCornerRadius, kPSSVCornerRadius)];
cornerRadii:CGSizeMake(cornerRadius_, cornerRadius_)];

// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
Expand Down Expand Up @@ -175,7 +173,7 @@ - (void)setShadow:(PSSVSide)shadow {
CAGradientLayer *leftShadow = [self shadowAsInverse:YES];
self.leftShadowLayer = leftShadow;
}
self.leftShadowLayer.frame = CGRectMake(-kPSSVShadowWidth, 0, kPSSVShadowWidth+kPSSVCornerRadius, self.controller.view.height);;
self.leftShadowLayer.frame = CGRectMake(-shadowWidth_, 0, shadowWidth_+cornerRadius_, self.controller.view.height);;
if ([self.layer.sublayers indexOfObjectIdenticalTo:self.leftShadowLayer] != 0) {
[self.layer insertSublayer:self.leftShadowLayer atIndex:0];
}
Expand All @@ -188,7 +186,7 @@ - (void)setShadow:(PSSVSide)shadow {
CAGradientLayer *rightShadow = [self shadowAsInverse:NO];
self.rightShadowLayer = rightShadow;
}
self.rightShadowLayer.frame = CGRectMake(self.width-kPSSVCornerRadius, 0, kPSSVShadowWidth, self.controller.view.height);
self.rightShadowLayer.frame = CGRectMake(self.width-cornerRadius_, 0, shadowWidth_, self.controller.view.height);
if ([self.layer.sublayers indexOfObjectIdenticalTo:self.rightShadowLayer] != 0) {
[self.layer insertSublayer:self.rightShadowLayer atIndex:0];
}
Expand All @@ -199,10 +197,10 @@ - (void)setShadow:(PSSVSide)shadow {
if (shadow) {
if (!self.innerShadowLayer) {
CAGradientLayer *innerShadow = [[CAGradientLayer alloc] init];
innerShadow.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:0.0f alpha:kPSSVShadowAlpha].CGColor, (id)[UIColor colorWithWhite:0.0f alpha:kPSSVShadowAlpha].CGColor, nil];
innerShadow.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:0.0f alpha:shadowAlpha_].CGColor, (id)[UIColor colorWithWhite:0.0f alpha:shadowAlpha_].CGColor, nil];
self.innerShadowLayer = innerShadow;
}
self.innerShadowLayer.frame = CGRectMake(kPSSVCornerRadius, 0, self.width-kPSSVCornerRadius*2, self.controller.view.height);
self.innerShadowLayer.frame = CGRectMake(cornerRadius_, 0, self.width-cornerRadius_*2, self.controller.view.height);
if ([self.layer.sublayers indexOfObjectIdenticalTo:self.innerShadowLayer] != 0) {
[self.layer insertSublayer:self.innerShadowLayer atIndex:0];
}
Expand Down
17 changes: 17 additions & 0 deletions PSStackedView/PSStackedViewController.h
Expand Up @@ -121,6 +121,21 @@ enum {
/// Property to disable dragging past left insets, recommend setting enableBouces to NO when this is set to NO
@property(nonatomic, assign) BOOL enableDraggingPastInsets;

/// enable scaling while fade in/out
@property(nonatomic, assign) BOOL enableScalingFadeInOut;

/// shadow width
@property(nonatomic, assign) CGFloat defaultShadowWidth;

/// shadow alpha
@property(nonatomic, assign) CGFloat defaultShadowAlpha;

/// corner radius
@property(nonatomic, assign) CGFloat cornerRadius;

///number of touches
@property(nonatomic, assign) NSUInteger numberOfTouches;

/// left inset thats always visible. Defaults to 60.
@property(nonatomic, assign) NSUInteger leftInset;
/// animate setting of the left inset that is always visible
Expand All @@ -131,6 +146,8 @@ enum {
/// animate setting of large left inset
- (void)setLargeLeftInset:(NSUInteger)largeLeftInset animated:(BOOL)animated;



// compatibility with UINavigationBar -- returns nil
#ifdef ALLOW_SWIZZLING_NAVIGATIONCONTROLLER
@property(nonatomic, assign) UINavigationBar *navigationBar;
Expand Down
60 changes: 46 additions & 14 deletions PSStackedView/PSStackedViewController.m
Expand Up @@ -64,6 +64,11 @@ @implementation PSStackedViewController
@synthesize enableBounces = enableBounces_;
@synthesize enableShadows = enableShadows_;
@synthesize enableDraggingPastInsets = enableDraggingPastInsets_;
@synthesize enableScalingFadeInOut = enableScalingFadeInOut_;
@synthesize defaultShadowWidth = defaultShadowWidth_;
@synthesize defaultShadowAlpha = defaultShadowAlpha_;
@synthesize cornerRadius = cornerRadius_;
@synthesize numberOfTouches = numberOfTouches_;
@dynamic firstVisibleIndex;

#ifdef ALLOW_SWIZZLING_NAVIGATIONCONTROLLER
Expand All @@ -73,6 +78,26 @@ @implementation PSStackedViewController
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - NSObject

- (void)configureGestureRecognizer
{
[self.view removeGestureRecognizer:self.panRecognizer];

// add a gesture recognizer to detect dragging to the guest controllers
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
if (numberOfTouches_ > 0)
{
[panRecognizer setMinimumNumberOfTouches:numberOfTouches_];
} else {
[panRecognizer setMaximumNumberOfTouches:1];
}
[panRecognizer setDelaysTouchesBegan:NO];
[panRecognizer setDelaysTouchesEnded:YES];
[panRecognizer setCancelsTouchesInView:YES];
panRecognizer.delegate = self;
[self.view addGestureRecognizer:panRecognizer];
self.panRecognizer = panRecognizer;
}

- (id)initWithRootViewController:(UIViewController *)rootViewController; {
if ((self = [super init])) {
rootViewController_ = rootViewController;
Expand All @@ -84,20 +109,16 @@ - (id)initWithRootViewController:(UIViewController *)rootViewController; {
leftInset_ = 60;
largeLeftInset_ = 200;

// add a gesture recognizer to detect dragging to the guest controllers
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelaysTouchesBegan:NO];
[panRecognizer setDelaysTouchesEnded:YES];
[panRecognizer setCancelsTouchesInView:YES];
panRecognizer.delegate = self;
[self.view addGestureRecognizer:panRecognizer];
self.panRecognizer = panRecognizer;
[self configureGestureRecognizer];

enableBounces_ = YES;
enableShadows_ = YES;
enableDraggingPastInsets_ = YES;


enableScalingFadeInOut_ = YES;
defaultShadowWidth_ = 60.0f;
defaultShadowAlpha_ = 0.2f;
cornerRadius_ = 6.0f;

#ifdef ALLOW_SWIZZLING_NAVIGATIONCONTROLLER
PSSVLog("Swizzling UIViewController.navigationController");
Method origMethod = class_getInstanceMethod([UIViewController class], @selector(navigationController));
Expand Down Expand Up @@ -790,7 +811,7 @@ - (void)moveStackWithOffset:(NSInteger)offset animated:(BOOL)animated userDraggi
} completion:nil];
}

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
CGPoint translatedPoint = [recognizer translationInView:self.view];
UIGestureRecognizerState state = recognizer.state;

Expand Down Expand Up @@ -939,6 +960,9 @@ - (void)pushViewController:(UIViewController *)viewController fromViewController
container.left = leftGap;
container.width = viewController.view.width;
container.autoresizingMask = UIViewAutoresizingFlexibleHeight; // width is not flexible!
container.shadowWidth = defaultShadowWidth_;
container.shadowAlpha = defaultShadowAlpha_;
container.cornerRadius = cornerRadius_;
[container limitToMaxWidth:[self maxControllerWidth]];
PSSVLog(@"container frame: %@", NSStringFromCGRect(container.frame));

Expand All @@ -947,7 +971,8 @@ - (void)pushViewController:(UIViewController *)viewController fromViewController

if (animated) {
container.alpha = 0.f;
container.transform = CGAffineTransformMakeScale(1.2, 1.2); // large but fade in
if (enableScalingFadeInOut_)
container.transform = CGAffineTransformMakeScale(1.2, 1.2); // large but fade in
}

[self.view addSubview:container];
Expand Down Expand Up @@ -1002,7 +1027,8 @@ - (UIViewController *)popViewControllerAnimated:(BOOL)animated; {
if (animated) { // kPSSVStackAnimationDuration
[UIView animateWithDuration:kPSSVStackAnimationPopDuration delay:0.f options:UIViewAnimationOptionBeginFromCurrentState animations:^(void) {
lastController.containerView.alpha = 0.f;
lastController.containerView.transform = CGAffineTransformMakeScale(0.8, 0.8); // make smaller while fading out
if (enableScalingFadeInOut_)
lastController.containerView.transform = CGAffineTransformMakeScale(0.8, 0.8); // make smaller while fading out
} completion:^(BOOL finished) {
// even with duration = 0, this doesn't fire instantly but on a future runloop with NSFireDelayedPerform, thus ugly double-check
if (finished) {
Expand Down Expand Up @@ -1346,6 +1372,12 @@ - (NSUInteger)expandStack:(NSInteger)steps animated:(BOOL)animated; { // (---> d
return steps;
}

- (void)setNumberOfTouches:(NSUInteger)numberOfTouches
{
numberOfTouches_ = numberOfTouches;
[self configureGestureRecognizer];
}

- (void)setLeftInset:(NSUInteger)leftInset {
[self setLeftInset:leftInset animated:NO];
}
Expand Down

0 comments on commit 1dcbed1

Please sign in to comment.