iOS 双击TabBar移动至未读消息cell


- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSDate * currentDate = [NSDate new];
    BaseNavigationController * nav = tabBarController.selectedViewController;//获取TabBar
    if (([nav.viewControllers.firstObject isKindOfClass:[self class]]) && (currentDate.timeIntervalSince1970 - self.lastDate.timeIntervalSince1970) <= 0.5) {//根据两次点击事件判断
        [self doubleSelectTabbar];
    }
    if ([nav.viewControllers.firstObject isKindOfClass:[self class]]) {
        self.lastDate = currentDate;//赋值第一次点击
    }
    
    return  true;
}

- (void)doubleSelectTabbar{
//由于项目需求做了frame修改
    CGRect temp = self.tableView.frame;
    temp.origin.y = 0;
    self.tableView.frame = temp;
    tableViewCell *firstCell = [self.tableView visibleCells].firstObject;//显示的第一个cell
    NSIndexPath *firstCellIndex = [self.tableView indexPathForCell:firstCell];//显示cell的indexpath
    
    tableViewCell *lastCell = [self.tableView visibleCells].lastObject;//显示的最后一个cell
    NSIndexPath *lastCellIndex = [self.tableView indexPathForCell:lastCell];//显示的最后一个cell的indexpath
    
    if (firstCellIndex.row < self.conversations.count-1 && lastCellIndex.row < self.conversations.count-1) {
        for (NSInteger i = firstCellIndex.row+1; i < self.conversations.count; i++) {
            DataModel *lastModel = self.conversations[i];
            if (lastModel.unreadCount.unread>0) {//是否有未读消息
                self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:0];//移动至top
                [self toScrollPosition];
                CGRect temp = self.tableView.frame;
                temp.origin.y = temp.origin.y + ( kIs_iPhoneX ? 16 : -8);
                self.tableView.frame = temp;
                break;
            }else{
                
                for (NSInteger i = 0; i < self.conversations.count; i++) {//重新开始移动cell
                    DataModel *lastModel = self.conversations[i];
                    if (lastModel.unreadCount.unread>0) {
                        self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:0];
                        [self toScrollPosition];
                        break;
                    }
                }
            }
        }
    }else{
        for (NSInteger i = 0; i < self.conversations.count; i++) {//没有data
            DataModel *lastModel = self.conversations[i];
            if (lastModel.unreadCount.unread>0) {
                self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:0];
                [self toScrollPosition];
                break;
            }
        }
    }

    temp.origin.y = temp.origin.y + ( kIs_iPhoneX ? 16 : -8);
    self.tableView.frame = temp;

}



- (void)toScrollPosition{
    [self.tableView scrollToRowAtIndexPath:self.currentCellIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

 

转载于:https://my.oschina.net/wayzhu/blog/3089328

你可能感兴趣的:(iOS 双击TabBar移动至未读消息cell)