|
//
// 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 "orderHelpController.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface YRSideViewController ()<UIGestureRecognizerDelegate,orderContentHelpDelegate,UIAlertViewDelegate>{
UIView *_baseView;//目前是_baseView
UIView *_currentView;//其实就是rootViewController.view
UIPanGestureRecognizer *_panGestureRecognizer;
UISwipeGestureRecognizer *_swipeGestrurerecognizer;
CGPoint _startPanPoint;
CGPoint _lastPanPoint;
BOOL _panMovingRightOrLeft;//true是向右,false是向左
UIButton *_coverButton;
BOOL _isInit;//是否是初始化
NSInteger snapCount;
}
@property(nonatomic, strong)UIImageView *maskView;
@property(nonatomic, strong)UIImageView *addBtnView;
@property(nonatomic, strong)orderHelpController *orderHelpVC;
@end
@implementation YRSideViewController
@synthesize panGestureRecognizer;
@synthesize delegate;
- (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;
_panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[_panGestureRecognizer setDelegate:self];
_panMovingRightOrLeft = false;
_lastPanPoint = CGPointZero;
_swipeGestrurerecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
_swipeGestrurerecognizer.direction= UISwipeGestureRecognizerDirectionRight;
_coverButton = [[UIButton alloc]initWithFrame: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.
_baseView = self.view;
[_baseView setBackgroundColor:[UIColor colorWithRed:0.5 green:0.6 blue:0.8 alpha:1]];
self.needSwipeShowMenu = true;
snapCount = 0;
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[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"];
}
- (UIImageView*)maskView {
#warning ios 6 or below will break up. Please take care of this!
// _maskView = [self customSnapshotImageFromView:_currentView];
_maskView = [UIImageView new];
_maskView.frame = _currentView.bounds;
_maskView.userInteractionEnabled = YES;
_maskView.alpha = 0.0f;
snapCount = snapCount + 1;
if (snapCount == 10) {
snapCount = 10;
}
return _maskView;
}
- (UIImageView*)addBtnView {
if (!_addBtnView) {
_addBtnView = [[UIImageView alloc]initWithFrame:CGRectZero];
if (isIOS7) {
_addBtnView.frame = CGRectMake(_leftViewShowWidth+15, 18+20, 20, 20);
}else {
_addBtnView.frame = CGRectMake(_leftViewShowWidth+15, 0+20, 20, 20);
}
_addBtnView.hidden = YES;
}
if ([[TPUserDefault instance].isNightMode intValue] == 0) {
_addBtnView.image = Image(@"frontPage/add_btn.png");
}else {
_addBtnView.image = Image(@"frontPage/add_btn_night.png");
}
/**
* bug:5180(夜间模式,订阅导航页,右上角的X不显示)
*/
return _addBtnView;
}
- (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];
}
[_leftViewController.view addSubview:self.addBtnView];
}
}
#pragma mark ShowOrHideTheView
- (void)willShowLeftViewController{
if (!_leftViewController || _leftViewController.view.superview) {
return;
}
if (!_maskView) {
self.maskView.hidden = YES;
}
if (snapCount == 1) {
[_currentView addSubview:_maskView];
}
// if (isIOS7) {
// _maskView.image = [self customSnapshotImageFromView:_currentView].image;
// }else _maskView.backgroundColor = [UIColor colorWithHexString:@"0x3e3c3d"];
/**
* bug:5205(首页瀑布流,快速右滑无法进入订阅导航页)
*/
_maskView.backgroundColor = [UIColor colorWithHexString:@"0x3e3c3d"];
_leftViewController.view.frame= CGRectMake(-_leftViewShowWidth, 0, _leftViewShowWidth, CGRectGetHeight(_baseView.bounds));
[_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;
}
[MobClick event:@"15"];
[self willShowLeftViewController];
if (_maskView.hidden) {
_maskView.hidden = NO;
_maskView.alpha = 0.0f;
}
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];
}completion:^(BOOL finished) {
if ([self.delegate respondsToSelector:@selector(leftControllerDidShowed)]) {
[self.delegate leftControllerDidShowed];
if ([[TPUserDefault instance].helpArray[@"order"] intValue] == 0) {
self.needSwipeShowMenu =NO;
NSMutableDictionary *array = [NSMutableDictionary dictionaryWithDictionary:[TPUserDefault instance].helpArray];
[array setObject:@"1" forKey:@"order"];
[TPUserDefault instance].helpArray = array;
self.orderHelpVC = [orderHelpController new];
self.orderHelpVC.delegate =self;
[self.view addSubview:self.orderHelpVC.view];
[self.orderHelpVC.view makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.left);
make.right.equalTo(self.view.right);
make.top.equalTo(self.view.top);
make.bottom.equalTo(self.view.bottom);
}];
}
/**
* bug:5392(【倒退】栏目导航页:首次进入时,“长按拖入订阅”字样返回首页依然会显示)
*/
}
}];
}
-(void)disHelp{
[UIView animateWithDuration:0.3 animations:^{
[self.orderHelpVC.view removeFromSuperview];
}completion:^(BOOL finished) {
self.orderHelpVC = nil;
self.needSwipeShowMenu = YES;
}];
}
- (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.addBtnView.hidden = YES;
[_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)swipe:(UISwipeGestureRecognizer*)gesture {
[self showLeftViewController:YES];
}
- (void)pan:(UIPanGestureRecognizer*)pan{
if (_panGestureRecognizer.state==UIGestureRecognizerStateBegan) {
_startPanPoint=_currentView.frame.origin;
if (_currentView.frame.origin.x==0) {
[self showShadow:_showBoundsShadow];
}
[_currentView addSubview:_coverButton];
/**
* bug:5394( 栏目导航页:用手拖住向右滑动后,点击右侧空的地方无法返回,见图)
*/
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 (_maskView.hidden) {
_maskView.hidden = NO;
_maskView.alpha = 0.0f;
}
if (currentPostion.x>0 && self.leftViewController.view.frame.origin.x == 0) {
return;
}
if (_leftViewController && _leftViewController.view.superview) {
xoffset = xoffset>_leftViewShowWidth?_leftViewShowWidth:xoffset;
}else{
xoffset = 0;
}
}else if(xoffset<0){//向左滑
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/3) {
[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 {
self.addBtnView.hidden = NO;
[_currentView bringSubviewToFront:self.addBtnView];
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.98 * xoffset/_leftViewShowWidth;
}
[_currentView bringSubviewToFront:_coverButton];
/**
* bug:5394( 栏目导航页:用手拖住向右滑动后,点击右侧空的地方无法返回,见图)
*/
CGAffineTransform at =CGAffineTransformMakeRotation(M_PI/4*_maskView.alpha);
[self.addBtnView setTransform:at];
// TPLOG(@"%f",xoffset);
return;
}
- (UIImageView*)customSnapshotImageFromView:(UIView*)inputView {
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, YES, 1);
[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc]initWithImage:[viewImage applyDarkEffect]];
return imageView;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
[MobClick event:@"41"];
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:@"42"];
}
#pragma mark - shouldAutorotate
- (UIStatusBarStyle)preferredStatusBarStyle {
if ([[TPUserDefault instance].isNightMode intValue] == 1) {
return UIStatusBarStyleLightContent;
}else {
return UIStatusBarStyleDefault;
}
}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
@end
|