// // BackPanViewController.m // ThePaperDemo // // Created by zhousan on 15/7/10. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "BackPanViewController.h" @interface BackPanViewController () { CGPoint startTouch; CGPoint askStartTouch; } @property (nonatomic,assign)BOOL isMoving; @property(nonatomic, strong)UIView *contentView; @property(nonatomic, strong)UIView *askView; @property(nonatomic, strong)UIView *backView; @end #define VIEWWIDTH CGRectGetWidth(self.view.bounds) @implementation BackPanViewController @synthesize panGestureRecognizer; - (void)viewDidLoad { [super viewDidLoad]; panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)]; panGestureRecognizer.maximumNumberOfTouches = 1; panGestureRecognizer.delegate = self; [panGestureRecognizer delaysTouchesBegan]; [self.view addGestureRecognizer:panGestureRecognizer]; [self addChildViewController:self.fontVC]; [self.view addSubview:self.backView]; __weak typeof(self) weakSelf = self; [self.backView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.view.left); make.top.equalTo(weakSelf.view.top); make.bottom.equalTo(weakSelf.view.bottom); make.width.mas_equalTo(2*rect_screen.size.width); }]; } #pragma mark - layout - (void)layoutsubViews { __weak typeof(self) weakSelf = self; [self.backView addSubview:self.contentView]; [_contentVC.webView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.contentView.left); make.top.equalTo(weakSelf.naviBar.bottom); make.bottom.equalTo(weakSelf.contentView.bottom); make.right.mas_equalTo(weakSelf.view.right); }]; [self.contentView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left); make.top.equalTo(weakSelf.backView.top); make.bottom.equalTo(weakSelf.backView.bottom); make.right.mas_equalTo(weakSelf.view.right); }]; [self.contentVC.view makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView); make.top.equalTo(weakSelf.backView); make.bottom.equalTo(weakSelf.backView); make.width.mas_equalTo(weakSelf.view.frame.size.width); }]; [self.askVC.view makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(weakSelf.askView); }]; [self.naviBar makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.view); make.top.equalTo(weakSelf.view); make.height.mas_equalTo(weakSelf.barHeight); make.width.equalTo(weakSelf.view.width); }]; [self.backView addSubview:self.askView]; [self.askView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.contentView.right); make.top.equalTo(weakSelf.backView.top); make.bottom.equalTo(weakSelf.backView.bottom); make.width.mas_equalTo(weakSelf.view.frame.size.width); }]; [self.fontVC.view makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(weakSelf.naviBar.bottom); make.left.equalTo(weakSelf.backView.left); make.right.equalTo(weakSelf.contentView.right); make.bottom.equalTo(weakSelf.contentView.bottom); }]; self.fontVC.view.hidden = YES; } #pragma mark - Getter - (UIView *)backView { if (!_backView) { _backView = [[UIView alloc]initWithFrame:CGRectZero]; _backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; } return _backView; } - (UIView*)contentView { if (!_contentView) { _contentView = [[UIView alloc]initWithFrame:CGRectZero]; self.contentVC.view.bounds = _contentView.bounds; [_contentView addSubview:self.contentVC.view]; [self.naviBar removeFromSuperview]; [self.contentView addSubview:self.fontVC.view]; [_contentView addSubview:self.naviBar]; } return _contentView; } - (UIView*)askView { if (!_askView) { _askView = [[UIView alloc]initWithFrame:CGRectZero]; _askView.backgroundColor = [UIColor lightGrayColor]; self.askVC.view.bounds = _askView.bounds; [_askView addSubview:self.askVC.view]; } return _askView; } - (fontSizeSliderController *)fontVC { if (_fontVC == nil) { _fontVC = [[fontSizeSliderController alloc] init]; } return _fontVC; } #pragma mark - Setteeer - (void)setContentVC:(hotController *)contentVC { _contentVC = contentVC; [self addChildViewController:_contentVC]; if (_askVC != nil) { [self layoutsubViews]; } } - (void)setAskVC:(commentAndAskController *)askVC { _askVC = askVC; [self addChildViewController:_askVC]; _askVC.delegate = self; if (_contentVC != nil) { [self layoutsubViews]; } } #pragma mark - pan handler - (void)pan:(UIPanGestureRecognizer*)panGesture { return; // if (self.isPlay == YES) { // return; // } CGPoint touchPoint = [panGesture locationInView:KEY_WINDOW]; CGFloat offset = touchPoint.x - startTouch.x; // if (self.contentVC.tableView.isDragging) { // _isMoving = NO; // startTouch = touchPoint; // return; // } if (panGesture.state == UIGestureRecognizerStateBegan) { _isMoving = YES; UIMenuController *menu = [UIMenuController sharedMenuController]; if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; } [_askView layer].shadowPath =[UIBezierPath bezierPathWithRect:_askView.bounds].CGPath; _askView.layer.shadowColor = [[UIColor blackColor]CGColor]; _askView.layer.shadowOffset = CGSizeMake(5, 5); _askView.layer.shadowRadius = 5; _askView.layer.shadowOpacity = 1; //解决阴影卡顿 /** * bug:5310(新闻详情页,左滑拉出互动列表,页面之间需要有阴影部分) */ startTouch = touchPoint; }else if (panGesture.state == UIGestureRecognizerStateEnded) { TPLOG(@"end"); if (offset < (0-VIEWWIDTH)/4) { [UIView animateWithDuration:0.3 animations:^{ [self moveViewWithX:0-VIEWWIDTH panRec:panGesture]; } completion:^(BOOL finished) { _isMoving = NO; panGestureRecognizer.enabled = NO; // self.contentVC.tableView.scrollEnabled = YES; // ((MLNavigationController*)self.navigationController).canDragBack = NO; }]; } else { [UIView animateWithDuration:0.3 animations:^{ [self moveViewWithX:0 panRec:panGesture]; } completion:^(BOOL finished) { _isMoving = NO; // self.contentVC.tableView.scrollEnabled = YES; }]; } return; }else if (panGesture.state == UIGestureRecognizerStateCancelled) { [UIView animateWithDuration:0.3 animations:^{ [self moveViewWithX:0 panRec:panGesture]; } completion:^(BOOL finished) { _isMoving = NO; // self.contentVC.tableView.scrollEnabled = YES; }]; return; }else if (panGesture.state == UIGestureRecognizerStateChanged) { TPLOG(@"begin"); // self.contentVC.tableView.scrollEnabled = NO; } if (_isMoving) { [self moveViewWithX:offset panRec:panGesture]; } } - (void)moveViewWithX:(float)x panRec:(UIPanGestureRecognizer*)recoginzer { x = x<(0-VIEWWIDTH)?(0-VIEWWIDTH):x; x= x>0?0:x; if (x != (0-VIEWWIDTH)) { CGPoint offset = [recoginzer translationInView:KEY_WINDOW]; if (x != 0) { CGRect viewFrame = self.contentView.frame; CGRect askFrame = self.askView.frame; askFrame.origin.x = askFrame.origin.x + offset.x; self.askView.frame = askFrame; viewFrame.origin.x = viewFrame.origin.x + offset.x/2; self.contentView.frame = viewFrame; }else { CGRect viewFrame = self.contentView.frame; CGRect askFrame = self.askView.frame; askFrame.origin.x = VIEWWIDTH; self.askView.frame = askFrame; viewFrame.origin.x = 0; self.contentView.frame = viewFrame; } [recoginzer setTranslation:CGPointMake(0, 0) inView:KEY_WINDOW]; }else { CGRect viewFrame = self.contentView.frame; CGRect askFrame = self.askView.frame; askFrame.origin.x = 0; self.askView.frame = askFrame; viewFrame.origin.x = (0-VIEWWIDTH)/2; self.contentView.frame = viewFrame; [self moveCommentViewEnded]; } } - (void)moveCommentViewEnded { } #pragma mark - askDelegate - (void)removePanGesture { [self.view removeGestureRecognizer:panGestureRecognizer]; } - (void)reAddpanGesture { [self.view addGestureRecognizer:panGestureRecognizer]; } - (void)removeAskView:(UIPanGestureRecognizer*)panGesture { if (panGestureRecognizer.enabled) { return; } CGPoint touchPoint = [panGesture locationInView:KEY_WINDOW]; CGFloat offset = touchPoint.x - askStartTouch.x; if (panGesture.state == UIGestureRecognizerStateBegan) { _isMoving = YES; UIMenuController *menu = [UIMenuController sharedMenuController]; if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; } askStartTouch = touchPoint; }else if (panGesture.state == UIGestureRecognizerStateEnded) { if (offset > VIEWWIDTH/3) { [UIView animateWithDuration:0.3 animations:^{ [self moveAskViewWithX:VIEWWIDTH panRec:panGesture]; } completion:^(BOOL finished) { _isMoving = NO; panGestureRecognizer.enabled = YES; // ((MLNavigationController*)self.navigationController).canDragBack = YES; }]; } else { [UIView animateWithDuration:0.3 animations:^{ [self moveAskViewWithX:0 panRec:panGesture]; } completion:^(BOOL finished) { _isMoving = NO; panGestureRecognizer.enabled = NO; // ((MLNavigationController*)self.navigationController).canDragBack = NO; }]; } return; }else if (panGesture.state == UIGestureRecognizerStateCancelled) { [UIView animateWithDuration:0.3 animations:^{ [self moveAskViewWithX:0 panRec:panGesture]; } completion:^(BOOL finished) { _isMoving = NO; panGestureRecognizer.enabled = NO; // ((MLNavigationController*)self.navigationController).canDragBack = NO; }]; return; } if (_isMoving) { [self moveAskViewWithX:offset panRec:panGesture]; } } - (void)removeButtonHandler { [self removeAskAndComment]; } - (void)moveToAskAndComment { [UIView animateWithDuration:0.3 animations:^{ CGRect viewFrame = self.contentView.frame; CGRect askFrame = self.askView.frame; askFrame.origin.x = 0; self.askView.frame = askFrame; viewFrame.origin.x = (0-VIEWWIDTH)/2; self.contentView.frame = viewFrame; } completion:^(BOOL finished) { panGestureRecognizer.enabled = NO; // ((MLNavigationController*)self.navigationController).canDragBack = NO; _isMoving = NO; }]; } - (void)removeAskAndComment { [UIView animateWithDuration:0.3 animations:^{ CGRect viewFrame = self.contentView.frame; CGRect askFrame = self.askView.frame; askFrame.origin.x = VIEWWIDTH; self.askView.frame = askFrame; viewFrame.origin.x = 0; self.contentView.frame = viewFrame; } completion:^(BOOL finished) { panGestureRecognizer.enabled = YES; // ((MLNavigationController*)self.navigationController).canDragBack = YES; _isMoving = NO; }]; } - (void)moveAskViewWithX:(float)x panRec:(UIPanGestureRecognizer*)recoginzer { x = x>VIEWWIDTH?VIEWWIDTH:x; x= x<0?0:x; if (x != VIEWWIDTH) { CGPoint offset = [recoginzer translationInView:KEY_WINDOW]; if (x != 0) { CGRect viewFrame = self.contentView.frame; CGRect askFrame = self.askView.frame; askFrame.origin.x = askFrame.origin.x + offset.x; self.askView.frame = askFrame; viewFrame.origin.x = viewFrame.origin.x + offset.x/2; self.contentView.frame = viewFrame; }else { CGRect viewFrame = self.contentView.frame; CGRect askFrame = self.askView.frame; askFrame.origin.x = 0; self.askView.frame = askFrame; viewFrame.origin.x = 0-VIEWWIDTH/2; self.contentView.frame = viewFrame; } [recoginzer setTranslation:CGPointMake(0, 0) inView:KEY_WINDOW]; }else { CGRect viewFrame = self.contentView.frame; CGRect askFrame = self.askView.frame; askFrame.origin.x = VIEWWIDTH; self.askView.frame = askFrame; viewFrame.origin.x = 0; self.contentView.frame = viewFrame; [self removeCommentViewEnded]; } } - (void)removeCommentViewEnded { } #pragma mark UIGestureRecognizerDelegate - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } - (void)tapNaviBar:(id)sender { [self.contentVC.webView.scrollView setContentOffset:CGPointZero animated:YES]; } - (void)commentSuccess { [self.askVC commentSuccess]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end