[iOS微博項目 - 3.1] - 發微博界面

 
A.發微博界面:自定義UITextView
1.需求
  • 用UITextView作一個編寫微博的輸入框
  • 沒有輸入任何文本的時候顯示佔位文本
  • 統一佔位文本和正文的字體
 
2.思路
  • 系統自帶的輸入控件有UITextField和UITextView兩種
  • UITextField:自帶佔位文本屬性,不能換行
  • UITextView:沒有佔位文本屬性,能換行
  • 這裏咱們選擇UITextView進行改造
  • 根據是否輸入文本決定是否顯示佔位文本(placeholder),可能使用代理或通知機制來監聽文本輸入,這裏咱們使用通知。由於設置代理只有一個,設爲view自己不合理,這樣就不能設置控制器代理了。
  • 在UITextView中加入一個UILabel,專門用來顯示placeholder
 
3.實現
自定義一個集成UITextView的控件
帶有一個placeHolder屬性
實現相關方法
  1 //
  2 //  HVWComposeTextView.h
  3 //  HVWWeibo
  4 //
  5 //  Created by hellovoidworld on 15/2/6.
  6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7 //
  8 
  9 #import <UIKit/UIKit.h>
 10 
 11 @interface HVWComposeTextView : UITextView
 12 
 13 /** placeholder 佔位文本 */
 14 @property(nonatomic, copy) NSString *placeHolder;
 15 
 16 @end
 17  
 18 //
 19 //  HVWComposeTextView.m
 20 //  HVWWeibo
 21 //
 22 //  Created by hellovoidworld on 15/2/6.
 23 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
 24 //
 25 
 26 #import "HVWComposeTextView.h"
 27 
 28 @interface HVWComposeTextView()
 29 
 30 @property(nonatomic, strong) UILabel *placeHolderLabel;
 31 
 32 @end
 33 
 34 @implementation HVWComposeTextView
 35 
 36 - (instancetype)initWithFrame:(CGRect)frame {
 37     self = [super initWithFrame:frame];
 38    
 39     if (self) {
 40         // 能夠拖曳
 41         self.scrollEnabled = YES;
 42         self.alwaysBounceVertical = YES;
 43        
 44         // 添加placeHolderLabel
 45         [self setupPlaceHolder];
 46       
 47         // 設置默認字體
 48         [self setFont:[UIFont systemFontOfSize:14]];
 49 
 50         // 設置通知
 51         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputtingText) name:UITextViewTextDidChangeNotification object:self];
 52     }
 53    
 54     return self;
 55 }
 56 
 57 /** 添加placeHolder */
 58 - (void) setupPlaceHolder {
 59     UILabel *placeHolderLabel = [[UILabel alloc] init];
 60     self.placeHolderLabel = placeHolderLabel;
 61    
 62     placeHolderLabel.textColor = [UIColor lightGrayColor];
 63     placeHolderLabel.userInteractionEnabled = NO;
 64     placeHolderLabel.numberOfLines = 0; // 自動換行
 65     placeHolderLabel.backgroundColor = [UIColor clearColor];
 66    
 67     [self addSubview:placeHolderLabel];
 68 }
 69 
 70 /** 設置子控件frame */
 71 - (void)layoutSubviews {
 72     [super layoutSubviews];
 73    
 74     self.placeHolderLabel.x = 5;
 75     self.placeHolderLabel.y = 8;
 76    
 77     NSMutableDictionary *attr = [NSMutableDictionary dictionary];
 78     attr[NSFontAttributeName] = self.placeHolderLabel.font;
 79     CGFloat placeHolderWidth = self.width - 2 * self.placeHolderLabel.x;
 80    
 81     CGRect tempRect = [self.placeHolderLabel.text boundingRectWithSize:CGSizeMake(placeHolderWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil];
 82     self.placeHolderLabel.size = tempRect.size;
 83 }
 84 
 85 - (void)setPlaceHolder:(NSString *)placeHolder {
 86     self.placeHolderLabel.text = placeHolder;
 87    
 88     // 從新計算frame,可能不會當即調用layoutSubviews
 89     [self setNeedsLayout];
 90 }
 91 
 92 /** 重寫setFont,更改正文font的時候也更改placeHolder的font */
 93 - (void)setFont:(UIFont *)font {
 94     [super setFont:font];
 95     self.placeHolderLabel.font = font;
 96    
 97     // 從新計算frame,可能不會當即調用layoutSubviews
 98     [self setNeedsLayout];
 99 }
100 
101 - (void)dealloc {
102     // 註銷通知監聽
103     [[NSNotificationCenter defaultCenter] removeObserver:self];
104 }
105 
106 /** 正在輸入文本 */
107 - (void) inputtingText {
108     if (self.text.length) {
109         self.placeHolderLabel.hidden = YES;
110     } else {
111         self.placeHolderLabel.hidden = NO;
112     }
113 }
114 
115 @end
 
 
在控制器中使用
 1 //  HVWComposeViewController.m
 2 - (void)viewDidLoad {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view.
 5    
 6     // 初始化一些功能按鈕
 7     self.title = @"發微博";
 8    
 9     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"退出" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
10    
11     // 添加自定義UITextView
12     HVWComposeTextView *composeView = [[HVWComposeTextView alloc] init];
13     self.composeView = composeView;
14     composeView.frame = self.view.bounds;
15     composeView.delegate = self;
16    
17     composeView.placeHolder = @"分享點滴精彩阿里山的會計法律考試大姐夫;拉可接受的;浪費空間阿斯頓;離開房間啊;數量的會計法律;阿克蘇交電費;拉可接受的分;垃圾可適當;浪費就卡死;老地方就卡死;懶得看積分;阿里快速的減肥;拉等級考試...";
18    
19     [self.view addSubview:composeView];
20 }

 

 
ComposeTextViewPlaceHolder
 
 
B.使用代理監聽UITextView的拖曳,縮回鍵盤
1 //  HVWComposeViewController.m
2 #pragma mark - UIScrollViewDelegate
3 /** 開始拖曳 */
4 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
5     // 縮回鍵盤
6     [self.composeView resignFirstResponder];
7 }
 
