在上一篇,咱们建立了发微博界面的Toolbar,而且能够根据键盘的弹出移动,下面咱们给Toolbar上添加按钮,相机、相册、@、#、表情五个按钮,而后添加点击事件,如咱们须要获取本地的图片发布在微博上,能够给相册添加点击事件,并经过代理(控制器),打开相册库,而后将选中的图片显示到发微博的界面。ide
###一、须要添加的按钮工具
定义toolbar工具条上的按钮类型,能够用枚举。布局
IWComposeToolbar.hatom
// // IWComposeToolbar.h // ItcastWeibo // // Created by kaiyi on 16-5-15. // Copyright (c) 2016年 itcast. All rights reserved. // #import <UIKit/UIKit.h> @class IWComposeToolbar; // 定义toolbar工具条上的按钮类型,五个按钮===*=== typedef enum { IWComposeToolbarButtonTypeCamera, IWComposeToolbarButtonTypePicture, IWComposeToolbarButtonTypeMention, IWComposeToolbarButtonTypeTrend, IWComposeToolbarButtonTypeEmotion, } IWComposeToolbarButtonType; @protocol IWComposeToolbarDelegate <NSObject> @optional -(void)composeToolbar:(IWComposeToolbar *)toolbar didClickedButton:(IWComposeToolbarButtonType)buttonType; @end @interface IWComposeToolbar : UIView @property(weak, nonatomic) id<IWComposeToolbarDelegate> delegate; @end
###二、在初始化工具条的时候,并添加上边的五个按钮代理
IWComposeToolbar.mcode
// // IWComposeToolbar.m // ItcastWeibo // // Created by kaiyi on 16-5-15. // Copyright (c) 2016年 itcast. All rights reserved. // #import "IWComposeToolbar.h" @implementation IWComposeToolbar - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 1.设置工具条背景图片,平铺 self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]]; // self.backgroundColor = [UIColor redColor]; // 2.添加按钮 [self addButtonWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted" tag:IWComposeToolbarButtonTypeCamera]; [self addButtonWithIcon:@"compose_toolbar_picture" highIcon:@"compose_toolbar_picture_highlighted" tag:IWComposeToolbarButtonTypePicture]; [self addButtonWithIcon:@"compose_mentionbutton_background" highIcon:@"compose_mentionbutton_background_highlighted" tag:IWComposeToolbarButtonTypeMention]; [self addButtonWithIcon:@"compose_trendbutton_background" highIcon:@"compose_trendbutton_background_highlighted" tag:IWComposeToolbarButtonTypeTrend]; [self addButtonWithIcon:@"compose_emoticonbutton_background" highIcon:@"compose_emoticonbutton_background_highlighted" tag:IWComposeToolbarButtonTypeEmotion]; } 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{ // NSLog(@"%d", button.tag); 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
###三、具体的打开相册的方法,并写将图片显示在imageView ** IWComposeViewController.m**orm
#import "IWComposeViewController.h" #import "IWTextView.h" #import "AFNetworking.h" #import "IWAccount.h" #import "IWWeiboTool.h" #import "IWAccountTool.h" #import "MBProgressHUD+MJ.h" #import "IWComposeToolbar.h" @interface IWComposeViewController ()<UITextViewDelegate,IWComposeToolbarDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate> @property (nonatomic, weak) IWTextView *textView; @property (nonatomic, weak)IWComposeToolbar *toolbar; @property (nonatomic, weak)UIImageView *imageView; // [new Add 2016-05-27] @end @implementation IWComposeViewController - (void)viewDidLoad { [super viewDidLoad]; // 设置导航栏属性 [self setupNavBar]; // 添加textView [self setupTextView]; // 添加toolbar ****===*** [self setupToolbar]; // 添加imageView (初始化) [new Add 2016-05-27] [self setupImageView]; } /** * 添加imageView */ -(void)setupImageView { UIImageView *imageView = [[UIImageView alloc] init]; // 设置frame imageView.frame = CGRectMake(5, 80, 60, 60); [self.textView addSubview:imageView]; // 赋值属性 self.imageView = imageView; } #pragma mark - toolbar的代理方法 - (void)composeToolbar:(IWComposeToolbar *)toolbar didClickedButton:(IWComposeToolbarButtonType)buttonType { switch (buttonType) { case IWComposeToolbarButtonTypeCamera: // 相机 [self openCamera]; break; case IWComposeToolbarButtonTypePicture: // 相册 NSLog(@"jinqule"); [self openPhotoLibarary]; break; default: break; } } /** * 打开相机 */ - (void)openCamera { UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; ipc.sourceType = UIImagePickerControllerSourceTypeCamera; ipc.delegate = self; [self presentViewController:ipc animated:YES completion:nil]; } /** * 打开相册 */ -(void)openPhotoLibarary{ NSLog(@"打开相册"); UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; ipc.delegate = self; [self presentViewController:ipc animated:YES completion:nil]; } /** * 图片选择控制器的代理 */ -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // 1.销毁picker控制器 [picker dismissViewControllerAnimated:YES completion:nil]; // 2.获取到的图片 UIImage *image = info[UIImagePickerControllerOriginalImage]; // 3.把用户选择的图片显示在发微博编辑框内 self.imageView.image = image; NSLog(@"%@", info); } /** * 添加Toolbar */ -(void)setupToolbar { IWComposeToolbar *toolbar = [[IWComposeToolbar alloc] init]; toolbar.delegate = self; // 点击打开图片库的按钮[2016-05-26 00:45 ADD] // 设置frame CGFloat toolbarH = 44; CGFloat toolbarW = self.view.frame.size.width; // 宽度为view的宽度 CGFloat toolbarX = 0; // 位置,在最左边 CGFloat toolbarY = self.view.frame.size.height - toolbarH; // 无键盘时的toolbar高度 toolbar.frame = CGRectMake(toolbarX, toolbarY, toolbarW, toolbarH); // toolbar的父控件是textview仍是控制器?通过分析新浪微博的toolbar,当键盘消失时,toolbar永远在view的最底部 // 因此,咱们肯定其父控件为控制器 [self.view addSubview:toolbar]; // 赋值属性 self.toolbar = toolbar; }
效果: 事件
注:本博客项目完整源码能够访问个人我的博客获取Corwien博客digtime.cn图片