Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Lluch committed Jul 8, 2013
1 parent 5016863 commit ef2bcec
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 39 deletions.
19 changes: 13 additions & 6 deletions SWRevealViewController/SWRevealViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ typedef enum
// Toogles the current state of the front controller between Left or Right and fully visible
// Use setFrontViewPosition to set a particular position
- (void)revealToggleAnimated:(BOOL)animated;
- (void)rightRevealToggleAnimated:(BOOL)animated;
- (void)rightRevealToggleAnimated:(BOOL)animated; // <-- simetric implementation of the above for the rightViewController

// The following methods are meant to be directly connected to the action method of a button
// to perform user triggered postion change of the controller views. This is ussually added to a
// button on top left or right of the frontViewController
- (void)revealToggle:(id)sender;
- (void)rightRevealToggle:(id)sender;
- (void)rightRevealToggle:(id)sender; // <-- simetric implementation of the above for the rightViewController

// The following method will provide a panGestureRecognizer suitable to be added to any view on the frontController
// in order to perform usual drag and swipe gestures on the frontViewController to reveal the rear views. This
Expand All @@ -101,21 +101,25 @@ typedef enum
// The following properties are provided for further customization, they are set to default values on initialization,
// you should not generally have to set them

// Defines how much of the rear view is shown, default is 260.
// Defines how much of the rear or right view is shown, default is 260.
@property (assign, nonatomic) CGFloat rearViewRevealWidth;
@property (assign, nonatomic) CGFloat rightViewRevealWidth;
@property (assign, nonatomic) CGFloat rightViewRevealWidth; // <-- simetric implementation of the above for the rightViewController

// Defines how much of an overdraw can occur when dragging further than 'rearViewRevealWidth', default is 60.
@property (assign, nonatomic) CGFloat rearViewRevealOverdraw;
@property (assign, nonatomic) CGFloat rightViewRevealOverdraw;
@property (assign, nonatomic) CGFloat rightViewRevealOverdraw; // <-- simetric implementation of the above for the rightViewController

// Defines how much displacement is applied to the rear view when animating or dragging the front view, default is 40.
@property (assign, nonatomic) CGFloat rearViewRevealDisplacement;
@property (assign, nonatomic) CGFloat rightViewRevealDisplacement;

// If YES (the default) the controller will bounce to the Left position when dragging further than 'rearViewRevealWidth'
@property (assign, nonatomic) BOOL bounceBackOnOverdraw;
@property (assign, nonatomic) BOOL bounceBackOnLeftOverdraw;

// If YES (default is NO) the controller will allow permanent dragging up to the rightMostPosition
@property (assign, nonatomic) BOOL stableDragOnOverdraw;
@property (assign, nonatomic) BOOL stableDragOnLeftOverdraw;
@property (assign, nonatomic) BOOL stableDragOnLeftOverdraw; // <-- simetric implementation of the above for the rightViewController

// Velocity required for the controller to toggle its state based on a swipe movement, default is 300
@property (assign, nonatomic) CGFloat quickFlickVelocity;
Expand All @@ -129,6 +133,9 @@ typedef enum
// Defines the radius of the front view's shadow offset default is {0.0f,2.5f}
@property (assign, nonatomic) CGSize frontViewShadowOffset;

//Defines the front view's shadow opacity, default is 1.0f
@property (assign, nonatomic) CGFloat frontViewShadowOpacity;

// The class properly handles all the relevant calls to appearance methods on the contained controllers.
// Moreover you can assign a delegate to let the class inform you on positions and animation activity.

Expand Down
108 changes: 75 additions & 33 deletions SWRevealViewController/SWRevealViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ - (void)_getAdjustedFrontViewPosition:(FrontViewPosition*)frontViewPosition forS

@implementation SWRevealView


static CGFloat scaledValue( CGFloat v1, CGFloat min2, CGFloat max2, CGFloat min1, CGFloat max1)
{
CGFloat result = min2 + (v1-min1)*((max2-min2)/(max1-min1));
if ( result != result ) return min2; // nan
if ( result < min2 ) return min2;
if ( result > max2 ) return max2;
return result;
}

