[iOS微博项目 - 3.1] - 发微博界面

github: https://github.com/hellovoidworld/HVWWeibo

 

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 }

B.使用代理监听UITextView的拖曳,缩回键盘

1 //  HVWComposeViewController.m
2 #pragma mark - UIScrollViewDelegate
3 /** 开始拖曳 */
4 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
5     // 缩回键盘
6     [self.composeView resignFirstResponder];
7 }

整理一下“发微博”控制器代码:

 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 }

(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 }

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

时间: 2024-08-02 16:48:47

[iOS微博项目 - 3.1] - 发微博界面的相关文章

iOS开发项目篇—33发微博

iOS开发项目篇—33发微博 一.简单说明 1.发送按钮 当textView的文字发生改变(有内容)的时候,设置导航栏右侧的按钮为可点击的. 说明:监听内容的改变,既可以使用通知来实现,也可以使用代理来实现(下面使用的是代理的方式) 代码说明: 1 #pragma mark-设置代理方法 2 /** 3 *当textView的内容改变的时候,通知导航栏“发送”按钮为可用 4 */ 5 -(void)textViewDidChange:(UITextView *)textView 6 { 7 se

iOS开发项目篇—36封装微博业务

iOS开发项目篇—36封装微博业务 一.简单说明 1.请求参数面向模型 2.请求结果面向模型 3.对控制器来说应该屏蔽业务细节.不让控制器关心(知道)业务细节,它只需要知道自己在做某个业务 @通过一个专门的业务处理类:处理微博业务细节 说明: 业务:加载新的微博首页数据 实现:给新浪服务器发送一个GET请求 业务:加载更多的首页微博数据 实现1:给新浪服务器发送一个GET请求 实现2:去沙盒中加载以前离线缓存的微博数据  二.实现 1.新建一个微博业务处理类,继承自NSObject 微博业务处理

[iOS微博项目 - 3.2] - 发送微博

github: https://github.com/hellovoidworld/HVWWeibo A.使用微博API发送微博 1.需求 学习发送微博API 发送文字微博 发送带有图片的微博 2.思路 直接按照微博API的文档指示使用 这里测试上传带图片微博 3.实现 在"发微博"界面,点击右上角发送就调用API 1 // HVWComposeViewController.m 2 /** 发送微博 */ 3 - (void) sendWeibo { 4 if (self.compos

[iOS微博项目 - 4.0] - 自定义微博cell

github: https://github.com/hellovoidworld/HVWWeibo A.自定义微博cell基本结构 1.需求 创建自定义cell的雏形 cell包含:内容.工具条 内容包含:原创内容.转发内容 2.思路 使用分层控件,逐层实现 分离model和view model:数据模型.frame模型 view:就是控件本身 frame模型:包含数据模型和子控件frame 根据数据模型来决定子控件是否显示(例如转发内容) cell的view设计雏形: 控件的成员属性层次:

猫猫学iOS 之微博项目实战(12)发送微博自定义工具条代理实现点击事件

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 二:封装好的工具条 NYComposeToolbar.h 带代理方法 #import <UIKit/UIKit.h> typedef enum { NYComposeToolbarButtonTypeCamera, // 拍照 NYComposeToolbarButtonTypePicture, // 相册 NYComposeToolbarButto

[iOS微博项目 - 2.6] - 获取微博数据

github: https://github.com/hellovoidworld/HVWWeibo   A.新浪获取微博API 1.读取微博API 2.“statuses/home_timeline”接口 B.在app中获取微博数据 1.在“首页”控制器发送请求,获取json数据 1 /** 加载微博数据 */ 2 - (void) loadWeiboData { 3 // 创建AFNetworking的http操作中管理器 4 AFHTTPRequestOperationManager *m

猫猫学iOS 之微博项目实战(13)发送微博调用相机里面的图片以及调用相机

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 二:代码 相机部分就简单多了,几行代码调用而已,但是如果你要是想实现更多丰富的功能,需要自己写.利用AssetsLibrary.framework,利用这个框架可以获得手机上的所有相册图片,写一个图片选择控制器. - (void)openCamera { [self openImagePickerController:UIImagePickerCont

iOS开发——项目需求-快速回到当前界面的顶部

利用UIWindow实现快速到达顶部 如下图,在状态栏添加一个没有颜色的UIWindow(里面添加一个按钮),实现点击这个按钮时能快速的回到当前界面的顶部 核心代码 一.利用UIWindow实现到达顶部1.创建一个新的窗口 1)UIWindow的级别,级别越高,越显示在上层(级别高的window盖在级别低的window上面) UIWindowLevelNormal < UIWindowLevelStatusBar < UIWindowLevelAlert @interface AppDeleg

iOS微博项目(七)发微博和定位

class a导入class b,class b导入class a 会出现错误 如果keyboard不显示,应该是hardware->connect选择了 1. 发微博UI 2.定位 一直不回调,后来发现是ios8后方法更新了:http://blog.devzeng.com/blog/ios8-corelocation-framework.html 1)[_locationManager requestAlwaysAuthorization];//添加这句2)在Info.plist文件中添加如下