ComposeHideKeyBoard
 
 
整理一下「發微博」控制器代碼:
 1 //
 2 //  HVWComposeViewController.m
 3 //  HVWWeibo
 4 //
 5 //  Created by hellovoidworld on 15/2/3.
 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import "HVWComposeViewController.h"
10 #import "HVWComposeTextView.h"
11 
12 @interface HVWComposeViewController () <UITextViewDelegate, UIScrollViewDelegate>
13 
14 @property(nonatomic, strong) HVWComposeTextView *composeView;
15 
16 @end
17 
18 @implementation HVWComposeViewController
19 
20 - (void)viewDidLoad {
21     [super viewDidLoad];
22     // Do any additional setup after loading the view.
23    
24    
25     // 設置導航欄
26     [self setupNavigationBar];
27    
28     // 添加自定義UITextView
29     [self setupTextView];
30 
31     // 添加工具欄
32     [self setupToolBar];
33 }
34 
35 /** 設置工具欄 */
36 - (void) setupToolBar {
37    
38 }
39 
40 /** 設置輸入控件 */
41 - (void) setupTextView {
42     HVWComposeTextView *composeView = [[HVWComposeTextView alloc] init];
43     self.composeView = composeView;
44     composeView.frame = self.view.bounds;
45     composeView.delegate = self;
46    
47     composeView.placeHolder = @"分享點滴精彩...";
48    
49     [self.view addSubview:composeView];
50 }
51 
52 /** 設置導航欄 */
53 - (void) setupNavigationBar {
54     // 標題
55     self.title = @"發微博";
56    
57     // 導航欄左方按鈕
58     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"退出" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
59 }
60 
61 - (void)viewDidAppear:(BOOL)animated {
62     // 自動彈出鍵盤
63     [self.composeView becomeFirstResponder];
64 }
65 
66 - (void) dismiss {
67     [self.composeView resignFirstResponder];
68     [self dismissViewControllerAnimated:YES completion:nil];
69    
70 }
71 
72 
73 #pragma mark - UIScrollViewDelegate
74 /** 開始拖曳 */
75 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
76     // 縮回鍵盤
77     [self.composeView resignFirstResponder];
78 }
79 
80 @end
 
 
C.鍵盤上方的工具條
1.需求
緊貼在鍵盤上方的工具欄,可隨鍵盤伸縮,始終可視
工具欄上的功能按鈕有:照相機、相冊、提到@、話題、表情
 