- (id)initWithFrame:(CGRect)frame controller:(SWRevealViewController*)controller
{
self = [super initWithFrame:frame];
Expand All @@ -138,7 +148,8 @@ - (id)initWithFrame:(CGRect)frame controller:(SWRevealViewController*)controller
CALayer *frontViewLayer = _frontView.layer;
frontViewLayer.masksToBounds = NO;
frontViewLayer.shadowColor = [UIColor blackColor].CGColor;
frontViewLayer.shadowOpacity = 1.0f;
//frontViewLayer.shadowOpacity = 1.0f;
frontViewLayer.shadowOpacity = _c.frontViewShadowOpacity;
frontViewLayer.shadowOffset = _c.frontViewShadowOffset;
frontViewLayer.shadowRadius = _c.frontViewShadowRadius;
}
Expand All @@ -151,12 +162,13 @@ - (void)layoutSubviews
{
if ( _disableLayout ) return;

[self _layoutRearViews];

CGRect bounds = self.bounds;

CGFloat xPosition = [self frontLocationForPosition:_c.frontViewPosition];
_frontView.frame = CGRectMake(xPosition, 0.0f, bounds.size.width, bounds.size.height);
CGFloat xLocation = [self frontLocationForPosition:_c.frontViewPosition];

[self _layoutRearViewsForLocation:xLocation];

_frontView.frame = CGRectMake(xLocation, 0.0f, bounds.size.width, bounds.size.height);

UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:_frontView.bounds];
_frontView.layer.shadowPath = shadowPath.CGPath;
Expand All @@ -171,7 +183,24 @@ - (void)prepareRearViewForPosition:(FrontViewPosition)newPosition
_rearView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self insertSubview:_rearView belowSubview:_frontView];
}
[self _layoutRearViews];

CGFloat xLocation = [self frontLocationForPosition:_c.frontViewPosition];
[self _layoutRearViewsForLocation:xLocation];
[self _prepareForNewPosition:newPosition];
}


- (void)prepareRightViewForPosition:(FrontViewPosition)newPosition
{
if ( _rightView == nil )
{
_rightView = [[UIView alloc] initWithFrame:self.bounds];
_rightView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self insertSubview:_rightView belowSubview:_frontView];
}

CGFloat xLocation = [self frontLocationForPosition:_c.frontViewPosition];
[self _layoutRearViewsForLocation:xLocation];
[self _prepareForNewPosition:newPosition];
}

Expand All @@ -197,38 +226,47 @@ - (CGFloat)frontLocationForPosition:(FrontViewPosition)frontViewPosition
}


- (void)dragFrontViewToXPosition:(CGFloat)xPosition
- (void)dragFrontViewToXLocation:(CGFloat)xLocation
{
CGRect bounds = self.bounds;
xPosition = [self _adjustedDragLocationForLocation:xPosition];
_frontView.frame = CGRectMake(xPosition, 0.0f, bounds.size.width, bounds.size.height);

xLocation = [self _adjustedDragLocationForLocation:xLocation];
[self _layoutRearViewsForLocation:xLocation];

_frontView.frame = CGRectMake(xLocation, 0.0f, bounds.size.width, bounds.size.height);
}


- (void)prepareRightViewForPosition:(FrontViewPosition)newPosition
{
if ( _rightView == nil )
{
_rightView = [[UIView alloc] initWithFrame:self.bounds];
_rightView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self insertSubview:_rightView belowSubview:_frontView];
}
[self _layoutRearViews];
[self _prepareForNewPosition:newPosition];
}
# pragma mark private

//- (void)_layoutRearViews
//{
// CGRect bounds = self.bounds;
//
// CGFloat rearWidth = _c.rearViewRevealWidth + _c.rearViewRevealOverdraw;
// _rearView.frame = CGRectMake(0.0, 0.0, rearWidth, bounds.size.height);
//
// CGFloat rightWidth = _c.rightViewRevealWidth + _c.rightViewRevealOverdraw;
// _rightView.frame = CGRectMake(bounds.size.width-rightWidth, 0.0f, rightWidth, bounds.size.height);
//}

