// // askAndCommandController.m // ThePaperDemo // // Created by Scar on 14-9-15. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "commentTableController.h" #import "commentAndAskController.h" #import "CoreAnimationEffect.h" #import "RubberBandView.h" #import "askTableController.h" #import "askDetailController.h" #import "writeCommentAndAskController.h" #import "loginHomeController.h" #import "ZSAnimationView.h" #import "detailContentHomeController.h" #import "imageShareContent.h" @interface commentMenuBtn : UIButton @property(nonatomic, strong)UILabel *typeLabel; @property(nonatomic, strong)UILabel *numLabel; @property(nonatomic, strong)NSString *numText; @property(nonatomic, strong)NSString *titleText; @end @implementation commentMenuBtn @synthesize numText = _numText; @synthesize titleText = _titleText; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; [self addSubview:self.typeLabel]; [self addSubview:self.numLabel]; } return self; } - (void)setSelected:(BOOL)selected { [super setSelected:selected]; if (!selected) { self.numLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; self.typeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; }else { self.numLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; self.typeLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; } } - (void)setNumText:(NSString *)text { _numText = text; self.numLabel.text = [NSString stringWithFormat:@"(%@)",text]; } - (void)setTitleText:(NSString *)text { _titleText = text; self.typeLabel.text = text; } - (UILabel*)typeLabel { if (!_typeLabel) { _typeLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _typeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; _typeLabel.textAlignment = NSTextAlignmentRight; // _typeLabel.userInteractionEnabled = NO; } _typeLabel.font = appFont(35/2, NO); return _typeLabel; } - (UILabel*)numLabel { if (!_numLabel) { if (!_numLabel) { _numLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _numLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; _numLabel.textAlignment = NSTextAlignmentLeft; // _numLabel.userInteractionEnabled = NO; } _numLabel.font = appFont(13, NO); } return _numLabel; } - (void)layoutSubviews { [super layoutSubviews]; self.typeLabel.frame = CGRectMake(0, 0, 45, CGRectGetHeight(self.bounds)); self.numLabel.frame = CGRectMake(CGRectGetMaxX(self.typeLabel.frame), 15/2, CGRectGetWidth(self.bounds)- CGRectGetWidth(self.typeLabel.frame), 15); } @end @interface commentAndAskController () { UIPanGestureRecognizer *panGestureRecognizer; UISwipeGestureRecognizer *swipeGesture; } @property(nonatomic, strong)NSArray *viewControllers; @property(nonatomic, strong)NSMutableArray *pageViewControllers; @property(nonatomic, strong)UIButton *commentButton; @property(nonatomic, strong)UIButton *askButton; @property(nonatomic, strong)commentMenuBtn *commentBtn; @property(nonatomic, strong)commentMenuBtn *askBtn; @property(nonatomic, strong)NSArray *askList; @property(nonatomic, strong)ZSAnimationView *menuRubberView; @property(nonatomic, strong)writeCommentAndAskController *writeCommentContentVC; @end @implementation commentAndAskController @synthesize viewControllers,pageViewControllers; @synthesize dataDic = _dataDic; @synthesize commentSum,qaNum; @synthesize commentList; @synthesize firstCommentUrl = _firstCommentUrl; @synthesize firstAskUrl = _firstAskUrl; @synthesize nodeId = _nodeId; @synthesize delegate; @synthesize gestureView; @synthesize writeCommentContentVC; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.titleStr = @""; self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; [self.view addSubview:self.pagingView]; [self.naviBar addSubview:self.commentBtn]; [self.naviBar addSubview:self.askBtn]; [self.naviBar addSubview:self.menuRubberView]; self.commentBtn.selected = YES; self.askBtn.selected = NO; UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.naviBar.bounds)/2+20, CGRectGetHeight(self.naviBar.bounds)-30, 1, 15)]; lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; [self.naviBar addSubview:lineView]; self.viewControllers = [NSArray arrayWithObjects:@"commentTableController",@"askTableController", nil]; [viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if (nil == self.pageViewControllers) { self.pageViewControllers = [NSMutableArray array]; } if ([obj isKindOfClass:[UIViewController class]]) { [self.pageViewControllers addObject:obj]; [self addChildViewController:obj]; } else { [self.pageViewControllers addObject:[NSNull null]]; } }]; UIView *view = self.pagingView; [self.pagingView removeFromSuperview]; view.frame = CGRectMake(0, self.barHeight, self.view.frame.size.width, self.view.frame.size.height-self.barHeight); [self.view addSubview:view]; [self.pagingView reloadData]; swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandler:)]; swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft; swipeGesture.delegate = self; [self.view addGestureRecognizer:swipeGesture]; panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)]; panGestureRecognizer.maximumNumberOfTouches = 1; [panGestureRecognizer delaysTouchesBegan]; [self.view addGestureRecognizer:panGestureRecognizer]; __weak typeof(self) weakSelf = self; [self.pagingView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.view.left); make.right.equalTo(weakSelf.view.right); make.bottom.equalTo(weakSelf.view.bottom); make.top.equalTo(weakSelf.view.top).offset(self.barHeight); }]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; } - (void)needrefreshNightMode:(id)sender{ self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; self.commentBtn.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; if (!self.commentBtn.selected) { self.commentBtn.numLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; self.commentBtn.typeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; }else { self.commentBtn.numLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; self.commentBtn.typeLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; } if (!self.askBtn.selected) { self.askBtn.numLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; self.askBtn.typeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; }else { self.askBtn.numLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; self.askBtn.typeLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; } self.askBtn.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; _menuRubberView.backgroundColor = [UIColor colorWithHexString:BLUECOLOR]; } - (void)dealloc { self.pagingView = nil; self.pageViewControllers = nil; self.viewControllers = nil; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)swipeHandler:(UISwipeGestureRecognizer*)gestureRecognizer{ if (self.pagingView.currentPageIndex == 0) { self.askBtn.selected = !self.askBtn.selected; self.commentBtn.selected = !self.commentBtn.selected; if (self.askBtn.selected) { [self.pagingView setCurrentPageIndexWithAnimation:1]; /** * bug:5014 */ // [CoreAnimationEffect animationPushRight:self.pagingView]; [self.menuRubberView animationStartWithOffsetX:self.askBtn.frame.origin.x+10]; // [self.menuRubberView pullWithOffSet:290/2-6]; // [self.menuRubberView recoverStateAnimation]; } }else { self.commentBtn.selected = !self.commentBtn.selected; self.askBtn.selected = !self.askBtn.selected; if (self.commentBtn.selected) { [self.pagingView setCurrentPageIndexWithAnimation:0]; /** * bug:5014 */ // [CoreAnimationEffect animationPushLeft:self.pagingView]; [self.menuRubberView animationStartWithOffsetX:self.commentBtn.frame.origin.x+10]; } } } - (void)pan:(UIPanGestureRecognizer*)panGesture { if ([delegate conformsToProtocol:@protocol(askDelegate)] && [delegate respondsToSelector:@selector(removeAskView:)]) { [delegate removeAskView:panGesture]; } } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } - (void)backHandler { if ([delegate conformsToProtocol:@protocol(askDelegate)] && [delegate respondsToSelector:@selector(removeButtonHandler)]) { [delegate removeButtonHandler]; }else{ [self.navigationController popViewControllerAnimated:YES]; } } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:NO]; } #pragma mark - tabBar Menu Button - (commentMenuBtn *)commentBtn { if (!_commentBtn) { _commentBtn = [[commentMenuBtn alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.naviBar.bounds)/2-215/2+10, CGRectGetHeight(self.naviBar.bounds)-35,290/2-6, 25)]; _commentBtn.titleText = @"评论"; _commentBtn.center = CGPointMake(CGRectGetWidth(self.naviBar.bounds)*3/8, (CGRectGetHeight(self.naviBar.bounds)-35)*3/2); /** * bug:5202(互动列表页,上方评论和追问要居中) */ [_commentBtn addTarget:self action:@selector(commentBtnEvent:) forControlEvents:UIControlEventTouchUpInside]; } return _commentBtn; } - (commentMenuBtn *)askBtn { if (!_askBtn) { _askBtn = [[commentMenuBtn alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.naviBar.bounds)/2+65/2+10, CGRectGetHeight(self.naviBar.bounds)-35, 290/2+6, 25)]; _askBtn.titleText = @"追问"; _askBtn.center = CGPointMake(CGRectGetWidth(self.naviBar.bounds)*7/8, (CGRectGetHeight(self.naviBar.bounds)-35)*3/2); [_askBtn addTarget:self action:@selector(askBtnEvent:) forControlEvents:UIControlEventTouchUpInside]; } return _askBtn; } - (void)setCommentSum:(NSInteger)sum { commentSum = sum; self.commentBtn.numText = [NSString stringWithFormat:@"%ld",(long)sum]; } - (void)reflashNumsWithCount:(NSString *)total { self.commentBtn.numText = total; } - (void)setQaNum:(NSInteger)num { qaNum = num; self.askBtn.numText = [NSString stringWithFormat:@"%ld",(long)num]; } - (void)reflashAskNumsWithCount:(NSString *)total { self.askBtn.numText = total; } - (void)commentBtnEvent:(UIButton*)btn { if (!btn.selected) { btn.selected = !btn.selected; self.askBtn.selected = !btn.selected; if (btn.selected) { [self.pagingView setCurrentPageIndexWithAnimation:0]; // [CoreAnimationEffect animationPushLeft:self.pagingView]; [self.menuRubberView animationStartWithOffsetX:self.commentBtn.frame.origin.x+10]; // NSLog(@"%f",self.menuRubberView.property.y); // [self.menuRubberView resetDefault]; } }else { [self.pageViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [((commentTableController*)obj).tableView setContentOffset:CGPointZero animated:YES]; }]; } } - (void)askBtnEvent:(UIButton*)btn { if (!btn.selected) { btn.selected = !btn.selected; self.commentBtn.selected = !btn.selected; if (btn.selected) { [self.pagingView setCurrentPageIndexWithAnimation:1]; /** * bug:5014 */ // [CoreAnimationEffect animationPushRight:self.pagingView]; [self.menuRubberView animationStartWithOffsetX:self.askBtn.frame.origin.x+10]; } }else { [self.pageViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [((askTableController*)obj).tableView setContentOffset:CGPointZero animated:YES]; }]; } } - (ZSAnimationView*)menuRubberView { if (!_menuRubberView) { _menuRubberView = [[ZSAnimationView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.commentBtn.frame)+10, self.barHeight-3, 60, 3)]; _menuRubberView.backgroundColor = [UIColor colorWithHexString:BLUECOLOR]; } return _menuRubberView; } - (void)commentSuccess { commentTableController *vc = self.pageViewControllers[0]; [vc commentSuccess:commentType]; } #pragma mark - paging view - (ATPagingView*)pagingView { if (!_pagingView) { _pagingView = [[ATPagingView alloc]initWithFrame:CGRectMake(0, self.barHeight, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-self.barHeight)]; _pagingView.gapBetweenPages = 0; _pagingView.recyclingEnabled = NO; _pagingView.backgroundColor = [UIColor whiteColor]; _pagingView.autoresizingMask = AutoresizingFull; _pagingView.delegate = self; _pagingView.scrollView.scrollEnabled = NO; } return _pagingView; } - (UIButton*)commentButton { if (!_commentButton) { _commentButton = [UIButton buttonWithType:UIButtonTypeCustom]; } return _commentButton; } - (UIButton*)askButton { if (!_askButton) { } return _askButton; } - (void)setCommentList:(NSArray *)list { commentList = list; [self.pageViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[commentTableController class]]) { commentTableController *commentTable = obj; commentTable.commentList = [list mutableCopy]; if (self.commentOT && !isBlankString(self.commentOT)) { commentTable.commentOT = self.commentOT; } } }]; } - (void)setDataDic:(NSDictionary *)dic { _dataDic = dic; __weak typeof(self) Self = self; [self.pageViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[askTableController class]]) { ((askTableController*)obj).askData = dic; [((askTableController*)obj) goToAskDetailPage:^(id commentObj) { askDetailController *vc = askDetailController.new; vc.commentBO = commentObj; [Self.navigationController pushViewController:vc animated:YES]; }]; } }]; } - (void)setFirstAskUrl:(NSString *)url { _firstAskUrl = url; [self.pageViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[askTableController class]]) { ((askTableController*)obj).nextUrl = url; ((askTableController*)obj).commentOT = self.commentOT; } }]; } - (void)setFirstCommentUrl:(NSString *)url { _firstCommentUrl = url; [self.pageViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[commentTableController class]]) { ((commentTableController*)obj).nextUrl = url; } }]; } - (void)setNodeId:(NSString *)nodeIdStr { _nodeId = nodeIdStr; [self.pageViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[commentTableController class]]) { ((commentTableController*)obj).nodeId = nodeIdStr; ((commentTableController*)obj).delegate = self; } }]; [self.pageViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[askTableController class]]) { ((askTableController*)obj).nodeId = nodeIdStr; ((askTableController*)obj).delegate = self; } }]; } #pragma mark ATPagingViewDelegate methods - (NSInteger)numberOfPagesInPagingView:(ATPagingView *)pagingView { return self.viewControllers.count; } - (UIView *)viewForPageInPagingView:(ATPagingView *)pagingView atIndex:(NSInteger)index { if (self.pageViewControllers[index] == [NSNull null]) { NSString *className = self.viewControllers[index]; Class class = NSClassFromString(className); UIViewController *vc = [[class alloc] init]; if (self.nodeId && !isBlankString(self.nodeId)) { [vc setValue:self.nodeId forKey:@"nodeId"]; } [vc setValue:self forKey:@"delegate"]; /** * bug:6086(直播页和外链文章:切换网络环境后,刷新互动页必闪退) */ [self addChildViewController:vc]; self.pageViewControllers[index] = vc; } return ((UIViewController*)self.pageViewControllers[index]).view; } - (void)currentPageDidChangeInPagingView:(ATPagingView *)pageView { } - (void)pagesDidChangeInPagingView:(ATPagingView *)pageView { } - (void)pagingViewWillBeginMoving:(ATPagingView *)pageView { } - (void)pagingViewdDidScroll:(UIScrollView *)scrollerView { } #pragma mark - askTableView 回复delegate - (void)gotoCommentWithComment:(commentObjectVO *)comment { [self presentAskToUserContent:comment]; } #pragma mark - commentTable delegate - (void)presentWriteContent { self.writeCommentContentVC = [[writeCommentAndAskController alloc]init]; self.writeCommentContentVC.type = commentType; self.writeCommentContentVC.nodeId = self.nodeId; self.writeCommentContentVC.delegate = self; [self presentController:self.writeCommentContentVC animated:YES completion:^{ panGestureRecognizer.enabled = NO; // [((detailContentHomeController *)self.parentViewController) removePanGesture]; swipeGesture.enabled = NO; }]; } - (void)presentWriteToUser:(commentObjectVO *)commentBO { self.writeCommentContentVC = [[writeCommentAndAskController alloc]init]; self.writeCommentContentVC.type = commentForUserType; self.writeCommentContentVC.nodeId = self.nodeId; self.writeCommentContentVC.commentBO = commentBO; self.writeCommentContentVC.delegate = self; self.writeCommentContentVC.view.frame = self.view.bounds; [self presentController:self.writeCommentContentVC animated:YES completion:^{ panGestureRecognizer.enabled = NO; // [((detailContentHomeController *)self.parentViewController) removePanGesture]; swipeGesture.enabled = NO; }]; } - (void)presentAskContent { self.writeCommentContentVC = [[writeCommentAndAskController alloc]init]; self.writeCommentContentVC.type = askType; self.writeCommentContentVC.nodeId = self.nodeId; self.writeCommentContentVC.delegate = self; self.writeCommentContentVC.view.frame = self.view.bounds; [self presentController:self.writeCommentContentVC animated:YES completion:^{ panGestureRecognizer.enabled = NO; // [((detailContentHomeController *)self.parentViewController) removePanGesture]; swipeGesture.enabled = NO; }]; } - (void)presentAskToUserContent:(commentObjectVO *)commentBO { self.writeCommentContentVC = [[writeCommentAndAskController alloc]init]; self.writeCommentContentVC.type = askForUserType; self.writeCommentContentVC.nodeId = self.nodeId; self.writeCommentContentVC.delegate = self; self.writeCommentContentVC.commentBO = commentBO; [self presentController:self.writeCommentContentVC animated:YES completion:^{ panGestureRecognizer.enabled = NO; // [((detailContentHomeController *)self.parentViewController) removePanGesture]; swipeGesture.enabled = NO; }]; } - (void)commentPushToLoginView { loginHomeController *vc = loginHomeController.new; [self.navigationController pushViewController:vc animated:YES]; } - (void)askPushToLoginView { loginHomeController *vc = loginHomeController.new; [self.navigationController pushViewController:vc animated:YES]; } - (void)dismissWriteContent:(commentAndAskType)type{ __weak typeof(self) Self = self; [[UIApplication sharedApplication]beginIgnoringInteractionEvents]; [self dismissViewController:self.writeCommentContentVC animated:YES completion:^{ Self.writeCommentContentVC = nil; ((MLNavigationController *)self.navigationController).canDragBack = YES; panGestureRecognizer.enabled = YES; // [((detailContentHomeController *)self.parentViewController) reAddpanGesture]; swipeGesture.enabled = YES; [[UIApplication sharedApplication]endIgnoringInteractionEvents]; }]; } - (void)commentSuccess:(commentAndAskType)type { __weak typeof(self) Self = self; swipeGesture.enabled = YES; ((MLNavigationController *)self.navigationController).canDragBack = YES; panGestureRecognizer.enabled = YES; // [((detailContentHomeController *)self.parentViewController) reAddpanGesture]; [self dismissViewController:self.writeCommentContentVC animated:YES completion:^{ if (Self.writeCommentContentVC.type == commentType || Self.writeCommentContentVC.type == commentForUserType) { [Self.pageViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[commentTableController class]]) { [(commentTableController*)obj commentSuccess:type]; } }]; }else if (Self.writeCommentContentVC.type == askType || Self.writeCommentContentVC.type == askForUserType) { [Self.pageViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[askTableController class]]) { [(askTableController*)obj commentSuccess:type]; } }]; } Self.writeCommentContentVC = nil; }]; } #pragma mark -- SHARE -(void)askToShare:(UIImage *)img contentBO:(contentObjectBO *)contentBO{ imageShareContent *askShareContentView = [[imageShareContent alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))]; askShareContentView.contentBO = contentBO; askShareContentView.shareImg = img; askShareContentView.baseController = self; askShareContentView.sharestyle = shareNewsStyle; [askShareContentView presentShareContentInView:self.view sender:nil]; } -(void) commentToShare:(UIImage *)img contentBO:(contentObjectBO *)contentBO{ imageShareContent *shareContentView = [[imageShareContent alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))]; shareContentView.contentBO = contentBO; shareContentView.shareImg = img; shareContentView.baseController = self; shareContentView.sharestyle = shareNewsStyle; [shareContentView presentShareContentInView:self.view sender:nil]; } - (void)disablePanGesture { panGestureRecognizer.enabled = NO; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end