美文网首页
两个标题的跑马灯第一篇,类似支付宝口碑的封装

两个标题的跑马灯第一篇,类似支付宝口碑的封装

作者: f2efa87f6528 | 来源:发表于2017-06-27 10:43 被阅读8次

    封装一个view 有上下label 代理回调

    @protocol ContentViewDegate <NSObject>
    
    - (void)UpLabelClick:(NSString *)UpLabelTitle;
    - (void)DownLabelClick:(NSString *)DownLabelTitle;
    
    @end
    
    @interface ContentView : UIView
    
    //上label赋值
    - (void)settingUpLabel:(NSString *)UpTitle;
    
    //下label赋值
    - (void)settingDownLabel:(NSString *)DownTitle;
    //代理
    @property (nonatomic ,weak)id <ContentViewDegate>delegate;
    
    #import "Masonry.h"
    @interface ContentView ()
    @property (nonatomic ,strong)UILabel *showLabelUp; //上面label
    @property (nonatomic ,strong)UILabel *showLabelDown; //下面label
    @end
    
    
    @implementation ContentView
    
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            [self setUpUI];
            
            self.userInteractionEnabled =YES;
        }
        return self;
    }
    
    
    - (void)setUpUI
    {
        [self addSubview:self.showLabelUp];
        
        [self.showLabelUp mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.top.equalTo(self).offset(10);
            make.height.mas_equalTo(20);
        }];
        
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(UpLabelClick)];
        [self.showLabelUp addGestureRecognizer:tap1];
        
        
        [self addSubview:self.showLabelDown];
        
        [self.showLabelDown mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.top.equalTo(self.showLabelUp.mas_bottom).offset(10);
            make.height.mas_equalTo(20);
        }];
        
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(DownLabelClick)];
        [self.showLabelDown addGestureRecognizer:tap2];
    }
    
    //上label赋值
    - (void)settingUpLabel:(NSString *)UpTitle
    {
        self.showLabelUp.text = UpTitle;
        
    }
    
    //下label赋值
    - (void)settingDownLabel:(NSString *)DownTitle
    {
        
        self.showLabelDown.text = DownTitle;
    }
    
    
    - (UILabel *)showLabelUp
    {
        if (!_showLabelUp) {
            
            _showLabelUp = [[UILabel alloc]init];
            _showLabelUp.textColor = [UIColor blackColor];
            _showLabelUp.font = [UIFont systemFontOfSize:13];
            _showLabelUp.backgroundColor = [UIColor blueColor];
             _showLabelUp.userInteractionEnabled =YES;
        }
    
        return _showLabelUp;
    }
    
    - (UILabel *)showLabelDown
    {
        if (!_showLabelDown) {
            
            _showLabelDown = [[UILabel alloc]init];
            _showLabelDown.textColor = [UIColor blackColor];
            _showLabelDown.font = [UIFont systemFontOfSize:13];
            _showLabelDown.backgroundColor = [UIColor redColor];
            _showLabelDown.userInteractionEnabled =YES;
        
        }
        
        return _showLabelDown;
    }
    
    
    #pragma mark -点击方法
    - (void)UpLabelClick
    {
        if (_delegate && [_delegate respondsToSelector:@selector(UpLabelClick:)]) {
            
            [_delegate UpLabelClick:self.showLabelUp.text];
        }
    }
    
    - (void)DownLabelClick
    {
        
        if (_delegate && [_delegate respondsToSelector:@selector(DownLabelClick:)]) {
            
            [_delegate DownLabelClick:self.showLabelDown.text];
        }
    }
    

    相关文章

      网友评论

          本文标题:两个标题的跑马灯第一篇,类似支付宝口碑的封装

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