# pragma mark private

- (void)_layoutRearViews

- (void)_layoutRearViewsForLocation:(CGFloat)xLocation
{
CGRect bounds = self.bounds;

CGFloat rearWidth = _c.rearViewRevealWidth + _c.rearViewRevealOverdraw;
_rearView.frame = CGRectMake(0.0, 0.0, rearWidth, bounds.size.height);
CGFloat rearRevealWidth = _c.rearViewRevealWidth;
CGFloat rearXLocation = scaledValue(xLocation, -_c.rearViewRevealDisplacement, 0, 0, rearRevealWidth);

CGFloat rearWidth = rearRevealWidth + _c.rearViewRevealOverdraw;
_rearView.frame = CGRectMake(rearXLocation, 0.0, rearWidth, bounds.size.height);

CGFloat rightWidth = _c.rightViewRevealWidth + _c.rightViewRevealOverdraw;
_rightView.frame = CGRectMake(bounds.size.width-rightWidth, 0.0f, rightWidth, bounds.size.height);
CGFloat rightRevealWidth = _c.rightViewRevealWidth;
CGFloat rightXLocation = scaledValue(xLocation, 0, _c.rightViewRevealDisplacement, -rightRevealWidth, 0);

CGFloat rightWidth = rightRevealWidth + _c.rightViewRevealOverdraw;
_rightView.frame = CGRectMake(bounds.size.width-rightWidth+rightXLocation, 0.0f, rightWidth, bounds.size.height);
}


Expand Down Expand Up @@ -349,8 +387,10 @@ - (void)_initDefaultProperties
_rightViewPosition = FrontViewPositionLeft;
_rearViewRevealWidth = 260.0f;
_rearViewRevealOverdraw = 60.0f;
_rearViewRevealDisplacement = 40.0f;
_rightViewRevealWidth = 260.0f;
_rightViewRevealOverdraw = 60.0f;
_rightViewRevealDisplacement = 40.0f;
_bounceBackOnOverdraw = YES;
_bounceBackOnLeftOverdraw = YES;
_stableDragOnOverdraw = NO;
Expand All @@ -359,6 +399,7 @@ - (void)_initDefaultProperties
_toggleAnimationDuration = 0.25;
_frontViewShadowRadius = 2.5f;
_frontViewShadowOffset = CGSizeMake(0.0f, 2.5f);
_frontViewShadowOpacity = 1.0f;
_userInteractionStore = YES;
_animationQueue = [NSMutableArray array];
}
Expand Down Expand Up @@ -490,11 +531,12 @@ - (NSUInteger)supportedInterfaceOrientations
}

// Support for earlier than iOS 6.0
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

#endif


#pragma mark - Public methods and property accessors
Expand Down Expand Up @@ -745,23 +787,23 @@ - (void)_handleRevealGestureStateChangedWithRecognizer:(UIPanGestureRecognizer *
CGFloat translation = [recognizer translationInView:_contentView].x;

CGFloat baseLocation = [_contentView frontLocationForPosition:_panInitialFrontPosition];
CGFloat xPosition = baseLocation + translation;
CGFloat xLocation = baseLocation + translation;

if ( xPosition < 0 )
if ( xLocation < 0 )
{
if ( _rightViewController == nil ) xPosition = 0;
if ( _rightViewController == nil ) xLocation = 0;
[self _rightViewDeploymentForNewFrontViewPosition:FrontViewPositionLeftSide]();
[self _rearViewDeploymentForNewFrontViewPosition:FrontViewPositionLeftSide]();
}

if ( xPosition > 0 )
if ( xLocation > 0 )
{
if ( _rearViewController == nil ) xPosition = 0;
if ( _rearViewController == nil ) xLocation = 0;
[self _rightViewDeploymentForNewFrontViewPosition:FrontViewPositionRight]();
[self _rearViewDeploymentForNewFrontViewPosition:FrontViewPositionRight]();
}

[_contentView dragFrontViewToXPosition:xPosition];
[_contentView dragFrontViewToXLocation:xLocation];
}


Expand Down

0 comments on commit ef2bcec

Please sign in to comment.