iOS之 KVO实例代码应用

//

//  JSTabBarButton.m

//  JSmaster

//

//  Created by jyd on 14/12/25.


#import "JSTabBarButton.h"

#import "JSBadgeButton.h"


#define JSImageRatio 0.6


@interface JSTabBarButton()


@property (nonatomic, weak) JSBadgeButton *badgeButton;


@end


@implementation JSTabBarButton


-(instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    

    if (self) {

        //设置一些初始化状态

        self.imageView.contentMode    = UIViewContentModeCenter;

        self.titleLabel.textAlignment = NSTextAlignmentCenter;

        self.titleLabel.font = [UIFont systemFontOfSize:13];

        [self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

        [self setTitleColor:COLOR_THEME_GREEN forState:UIControlStateSelected];

        

        //添加自定义的badgeButton

        [self setupBadgeButton];

    }

    

    return self;

}


//添加提醒文字

-(void)setupBadgeButton

{

    JSBadgeButton *badgeButton = [[JSBadgeButton alloc]init];

    badgeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;

    self.badgeButton = badgeButton;

    //[self addSubview:badgeButton];

}


-(void)setHighlighted:(BOOL)highlighted

{

    

}


/**

 *  设定自定义的按钮的图片尺寸、位置

 */

-(CGRect)imageRectForContentRect:(CGRect)contentRect

{

    CGFloat imageX = 0;

    CGFloat imageY = 0;

    CGFloat imageW = contentRect.size.width;

    CGFloat imageH = contentRect.size.height * JSImageRatio;

    

    return CGRectMake(imageX, imageY, imageW, imageH);

}



/**

 *  设定自定义的按钮的title的尺寸、位置

 */

-(CGRect)titleRectForContentRect:(CGRect)contentRect

{

    CGFloat titleX = 0;

    CGFloat titleY = contentRect.size.height * JSImageRatio;

    CGFloat titleW = contentRect.size.width;

    CGFloat titleH = contentRect.size.height - titleY;

    

    return CGRectMake(titleX, titleY, titleW, titleH);

}


-(void)setItem:(UITabBarItem *)item

{

    _item = item;

    

    //KVO事件监听

    [item addObserver:self forKeyPath:@"badgeValue" options:0 context:nil];

    [item addObserver:self forKeyPath:@"imageName" options:0 context:nil];

    [item addObserver:self forKeyPath:@"selectedImageName" options:0 context:nil];

    [item addObserver:self forKeyPath:@"title" options:0 context:nil];

    

    //初始化调用,为添加的自定义按钮设置属性

    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];

    

}


-(void)dealloc

{

    [self.item removeObserver:self forKeyPath:@"badgeValue"];

    [self.item removeObserver:self forKeyPath:@"imageName"];

    [self.item removeObserver:self forKeyPath:@"selectedImageName"];

    [self.item removeObserver:self forKeyPath:@"title"];

}


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

    //1.设置文字

    [self setTitle:self.item.title forState:UIControlStateNormal];

    [self setTitle:self.item.title forState:UIControlStateHighlighted];

    //2.设置图片

     [self setImage:self.item.image forState:UIControlStateNormal];

     [self setImage:self.item.selectedImage forState:UIControlStateSelected];

    //3.设置提醒数字

     self.badgeButton.badgeValue = self.item.badgeValue;

    //4.设置位置和尺寸

     CGFloat badgeY = 5;

     CGFloat badgeX = self.frame.size.width - self.badgeButton.frame.size.width - 10;

     

     CGRect badgeFrame = self.badgeButton.frame;

     badgeFrame.origin.x = badgeX;

     badgeFrame.origin.y = badgeY;

     self.badgeButton.frame = badgeFrame;

    

}



@end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值