//
// 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