iOS学习之----------自定义工具条

来源:互联网 发布:矢量软件是什么 编辑:程序博客网 时间:2024/04/30 04:26

在某些时候我们输入文字后可能需要加入一些图标,图片等其他形式内容,因此要在UITextView或UITextField上添加工具条,进行操作

设计原则:将需要添加的工具都设置为button并放在一个UIView上,将UITextView或UITextField 的inputAccessoryView 属性设置成该View即可显示。

1、定义toolBar类对其进行封装,外部只能对其创建 不能修改 ,而且 当该类里面的内容发生改变的时候,外部不会受影响

设置背景颜色 通过一张很小的图片,在整个View上平铺这张图片

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

2、添加子控件

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

//调用这个方法创建button并设置button在不同点击状态下的图片

- (void)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(yzComposeToolBarButtonType)tag
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
    [button setImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    button.tag = tag;
}

3、点击button的响应事件

  1》当点击button后控制器获取该点击事件,因此需要用代理,控制器成为toolbar的代理获得点击事件。

  2》控制器还需要知道点击了toolBar中的哪个button  因此需要对点击的button进行判断 ,控制器可以通过重写代理方法并根据参数判断是哪个button,这里使用枚举的方式,在创建button的时候  给每个button设置一个枚举值  ,控制器重写方法后,可以根据传出的buttonType判断点击了哪个按钮,而且在ToolBar类中  发生的改变 控制器不用去关心

枚举

typedef enum {
    yzComposeToolBarButtonTypeCamera,
    yzComposeToolBarButtonTypeEmoticon,//表情
    yzComposeToolBarButtonTypeMention,
    yzComposeToolBarButtonTypePicture,
    yzComposeToolBarButtonTypeTrend,//话题

}yzComposeToolBarButtonType;

@class yzComposeToolBar;

代理

@protocol yzComposeToolBarDelegate <NSObject>
@optional
- (void)composeToolBar:(yzComposeToolBar *)toolBar DidClickedButton:(yzComposeToolBarButtonType)buttonType;
@end
@interface yzComposeToolBar : UIView
@property (nonatomic,weak)id<yzComposeToolBarDelegate>delegate;
@end

button的点击事件

- (void)buttonOnClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(composeToolBar:DidClickedButton:)]) {
        [self.delegate composeToolBar:self DidClickedButton:button.tag];
    }
}

4、控制器中创建toolbar 并设置代理

- (void)setupToolBar
{
    //创建
    yzComposeToolBar *toolBar = [[yzComposeToolBar alloc]init];
    toolBar.width = self.view.width;
    toolBar.height = 44;
    toolBar.delegate = self;
    self.textView.inputAccessoryView = toolBar;
}

控制器对button点击事件的响应和判断

#pragma mark -- 代理方法
- (void)composeToolBar:(yzComposeToolBar *)toolBar DidClickedButton:(yzComposeToolBarButtonType)buttonType
{
    switch (buttonType) {
        case yzComposeToolBarButtonTypeCamera:
            NSLog(@"照相机");
            break;
        case yzComposeToolBarButtonTypeEmoticon:
            NSLog(@"表情");
            break;
        case yzComposeToolBarButtonTypeMention:
            NSLog(@"");
            break;
        case yzComposeToolBarButtonTypePicture:
            NSLog(@"");
            break;
        case yzComposeToolBarButtonTypeTrend:
            NSLog(@"");
            break;
    }
}



0 0
原创粉丝点击