2.思路
自定義一個UIView,封裝上述功能
使用代理監聽按鈕點擊
監聽鍵盤彈出縮回通知,使用transform移動工具條
 
3.實現
(1)自定義UIView
 1 //
 2 //  HVWComposeToolBar.h
 3 //  HVWWeibo
 4 //
 5 //  Created by hellovoidworld on 15/2/7.
 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 typedef enum {
12     HVWComposeToolBarButtonTagCamera, // 照相機
13     HVWComposeToolBarButtonTagPhotoLib, // 相冊
14     HVWComposeToolBarButtonTagMention, // 提到@
15     HVWComposeToolBarButtonTagTrend, // 話題
16     HVWComposeToolBarButtonTagEmotion // 表情
17 } HVWComposeToolBarButtonTag;
18 
19 @class HVWComposeToolBar;
20 @protocol HVWComposeToolBarDelegate <NSObject>
21 
22 @optional
23 - (void) composeToolBar:(HVWComposeToolBar *) composeToolBar didButtonClicked:(HVWComposeToolBarButtonTag) tag;
24 
25 @end
26 
27 @interface HVWComposeToolBar : UIView
28 
29 /** 代理 */
30 @property(nonatomic, weak) id<HVWComposeToolBarDelegate> delegate;
31 
32 @end
 
 1 //
 2 //  HVWComposeToolBar.m
 3 //  HVWWeibo
 4 //
 5 //  Created by hellovoidworld on 15/2/7.
 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import "HVWComposeToolBar.h"
