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

来源:互联网 发布:手机淘宝apk下载 编辑:程序博客网 时间:2024/06/05 01:57

//

//  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 = [superinitWithFrame:frame];

   if (self) {

        // 1.设置背景

        self.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageWithName:@"compose_toolbar_background"]];

        

        // 2.添加按钮

        [selfaddButtonWithIcon:@"compose_camerabutton_background"highIcon:@"compose_camerabutton_background_highlighted"tag:ZZComposeToolbarButtonTypeCamera];

        [selfaddButtonWithIcon:@"compose_toolbar_picture"highIcon:@"compose_toolbar_picture_highlighted"tag:ZZComposeToolbarButtonTypePicture];

        [selfaddButtonWithIcon:@"compose_mentionbutton_background"highIcon:@"compose_mentionbutton_background_highlighted"tag:ZZComposeToolbarButtonTypeMention];

        [selfaddButtonWithIcon:@"compose_trendbutton_background"highIcon:@"compose_trendbutton_background_highlighted"tag:ZZComposeToolbarButtonTypeTrend];

        [selfaddButtonWithIcon:@"compose_emoticonbutton_background"highIcon:@"compose_emoticonbutton_background_highlighted"tag:ZZComposeToolbarButtonTypeEmotion];

    }

    return self;

}


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

{

   UIButton *button = [[UIButtonalloc] init];

    button.tag = tag;

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

    [button setImage:[UIImageimageWithName:icon] forState:UIControlStateNormal];

    [button setImage:[UIImageimageWithName:highIcon] forState:UIControlStateHighlighted];

    [selfaddSubview:button];

}


/**

 *  监听按钮点击

 */

- (void)buttonClick:(UIButton *)button

{

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

        [self.delegatecomposeToolbar:selfdidClickedButton:button.tag];

    }

}


- (void)layoutSubviews

{

    [superlayoutSubviews];

    

    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



1 0
原创粉丝点击