仿微信双击tabbar依次跳到未读消息

#pragma mark - delegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
//双击那个item
    if (tabBarController.selectedIndex == 1) {
        if ([self checkIsDoubleClick:viewController]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kDoubleClickTabItemNotification" object:nil];
        }
    }
    
    return YES;
}

- (BOOL)checkIsDoubleClick:(UIViewController *)viewController
{
    static UIViewController *lastViewController = nil;
    static NSTimeInterval lastClickTime = 0;
    
    if (lastViewController != viewController) {
        lastViewController = viewController;
        lastClickTime = [NSDate timeIntervalSinceReferenceDate];
        
        return NO;
    }
    
    NSTimeInterval clickTime = [NSDate timeIntervalSinceReferenceDate];
    if (clickTime - lastClickTime > 0.5 ) {
        lastClickTime = clickTime;
        return NO;
    }
    
    lastClickTime = clickTime;
    return YES;
}



//通知方法的实现
- (void)DoubleClickTabItemNotification{
    NSLog(@"双击了 双击了 双击了 双击了");
    ConversationListCell *firstCell = [self.tableView visibleCells].firstObject;
    NSIndexPath *firstCellIndex = [self.tableView indexPathForCell:firstCell];
    
    ConversationListCell *lastCell = [self.tableView visibleCells].lastObject;
    NSIndexPath *lastCellIndex = [self.tableView indexPathForCell:lastCell];
    
    if (firstCellIndex.row < self.dataArray.count-1 && lastCellIndex.row < self.dataArray.count-1) {
        for (NSInteger i = firstCellIndex.row+1; i < self.dataArray.count; i++) {
            EaseConversationModel *lastModel = self.dataArray[i];
            if (lastModel.conversation.unreadMessagesCount>0) {
                self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:0];
                [self.tableView scrollToRowAtIndexPath:self.currentCellIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
                break;
            }
        }
    }else{
        for (NSInteger i = 0; i < self.dataArray.count; i++) {
            EaseConversationModel *lastModel = self.dataArray[i];
            if (lastModel.conversation.unreadMessagesCount>0) {
                self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:0];
                [self.tableView scrollToRowAtIndexPath:self.currentCellIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
                break;
            }
        }
    }
}

//其它相关可能会用到的方法
//获取tableView上面所有的cell

- (NSArray *)cellsForTableView:(UITableView *)tableView

{

    NSInteger sections = tableView.numberOfSections;

    NSMutableArray *cells = [[NSMutableArray alloc]  init];

    for (int section = 0; section < sections; section++) {

        NSInteger rows =  [tableView numberOfRowsInSection:section];

        for (int row = 0; row < rows; row++) {

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

            [cells addObject:[tableView cellForRowAtIndexPath:indexPath]];

        }

    }

    return cells;

}

你可能感兴趣的:(仿微信双击tabbar依次跳到未读消息)