10 
11 @implementation HVWComposeToolBar
12 
13 - (instancetype)initWithFrame:(CGRect)frame {
14     self = [super initWithFrame:frame];
15    
16     if (self) {
17         // 背景色
18         self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithNamed:@"compose_toolbar_background"]];
19        
20         // 添加按鈕
21         [self addButtonWithIcon:@"compose_camerabutton_background" highlightedIcon:@"compose_camerabutton_background_highlighted" tag:HVWComposeToolBarButtonTagCamera];
22        
23         [self addButtonWithIcon:@"compose_toolbar_picture" highlightedIcon:@"compose_toolbar_picture_highlighted" tag:HVWComposeToolBarButtonTagPhotoLib];
24        
25         [self addButtonWithIcon:@"compose_mentionbutton_background" highlightedIcon:@"compose_mentionbutton_background_highlighted" tag:HVWComposeToolBarButtonTagMention];
26        
27         [self addButtonWithIcon:@"compose_trendbutton_background" highlightedIcon:@"compose_trendbutton_background_highlighted" tag:HVWComposeToolBarButtonTagTrend];
28        
29         [self addButtonWithIcon:@"compose_emoticonbutton_background" highlightedIcon:@"compose_emoticonbutton_background_highlighted" tag:HVWComposeToolBarButtonTagEmotion];
30     }
31    
32    
33     return self;
34 }
35 
36 /** 添加一個按鈕 */
37 - (void) addButtonWithIcon:(NSString *) icon highlightedIcon:(NSString *) highlightedIcon tag:(HVWComposeToolBarButtonTag) tag {
38     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
39     [button setImage:[UIImage imageWithNamed:icon] forState:UIControlStateNormal];
40     [button setImage:[UIImage imageWithNamed:highlightedIcon] forState:UIControlStateHighlighted];
41     button.tag = tag;
42    
43     // 按鈕點擊事件
44     [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
45    
46     [self addSubview:button];
47 }
48 
49 /** 設置frame */
50 - (void)layoutSubviews {
51     [super layoutSubviews];
52    
53     CGFloat buttonWidth = self.width / self.subviews.count;
54    
55     // 設置每一個按鈕
56     for (int i=0; i<self.subviews.count; i++) {
57         UIButton *button = self.subviews[i];
58        
59         CGFloat buttonHeight = buttonWidth;
60         CGFloat buttonX = i * buttonWidth;
61         CGFloat buttonY = (self.height - buttonHeight) * 0.5;
62         button.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
63     }
64 }
65 
66 /** 按鈕點擊 */
67 - (void) buttonClicked:(UIButton *) button {
68     // 通知代理
69     if ([self.delegate respondsToSelector:@selector(composeToolBar:didButtonClicked:)]) {
70         [self.delegate composeToolBar:self didButtonClicked:button.tag];
71     }
72 }
73 
74 @end
 
(2)在控制器上測試
 1 //  HVWComposeViewController.m
 2 /** 設置工具欄 */
 3 - (void) setupToolBar {
 4     HVWComposeToolBar *toolBar = [[HVWComposeToolBar alloc] init];
 5     toolBar.width = self.view.width;
 6     toolBar.height = 44;
 7     toolBar.delegate = self;
 8    
 9     self.composeView.inputAccessoryView = toolBar;
10 }
 
Image(119)
 
(3)在控制器中調整工具條的位置
不要直接加載鍵盤的inputAccessoryView上,由於須要在鍵盤退出以後仍然顯示在底部
因此獨立設置位置,使用transform隨着鍵盤移動
 1 //  HVWComposeViewController.m
 2 /** 設置工具欄 */
 3 - (void) setupToolBar {
 4     HVWComposeToolBar *toolBar = [[HVWComposeToolBar alloc] init];
 5     self.toolBar = toolBar;
 6     toolBar.width = self.view.width;
 7     toolBar.height = 44;
 8     toolBar.delegate = self;
 9    
10     toolBar.x = 0;
11     // 在底部顯示
12     toolBar.y = self.view.height - toolBar.height;
13    
14     [self.view addSubview:toolBar];
15 }
16 
17 /** 設置輸入控件 */
18 - (void) setupTextView {
19     HVWComposeTextView *composeView = [[HVWComposeTextView alloc] init];
20     self.composeView = composeView;
21     composeView.frame = self.view.bounds;
22     composeView.delegate = self;
23    
24     composeView.placeHolder = @"分享點滴精彩...";
25    
26     [self.view addSubview:composeView];
27    
28     // 監聽鍵盤通知
29     // 鍵盤將彈出
30     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
31    
32     // 鍵盤將縮回
33     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
34    
35     // 添加圖片顯示區
36     [self setupImageDisplayView];
37 }
38  
39 #pragma mark - 鍵盤通知處理
40 /** 鍵盤將彈出 */
41 - (void) keyboardWillShow:(NSNotification *) note {
42     // 鍵盤彈出須要時間
43     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
44    
45     // 移動工具條
46     [UIView animateWithDuration:duration animations:^{
47         // 獲取鍵盤高度
48         CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
49         CGFloat keyboardHeight = keyboardFrame.size.height;
50        
51         self.toolBar.transform = CGAffineTransformMakeTranslation(0, -1 * keyboardHeight);
52     }];
53 }
54 
55 /** 鍵盤將縮回 */
56 - (void) keyboardWillHide:(NSNotification *) note {
57     // 鍵盤縮回須要時間
58     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
59    
60     // 移動工具條
61     [UIView animateWithDuration:duration animations:^{
62         self.toolBar.transform = CGAffineTransformIdentity;
63     }];
64 }

 

 
ComposeToolBar
 
D.相機 & 相冊 選擇圖片功能
1.需求
點擊「相機」/「相冊」進入相應界面,完成後把選擇的圖片顯示在輸入框
 
2.思路
使用UIImagePickerController打開相機/相冊
使用picker的代理方法,在完成選擇後獲取圖片
建立一個UIView用來裝載圖片,而後放在輸入框內
 
3.實現
 1 //
 2 //  HVWComposeImageDisplayView.h
 3 //  HVWWeibo
 4 //
 5 //  Created by hellovoidworld on 15/2/7.
 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface HVWComposeImageDisplayView : UIView
12 
13 - (void) addImage:(UIImage *) image;
14 
15 @end
 
 1 //
 2 //  HVWComposeImageDisplayView.m
 3 //  HVWWeibo
 4 //
 5 //  Created by hellovoidworld on 15/2/7.
 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import "HVWComposeImageDisplayView.h"
10 
11 #define MaxColumn 4
12 
13 @implementation HVWComposeImageDisplayView
14 
15 /** 添加圖片 */
16 - (void) addImage:(UIImage *) image {
17     HVWLog(@"addImage");
18     UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
19     imageView.contentMode = UIViewContentModeScaleAspectFit;
20    
21     [self addSubview:imageView];
22    
23     [self setNeedsDisplay];
24 }
25 
26 /** 設置frame */
27 - (void)layoutSubviews {
28     [super layoutSubviews];
29    
30     UIImageView *imageView = [self.subviews lastObject];
31     int index = self.subviews.count - 1;
32     // 所在列
33     int column = index % MaxColumn;
34     // 所在行
35     int row = index / MaxColumn;
36    
37     CGFloat margin = 10;
38     CGFloat imageWidth = (self.width - (MaxColumn + 1) * margin) / MaxColumn;
39     CGFloat imageHeight = imageWidth;
40     CGFloat imageX = column * (imageWidth + margin) + margin;
41     CGFloat imageY = row * (imageHeight + margin);
42    
43     imageView.frame = CGRectMake(imageX, imageY, imageWidth, imageHeight);
44 }
45 
46 @end

 

 
  1 //
  2 //  HVWComposeViewController.m
  3 //  HVWWeibo
  4 //
  5 //  Created by hellovoidworld on 15/2/3.
  6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7 //
  8 
  9 #import "HVWComposeViewController.h"
 10 #import "HVWComposeTextView.h"
 11 #import "HVWComposeToolBar.h"
 12 #import "HVWComposeImageDisplayView.h"
 13 
 14 @interface HVWComposeViewController () <UITextViewDelegate, UIScrollViewDelegate, HVWComposeToolBarDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
 15 
 16 /** 輸入框 */
 17 @property(nonatomic, strong) HVWComposeTextView *composeView;
 18 
 19 /** 工具條 */
 20 @property(nonatomic, strong) HVWComposeToolBar *toolBar;
 21 
 22 /** 圖片顯示區 */
 23 @property(nonatomic, strong) HVWComposeImageDisplayView *imageDisplayView;
 24 
 25 @end
 26 
 27 @implementation HVWComposeViewController
 28 
 29 - (void)viewDidLoad {
 30     [super viewDidLoad];
 31     // Do any additional setup after loading the view.
 32    
 33    
 34     // 設置導航欄
 35     [self setupNavigationBar];
 36    
 37     // 添加自定義UITextView
 38     [self setupTextView];
 39 
 40     // 添加工具欄
 41     [self setupToolBar];
 42 }
 43 
 44 /** 設置工具欄 */
 45 - (void) setupToolBar {
 46     HVWComposeToolBar *toolBar = [[HVWComposeToolBar alloc] init];
 47     self.toolBar = toolBar;
 48     toolBar.width = self.view.width;
 49     toolBar.height = 44;
 50     toolBar.delegate = self;
 51    
 52     toolBar.x = 0;
 53     // 在底部顯示
 54     toolBar.y = self.view.height - toolBar.height;
 55    
 56     [self.view addSubview:toolBar];
 57 }
 58 
 59 /** 設置輸入控件 */
 60 - (void) setupTextView {
 61     HVWComposeTextView *composeView = [[HVWComposeTextView alloc] init];
 62     self.composeView = composeView;
 63     composeView.frame = self.view.bounds;
 64     composeView.delegate = self;
 65    
 66     composeView.placeHolder = @"分享點滴精彩...";
 67    
 68     [self.view addSubview:composeView];
 69    
 70     // 監聽鍵盤通知
 71     // 鍵盤將彈出
 72     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
 73    
 74     // 鍵盤將縮回
 75     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
 76    
 77     // 添加圖片顯示區
 78     [self setupImageDisplayView];
 79 }
 80 
 81 /** 設置導航欄 */
 82 - (void) setupNavigationBar {
 83     // 標題
 84     self.title = @"發微博";
 85    
 86     // 導航欄左方按鈕
 87     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"退出" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
 88 }
 89 
 90 /** 添加圖片顯示區 */
 91 - (void) setupImageDisplayView {
 92     HVWComposeImageDisplayView *imageDisplayView = [[HVWComposeImageDisplayView alloc] init];
 93     imageDisplayView.size = self.composeView.size;
 94     imageDisplayView.x = 0;
 95     imageDisplayView.y = 100;
 96    
 97     self.imageDisplayView = imageDisplayView;
 98    
 99     [self.composeView addSubview:imageDisplayView];
100 }
101 
102 - (void)viewDidAppear:(BOOL)animated {
103     // 自動彈出鍵盤
104     [self.composeView becomeFirstResponder];
105 }
106 
107 - (void) dismiss {
108     [self.composeView resignFirstResponder];
109     [self dismissViewControllerAnimated:YES completion:nil];
110    
111 }
112 
113 
114 #pragma mark - UIScrollViewDelegate
115 /** 開始拖曳 */
116 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
117     // 縮回鍵盤
118     [self.composeView resignFirstResponder];
119 }
120 
121 #pragma mark - HVWComposeToolBarDelegate
122 /** 工具欄的按鈕被點擊了 */
123 - (void)composeToolBar:(HVWComposeToolBar *)composeToolBar didButtonClicked:(HVWComposeToolBarButtonTag)tag {
124     // 判斷哪一個按鈕被點擊
125     switch (tag) {
126         case HVWComposeToolBarButtonTagCamera: // 相機
127             [self openCamera];
128             break;
129         case HVWComposeToolBarButtonTagPhotoLib: // 相冊
130             [self openAlbum];
131             break;
132         case HVWComposeToolBarButtonTagMention: // 提到@
133            
134             break;
135         case HVWComposeToolBarButtonTagTrend: // 話題
136            
137             break;
138         case HVWComposeToolBarButtonTagEmotion: // 表情
139            
140             break;
141         default:
142             break;
143     }
144 }
145 
146 /** 打開相機 */
147 - (void) openCamera {
148     UIImagePickerController *picker = [[UIImagePickerController alloc] init];
149     picker.sourceType = UIImagePickerControllerSourceTypeCamera;
150     picker.delegate = self;
151    
152     [self presentViewController:picker animated:YES completion:nil];
153 }
154 
155 /** 打開相冊 */
156 - (void) openAlbum {
157     UIImagePickerController *picker = [[UIImagePickerController alloc] init];
158     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
159     picker.delegate = self;
160    
161     [self presentViewController:picker animated:YES completion:nil];
162 }
163 
164 #pragma mark - UIImagePickerControllerDelegate
165 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
166     // 取得原圖
167     UIImage *image = info[UIImagePickerControllerOriginalImage];
168     [self.imageDisplayView addImage:image];
169    
170     [picker dismissViewControllerAnimated:YES completion:nil];
171 }
172 
173 #pragma mark - 鍵盤通知處理
174 /** 鍵盤將彈出 */
175 - (void) keyboardWillShow:(NSNotification *) note {
176     // 鍵盤彈出須要時間
177     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
178    
179     // 移動工具條
180     [UIView animateWithDuration:duration animations:^{
181         // 獲取鍵盤高度
182         CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
183         CGFloat keyboardHeight = keyboardFrame.size.height;
184        
185         self.toolBar.transform = CGAffineTransformMakeTranslation(0, -1 * keyboardHeight);
186     }];
187 }
188 
189 /** 鍵盤將縮回 */
190 - (void) keyboardWillHide:(NSNotification *) note {
191     // 鍵盤縮回須要時間
192     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
193    
194     // 移動工具條
195     [UIView animateWithDuration:duration animations:^{
196         self.toolBar.transform = CGAffineTransformIdentity;
197     }];
198 }
199 
200 @end

 

 
ComposeImagePicker
相關文章
相關標籤/搜索