自定义TabBar如何实现连续点击刷新

首先找到自定义TabBar按钮的点击方法

//按钮被选中的事件
-(void) buttonSelected:(UIButton *) button{
    
    self.lastBut.selected=NO;
    
    button.selected=YES;
    
    self.lastBut=button;
    
    NSInteger index=[self.subviews indexOfObject:button];
    
    if (index>2) {
        
        index--;
    }
    
    //通过代理刷新viewcontroller
    if ([self.delegate respondsToSelector:@selector(tabBarRefreshController:currentIndex:)]) {

        [self.delegate tabBarRefreshController:self currentIndex:index];
    
    }
 
}

接下来找到代理方法


//HQTabBarView当中的代理方法用来刷新数据

-(void)tabBarRefreshController:(HQTabBarView *)tabBarView currentIndex:(NSInteger)currentIndex{

    //当tableBar被选中之后,发送一个通知
    //在我们的精华控制器内接收该通知
    NSMutableDictionary *info=[NSMutableDictionary dictionary];
    info[HQTabBarControllerSelectionIndexKey]=[NSNumber numberWithInteger:currentIndex];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:HQTabBarControllerSelectedNotification object:nil userInfo:info];
    
    self.selectedIndex=currentIndex;
}

在代理方法里发送了一个通知,然后去需要刷新的viewcontroller接收通知

-(void) tabBarClicked:(NSNotification *) notification{
    
    NSDictionary *info=notification.userInfo;
    
    //当前被选中的index
    NSInteger currentIndex=[info[HQTabBarControllerSelectionIndexKey] integerValue];
    
    self.clickCount++;
    
    //判断是不是精华控制器+是不是当前窗口+是不是连续点击
    if (currentIndex==0&&[self.view isShowingInWindow]&&self.clickCount>1) {
        [self.tableView.mj_header beginRefreshing];
    }
    
    //如果点击了其他的控制器,将点击次数清零
    if (currentIndex!=0) {
        self.clickCount=0;
    }
    
}


附判断一个view是否在当前窗口

#import "UIView+HQExtension.h"

@implementation UIView (HQExtension)

-(BOOL)isShowingInWindow{
    
    //拿到主窗口
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    
    //转换坐标系
    CGRect newFrame=[window convertRect:self.frame fromView:self.superview];
    
    //判断当前view的frame是否在窗口内
    BOOL isIn=CGRectIntersectsRect(newFrame, window.bounds);
    
    return !self.hidden&&self.alpha>0.01&&self.window==window&&isIn;
    
}

@end





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值