在上一篇,咱們建立了發微博界面的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圖片