|
//
// YRSideViewController.m
// YRSnippets
//
// Created by 王晓宇 on 14-5-10.
// Copyright (c) 2014年 王晓宇. All rights reserved.
//
#import "YRSideViewController.h"
#import "UIImage+ImageEffects.h"
#import <QuartzCore/QuartzCore.h>
#import "UIButton+BadgeView.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface YRSideViewController ()<UIGestureRecognizerDelegate>{
UIView *_baseView;//目前是_baseView
UIView *_currentView;//其实就是rootViewController.view
UIPanGestureRecognizer *_panGestureRecognizer;
CGPoint _startPanPoint;
CGPoint _lastPanPoint;
BOOL _panMovingRightOrLeft;//true是向右,false是向左
UIButton *_coverButton;
BOOL _isInit;//是否是初始化
NSInteger snapCount;
CGFloat naviHeight;
}
@property(nonatomic, strong)UIView *maskView;
@end
@implementation YRSideViewController
@synthesize panGestureRecognizer;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_leftViewShowWidth = 267;
_rightViewShowWidth = 267;
_animationDuration = 0.35;
_showBoundsShadow = true;
if (isIOS7) {
naviHeight = 64;
}else naviHeight = 48;
_panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[_panGestureRecognizer setDelegate:self];
_panMovingRightOrLeft = false;
_lastPanPoint = CGPointZero;
_coverButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
if (isNotIOS8) {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight
|| orientation ==UIInterfaceOrientationLandscapeLeft) {
_coverButton.frame = CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH);
}else {
_coverButton.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
}
[_coverButton addTarget:self action:@selector(hideSideViewController) forControlEvents:UIControlEventTouchUpInside];
_isInit = true;
}
return self;
}
- (id)init{
return [self initWithNibName:nil bundle:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBarHidden = NO;
_baseView = self.view;
[_baseView setBackgroundColor:[UIColor colorWithRed:0.5 green:0.6 blue:0.8 alpha:1]];
snapCount = 0;
if ([[TPUserDefault instance].isNightMode intValue] != 1) {
UIImageView *titleView = [[UIImageView alloc]initWithImage:Image(@"frontPage/paperLogo.png")];
titleView.frame = CGRectMake(0, 0, 43, 25);
titleView.contentMode = UIViewContentModeScaleAspectFit;
self.navigationItem.titleView = titleView;
[self leftIconButton:@"order/addOrder.png" highlited:nil selector:@selector(addOrder:)];
if ([[TPUserDefault instance] isHaveUnMarkedMsg]) {
[self addBudgeViewInRightButton];
}else {
[self rightIconButton:@"setting/setting.png" highlited:nil selector:@selector(callSetting:)];
}
}else {
UIImageView *titleView = [[UIImageView alloc]initWithImage:Image(@"frontPage/paperLogo_night.png")];
titleView.frame = CGRectMake(0, 0, 43, 25);
titleView.contentMode = UIViewContentModeScaleAspectFit;
self.navigationItem.titleView = titleView;
[self leftIconButton:@"order/addOrder_night.png" highlited:nil selector:@selector(addOrder:)];
if ([[TPUserDefault instance] isHaveUnMarkedMsg]) {
[self addBudgeViewInRightButton];
}else {
[self rightIconButton:@"setting/setting_night.png" highlited:nil selector:@selector(callSetting:)];
}
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshAfterOpenNightMode:) name:REFRESHAFTERNIGHTMODE object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
- (void)addBudgeViewInRightButton {
if ([[TPUserDefault instance].isNightMode intValue] != 1) {
[self rightIconButtonWithBudgeView:@"setting/setting.png" highlited:nil selector:@selector(callSetting:)];
}else {
[self rightIconButtonWithBudgeView:@"setting/setting_night.png" highlited:nil selector:@selector(callSetting:)];
}
}
- (void)removeBudgeViewInRightButton {
if ([[TPUserDefault instance].isNightMode intValue] != 1) {
[self rightIconButton:@"setting/setting.png" highlited:nil selector:@selector(callSetting:)];
}else {
[self rightIconButton:@"setting/setting_night.png" highlited:nil selector:@selector(callSetting:)];
}
}
- (void) leftIconButton:(NSString*)aIcon highlited:(NSString*)highlitedIcon selector:(SEL)sel {
UIImage* icon = Image(aIcon);
UIImage* icon_h = Image(highlitedIcon);
UIButton* leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[leftBtn setBackgroundImage:icon forState:UIControlStateNormal];
[leftBtn setBackgroundImage:icon_h forState:UIControlStateHighlighted];
leftBtn.frame = CGRectMake(0, 7, 20, 20);
if (nil != sel) {
[leftBtn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
}
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.leftBarButtonItem = button;
}
- (void) rightIconButton:(NSString*)aIcon highlited:(NSString*)highlitedIcon selector:(SEL)sel {
UIImage* icon = Image(aIcon);
UIImage* icon_h = Image(highlitedIcon);
UIButton* leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[leftBtn setBackgroundImage:icon forState:UIControlStateNormal];
[leftBtn setBackgroundImage:icon_h forState:UIControlStateHighlighted];
leftBtn.frame = CGRectMake(0, 7, 20, 20);
if (nil != sel) {
[leftBtn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
}
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.rightBarButtonItem = button;
}
- (void) rightIconButtonWithBudgeView:(NSString*)aIcon highlited:(NSString*)highlitedIcon selector:(SEL)sel {
UIImage* icon = Image(aIcon);
UIImage* icon_h = Image(highlitedIcon);
UIButton* leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[leftBtn setBackgroundImage:icon forState:UIControlStateNormal];
[leftBtn setBackgroundImage:icon_h forState:UIControlStateHighlighted];
leftBtn.frame = CGRectMake(0, 7, 20, 20);
if (nil != sel) {
[leftBtn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
}
[leftBtn addBadgeView];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.rightBarButtonItem = button;
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// [self layoutCurrentViewWithOffset:0];
[MobClick beginLogPageView:@"PageOne"];
if (!self.rootViewController) {
NSAssert(false, @"you must set rootViewController!!");
}
if (_isInit) {
[self resetCurrentViewToRootViewController];
_isInit=false;
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[MobClick endLogPageView:@"PageOne"];
}
- (UIView*)maskView {
if (!_maskView) {
_maskView = [[UIView alloc]initWithFrame:CGRectZero];
_maskView.backgroundColor = [UIColor colorWithHexString:MaskColor];
_maskView.frame = _currentView.bounds;
_maskView.userInteractionEnabled = YES;
_maskView.alpha = 0.0f;
}
return _maskView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setRootViewController:(UIViewController *)rootViewController{
if (_rootViewController!=rootViewController) {
if (_rootViewController) {
[_rootViewController removeFromParentViewController];
}
_rootViewController=rootViewController;
if (_rootViewController) {
[self addChildViewController:_rootViewController];
}
if (!_isInit) {
[self resetCurrentViewToRootViewController];
}
}
}
-(void)setLeftViewController:(UIViewController *)leftViewController{
if (_leftViewController!=leftViewController) {
if (_leftViewController) {
[_leftViewController removeFromParentViewController];
}
_leftViewController=leftViewController;
if (_leftViewController) {
[self addChildViewController:_leftViewController];
}
}
}
-(void)setRightViewController:(UIViewController *)rightViewController{
if (_rightViewController!=rightViewController) {
if (_rightViewController) {
[_rightViewController removeFromParentViewController];
}
_rightViewController=rightViewController;
if (_rightViewController) {
[self addChildViewController:_rightViewController];
}
}
}
- (void)setNeedSwipeShowMenu:(BOOL)needSwipeShowMenu{
_needSwipeShowMenu = needSwipeShowMenu;
if (needSwipeShowMenu) {
[_baseView addGestureRecognizer:_panGestureRecognizer];
}else{
[_baseView removeGestureRecognizer:_panGestureRecognizer];
}
}
- (void)showShadow:(BOOL)show{
_currentView.layer.shadowOpacity = show ? 0.8f : 0.0f;
if (show) {
_currentView.layer.cornerRadius = 4.0f;
_currentView.layer.shadowOffset = CGSizeZero;
_currentView.layer.shadowRadius = 4.0f;
_currentView.layer.shadowPath = [UIBezierPath bezierPathWithRect:_currentView.bounds].CGPath;
}
}
-(void)resetCurrentViewToRootViewController{
if (_currentView!=_rootViewController.view) {
CGRect frame = CGRectZero;
CGAffineTransform transform = CGAffineTransformIdentity;
if (!_currentView) {
frame = _baseView.bounds;
}else{
frame=_currentView.frame;
transform = _currentView.transform;
}
[_currentView removeFromSuperview];
_currentView = _rootViewController.view;
[_baseView addSubview:_currentView];
_currentView.transform=transform;
_currentView.frame = frame;
if (_leftViewController.view.superview||_rightViewController.view.superview) {
[_currentView addSubview:_coverButton];
[self showShadow:_showBoundsShadow];
}
}
}
#pragma mark ShowOrHideTheView
- (void)willShowLeftViewController{
if (!_leftViewController || _leftViewController.view.superview) {
return;
}
self.maskView.hidden = NO;
[_currentView addSubview:self.maskView];
_leftViewController.view.frame= CGRectMake(-_leftViewShowWidth, naviHeight, _leftViewShowWidth, CGRectGetHeight(_baseView.bounds)-naviHeight);
[_baseView addSubview:_leftViewController.view];
// [_baseView insertSubview:_leftViewController.view belowSubview:_currentView];
if (_rightViewController && _rightViewController.view.superview) {
[_rightViewController.view removeFromSuperview];
}
}
- (void)willShowRightViewController{
if (!_rightViewController || _rightViewController.view.superview) {
return;
}
_rightViewController.view.frame=_baseView.bounds;
[_baseView insertSubview:_rightViewController.view belowSubview:_currentView];
if (_leftViewController && _leftViewController.view.superview) {
[_leftViewController.view removeFromSuperview];
}
}
- (void)showLeftViewController:(BOOL)animated{
if (!_leftViewController) {
return;
}
[self willShowLeftViewController];
NSTimeInterval animatedTime=0;
if (animated) {
animatedTime = ABS(_leftViewShowWidth - _currentView.frame.origin.x) / _leftViewShowWidth * _animationDuration;
}
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView animateWithDuration:animatedTime animations:^{
[self layoutCurrentViewWithOffset:_leftViewShowWidth];
[_currentView addSubview:_coverButton];
[self showShadow:_showBoundsShadow];
}];
}
- (void)showRightViewController:(BOOL)animated{
if (!_rightViewController) {
return;
}
[self willShowRightViewController];
NSTimeInterval animatedTime = 0;
if (animated) {
animatedTime = ABS(_rightViewShowWidth + _currentView.frame.origin.x) / _rightViewShowWidth * _animationDuration;
}
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView animateWithDuration:animatedTime animations:^{
[self layoutCurrentViewWithOffset:-_rightViewShowWidth];
[_currentView addSubview:_coverButton];
[self showShadow:_showBoundsShadow];
}];
}
- (void)hideSideViewController:(BOOL)animated{
[self showShadow:false];
NSTimeInterval animatedTime = 0;
if (animated) {
// animatedTime = ABS(_currentView.frame.origin.x / (_currentView.frame.origin.x>0?_leftViewShowWidth:_rightViewShowWidth)) * _animationDuration;
animatedTime = 0.3;
}
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView animateWithDuration:animatedTime animations:^{
[self layoutCurrentViewWithOffset:0];
} completion:^(BOOL finished) {
_maskView.hidden = YES;
[self.maskView removeFromSuperview];
[_coverButton removeFromSuperview];
[_leftViewController.view removeFromSuperview];
[_rightViewController.view removeFromSuperview];
}];
}
- (void)hideSideViewController{
[self hideSideViewController:true];
}
#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
// Check for horizontal pan gesture
if (gestureRecognizer == _panGestureRecognizer) {
UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer*)gestureRecognizer;
CGPoint translation = [panGesture translationInView:_baseView];
if ([panGesture velocityInView:_baseView].x < 600 && ABS(translation.x)/ABS(translation.y)>1) {
return YES;
}
return NO;
}
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)pan:(UIPanGestureRecognizer*)pan{
if (_panGestureRecognizer.state==UIGestureRecognizerStateBegan) {
_startPanPoint=_currentView.frame.origin;
if (_currentView.frame.origin.x==0) {
[self showShadow:_showBoundsShadow];
}
CGPoint velocity=[pan velocityInView:_baseView];
if(velocity.x>0){
_startPanPoint = CGPointMake(0, 0);
if (_currentView.frame.origin.x>=0 && _leftViewController && !_leftViewController.view.superview) {
[self willShowLeftViewController];
}
}else if (velocity.x<0) {
_startPanPoint = CGPointMake(_leftViewShowWidth, 0);
if (_currentView.frame.origin.x<=0 && _rightViewController && !_rightViewController.view.superview) {
[self willShowRightViewController];
}
}
return;
}
CGPoint currentPostion = [pan translationInView:_baseView];
CGFloat xoffset = _startPanPoint.x + currentPostion.x;
if (xoffset>0) {//向右滑
if (currentPostion.x>0 && self.leftViewController.view.frame.origin.x == 0) {
return;
}
if (currentPostion.x>0 && self.leftViewController.view.frame.origin.x == 0-_leftViewShowWidth) {
return;
}
if (_leftViewController && _leftViewController.view.superview) {
xoffset = xoffset>_leftViewShowWidth?_leftViewShowWidth:xoffset;
}else{
xoffset = 0;
}
}else if(xoffset<0){//向左滑
if (currentPostion.x>0 && self.leftViewController.view.frame.origin.x == 0-_leftViewShowWidth) {
return;
}
if (_rightViewController && _rightViewController.view.superview) {
xoffset = xoffset<-_rightViewShowWidth?-_rightViewShowWidth:xoffset;
}else {
xoffset = xoffset;
}
}
[self layoutCurrentViewWithOffset:xoffset];
if (_panGestureRecognizer.state==UIGestureRecognizerStateEnded) {
if (_currentView.frame.origin.x==0) {
[self showShadow:false];
}
if (_panMovingRightOrLeft && _leftViewController.view.frame.origin.x > -_leftViewShowWidth/2) {
[self showLeftViewController:true];
}else if(!_panMovingRightOrLeft && _currentView.frame.origin.x<-20){
[self showRightViewController:true];
}else{
[self hideSideViewController];
}
_lastPanPoint = CGPointZero;
}else{
CGPoint velocity = [pan velocityInView:_baseView];
if (velocity.x>0) {
_panMovingRightOrLeft = true;
}else if(velocity.x<0){
_panMovingRightOrLeft = false;
}
}
}
//重写此方法可以改变动画效果,PS._currentView就是RootViewController.view
- (void)layoutCurrentViewWithOffset:(CGFloat)xoffset {
if (_showBoundsShadow) {
_currentView.layer.shadowPath = [UIBezierPath bezierPathWithRect:_currentView.bounds].CGPath;
}
if (self.rootViewMoveBlock) {//如果有自定义动画,使用自定义的效果
self.rootViewMoveBlock(_currentView,_baseView.bounds,xoffset);
// return;
}
//平移的动画
[_currentView setFrame:CGRectMake(0, _baseView.bounds.origin.y, _baseView.frame.size.width, _baseView.frame.size.height)];
CGRect leftRect = self.leftViewController.view.frame;
[self.leftViewController.view setFrame:CGRectMake(xoffset-_leftViewShowWidth, leftRect.origin.y, leftRect.size.width, leftRect.size.height)];
if (xoffset == 0) {
_maskView.alpha = 0.0f;
}else {
_maskView.alpha = 0.6*xoffset/_leftViewShowWidth;
}
// TPLOG(@"%f",xoffset);
return;
}
#pragma mark - order btn handler
- (void)addOrder:(id)sender {
if (_leftViewController.view.frame.origin.x == 0 && _leftViewShowWidth == _leftViewController.view.frame.size.width) {
[self hideSideViewController:YES];
}else {
[self showLeftViewController:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:PUSHTOSETTINGS object:@"1"];
}
}
#pragma mark - call setting
- (void)callSetting:(id)sender {
[self removeBudgeViewInRightButton];
[[NSNotificationCenter defaultCenter] postNotificationName:PUSHTOSETTINGS object:nil];
[self hideSideViewController:YES];
}
#pragma mark - add tools
- (void)statusBarOrientationChange:(NSNotification *)notification {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight
|| orientation ==UIInterfaceOrientationLandscapeLeft) {
_baseView.frame = CGRectMake(0, 0, 1024, 768);
}else _baseView.frame = CGRectMake(0, 0, 768, 1024);
_currentView.frame = _baseView.bounds;
_maskView.frame = _currentView.bounds;
_leftViewController.view.frame = CGRectMake(_leftViewController.view.bounds.origin.x,
naviHeight,
_leftViewShowWidth, _leftViewController.view.bounds.size.height);
_coverButton.frame = _baseView.bounds;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
[MobClick event:@"45"];
NSString *str = [NSString stringWithFormat:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=%@&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8",@"878962716" ];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}else{
[MobClick event:@"46"];
}
}
- (void)refreshAfterOpenNightMode:(NSNotification*)noti {
if ([[TPUserDefault instance].isNightMode intValue] != 1) {
UIImageView *titleView = [[UIImageView alloc]initWithImage:Image(@"frontPage/paperLogo.png")];
titleView.frame = CGRectMake(0, 0, 43, 25);
titleView.contentMode = UIViewContentModeScaleAspectFit;
self.navigationItem.titleView = titleView;
[self leftIconButton:@"order/addOrder.png" highlited:nil selector:@selector(addOrder:)];
if ([[TPUserDefault instance] isHaveUnMarkedMsg]) {
[self addBudgeViewInRightButton];
}else {
[self rightIconButton:@"setting/setting.png" highlited:nil selector:@selector(callSetting:)];
}
}else {
UIImageView *titleView = [[UIImageView alloc]initWithImage:Image(@"frontPage/paperLogo_night.png")];
titleView.frame = CGRectMake(0, 0, 43, 25);
titleView.contentMode = UIViewContentModeScaleAspectFit;
self.navigationItem.titleView = titleView;
[self leftIconButton:@"order/addOrder_night.png" highlited:nil selector:@selector(addOrder:)];
if ([[TPUserDefault instance] isHaveUnMarkedMsg]) {
[self addBudgeViewInRightButton];
}else {
[self rightIconButton:@"setting/setting_night.png" highlited:nil selector:@selector(callSetting:)];
}
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
|