iOS开发-自定义一个发状态的工具栏toolbar(类似QQ和微博的发状态的工具栏)

//

//  ZZComposeToolbar.h

//  ZZ_APP主流框架

//

//  Created by ZZ_Macpro on 15/10/12.

//  Copyright (c) 2015年 ZZ_Macpro. All rights reserved.

//

 

#import <UIKit/UIKit.h>

@class ZZComposeToolbar;

 

typedef enum {

    ZZComposeToolbarButtonTypeCamera,

    ZZComposeToolbarButtonTypePicture,

    ZZComposeToolbarButtonTypeMention,

    ZZComposeToolbarButtonTypeTrend,

    ZZComposeToolbarButtonTypeEmotion

} ZZComposeToolbarButtonType;

 

@protocol ZZComposeToolbarDelegate <NSObject>

@optional

- (void)composeToolbar:(ZZComposeToolbar *)toolbar didClickedButton:(ZZComposeToolbarButtonType)buttonType;

@end

 

@interface ZZComposeToolbar : UIView

@property (weak, nonatomic) id<ZZComposeToolbarDelegate> delegate;

 

@end

 

 

//

//  ZZComposeToolbar.m

//  ZZ_APP主流框架

//

//  Created by ZZ_Macpro on 15/10/12.

//  Copyright (c) 2015年 ZZ_Macpro. All rights reserved.

//

 

#import "ZZComposeToolbar.h"

 

@implementation ZZComposeToolbar

 

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // 1.设置背景

        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]];

        

        // 2.添加按钮

        [self addButtonWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted" tag:ZZComposeToolbarButtonTypeCamera];

        [self addButtonWithIcon:@"compose_toolbar_picture" highIcon:@"compose_toolbar_picture_highlighted" tag:ZZComposeToolbarButtonTypePicture];

        [self addButtonWithIcon:@"compose_mentionbutton_background" highIcon:@"compose_mentionbutton_background_highlighted" tag:ZZComposeToolbarButtonTypeMention];

        [self addButtonWithIcon:@"compose_trendbutton_background" highIcon:@"compose_trendbutton_background_highlighted" tag:ZZComposeToolbarButtonTypeTrend];

        [self addButtonWithIcon:@"compose_emoticonbutton_background" highIcon:@"compose_emoticonbutton_background_highlighted" tag:ZZComposeToolbarButtonTypeEmotion];

    }

    return self;

}

 

- (void)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(int)tag

{

    UIButton *button = [[UIButton alloc] init];

    button.tag = tag;

    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    [button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];

    [button setImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted];

    [self addSubview:button];

}

 

/**

 *  监听按钮点击

 */

- (void)buttonClick:(UIButton *)button

{

    if ([self.delegate respondsToSelector:@selector(composeToolbar:didClickedButton:)]) {

        [self.delegate composeToolbar:self didClickedButton:button.tag];

    }

}

 

- (void)layoutSubviews

{

    [super layoutSubviews];

    

    CGFloat buttonW = self.frame.size.width / self.subviews.count;

    CGFloat buttonH = self.frame.size.height;

    for (int i = 0; i<self.subviews.count; i++) {

        UIButton *button = self.subviews[i];

        CGFloat buttonX = buttonW * i;

        button.frame = CGRectMake(buttonX, 0, buttonW, buttonH);

    }

}

@end

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值