美文网首页
监听tabBarButton这个私有类的点击方法

监听tabBarButton这个私有类的点击方法

作者: 小苗晓雪 | 来源:发表于2017-04-05 14:50 被阅读9次

    双击tabBarButton时候会用到 UIControlEventTouchDownRepeat 这个按钮点击模式 !!!

    #import "BaseTabBar.h"
    
    @interface BaseTabBar ()
    @property (nonatomic , weak) UIButton *publishButton ;
    //上一次点击的按钮:
    @property (nonatomic , strong) UIControl *previousClickTabBarButton ;
    
    @end
    
    @implementation BaseTabBar
    
    #pragma mark - lazyLoad
    - (UIButton *)publishButton {
        if (!_publishButton) {
            UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom] ;
            [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal] ;
            [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted] ;
            [publishButton sizeToFit] ;
            _publishButton = publishButton ;
            [self addSubview:_publishButton] ;
        }
        return _publishButton ;
    }
    
    #pragma mark - layoutSubviews
    - (void)layoutSubviews {
        [super layoutSubviews] ;
        NSInteger count = self.items.count + 1 ;
        CGFloat buttonW = self.width / count ;
        CGFloat buttonH = self.height ;
        CGFloat buttonY = 0 ;
        CGFloat buttonX = 0 ;
        NSInteger i = 0 ;
        for (UIControl *tabBarButton in self.subviews) {
            //遍历私有属性:UITabBarButton只能用 NSClassFromString 方法:
            if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                //一进入程序就让精华按钮处于被点击一次的状态:
                //layoutSubviews会被调用多次 , 如果只是i == 0 判断不够严谨 ;
                if (i == 0 && self.previousClickTabBarButton == nil) {
                    self.previousClickTabBarButton = tabBarButton ;
                }
                
                if (i == 2) i += 1 ;
                buttonX = i * buttonW ;
                //系统的UITabBarButton的高度就是48 , 所以我这里也修改一下
                tabBarButton.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH - buttonY) ;
                i++ ;
                
                //监听点击:
                //因为 UITabBarButton 是UITabBar的私有类 , 不能遍历特定该属性 ;如果还是UIView就不能addTarget:方法 ;所以找到 tabBarButton.superclass 继承自UIControl ;
                [tabBarButton addTarget:self action:@selector(tabBarButtonAction:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchDownRepeat] ;
                
            }
        }
        self.publishButton.center = CGPointMake(self.width * 0.5, self.height * 0.5) ;
        NSLog(@"%@" , self.subviews) ;
        
    }
    
    - (void)tabBarButtonAction:(UIControl *)tabBarButton {
        //判断为重复点击上一次的按钮:
        if (self.previousClickTabBarButton == tabBarButton) {
            DebugLog ;
        }
        //把上一次点击的按钮存起来:
        self.previousClickTabBarButton = tabBarButton ;
        
    }
    
    @end
    
    

    愿编程让这个世界更美好

    相关文章

      网友评论

          本文标题:监听tabBarButton这个私有类的点击方法

          本文链接:https://www.haomeiwen.com/subject/gjztattx.html