//
// ZZComposeController.h
// ZZ_APP主流框架
//
// Created by ZZ_Macpro on 15/10/9.
// Copyright (c) 2015年 ZZ_Macpro. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ZZComposeController : UIViewController
+ (ZZComposeController *)compose;
@end
//
// ZZComposeController.m
// ZZ_APP主流框架
//
// Created by ZZ_Macpro on 15/10/9.
// Copyright (c) 2015年 ZZ_Macpro. All rights reserved.
//
#import "ZZComposeController.h"
#import "ZZTextView.h"
#import "ZZComposeToolbar.h"
#import "ZZComposePhotosView.h"
@interface ZZComposeController () <UITextViewDelegate, ZZComposeToolbarDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, weak) ZZTextView *textView;
@property (nonatomic, weak) ZZComposeToolbar *toolbar;
@property (nonatomic, weak) ZZComposePhotosView *photosView;
@end
@implementation ZZComposeController
+ (ZZComposeController *)compose
{
return [[ZZComposeController alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 设置导航栏属性
[self setupNavBar];
// 添加textView
[self setupTextView];
// 添加toolbar
[self setupToolbar];
// 添加photosView
[self setupPhotosView];
}
/**
* 添加textView
*/
- (void)setupTextView
{
// 1.添加
ZZTextView *textView = [[ZZTextView alloc] init];
textView.font = [UIFont systemFontOfSize:15];
textView.frame = self.view.bounds;
// 垂直方向上永远可以拖拽
textView.alwaysBounceVertical = YES;
textView.delegate = self;
textView.placeholder = @"分享新鲜事...";
[self.view addSubview:textView];
self.textView = textView;
// 2.监听textView文字改变的通知
[ZZNotificationCenter addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:textView];
// 3.监听键盘的通知
[ZZNotificationCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[ZZNotificationCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
/**
* 监听文字改变
*/
- (void)textDidChange
{
self.navigationItem.rightBarButtonItem.enabled = (self.textView.text.length != 0);
}
- (void)dealloc
{
[ZZNotificationCenter removeObserver:self];
}
/**
* 设置导航栏属性
*/
- (void)setupNavBar
{
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"哈哈" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
#pragma warning ---- 为何???
self.navigationItem.rightBarButtonItem.title = @"发送";
self.navigationItem.rightBarButtonItem.enabled = NO;
self.title = @"发微博";
}
/**
* 添加photosView
*/
- (void)setupPhotosView
{
ZZComposePhotosView *photosView = [[ZZComposePhotosView alloc] init];
CGFloat photosW = self.textView.frame.size.width;
CGFloat photosH = self.textView.frame.size.height;
CGFloat photosY = 110;
photosView.frame = CGRectMake(0, photosY, photosW, photosH);
[self.textView addSubview:photosView];
self.photosView = photosView;
}
/**
* 添加toolbar
*/
- (void)setupToolbar
{
ZZComposeToolbar *toolbar = [[ZZComposeToolbar alloc] init];
toolbar.delegate = self;
CGFloat toolbarH = 44;
CGFloat toolbarW = self.view.frame.size.width;
CGFloat toolbarX = 0;
CGFloat toolbarY = self.view.frame.size.height - toolbarH;
toolbar.frame = CGRectMake(toolbarX, toolbarY, toolbarW, toolbarH);
[self.view addSubview:toolbar];
self.toolbar = toolbar;
}
#pragma mark - toolbar的代理方法
- (void)composeToolbar:(ZZComposeToolbar *)toolbar didClickedButton:(ZZComposeToolbarButtonType)buttonType
{
switch (buttonType) {
case ZZComposeToolbarButtonTypeCamera: // 相机
[self openCamera];
break;
case ZZComposeToolbarButtonTypePicture: // 相册
[self openPhotoLibrary];
break;
default:
break;
}
}
/**
* 打开相机
*/
- (void)openCamera
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
/**
* 打开相册
*/
- (void)openPhotoLibrary
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
#pragma mark - 图片选择控制器的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// 1.销毁picker控制器
[picker dismissViewControllerAnimated:YES completion:nil];
// 2.去的图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
[self.photosView addImage:image];
}
/**
* 键盘即将显示的时候调用
*/
- (void)keyboardWillShow:(NSNotification *)note
{
// 1.取出键盘的frame
CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 2.取出键盘弹出的时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 3.执行动画
[UIView animateWithDuration:duration animations:^{
self.toolbar.transform = CGAffineTransformMakeTranslation(0, -keyboardF.size.height);
}];
}
/**
* 键盘即将退出的时候调用
*/
- (void)keyboardWillHide:(NSNotification *)note
{
// 1.取出键盘弹出的时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 2.执行动画
[UIView animateWithDuration:duration animations:^{
self.toolbar.transform = CGAffineTransformIdentity;
}];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self.view endEditing:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.textView becomeFirstResponder];
}
/**
* 取消
*/
- (void)cancel
{
if (self.textView.text.length) {
/**
* 根据用户选择来取
*/
NSUserDefaults *choiceDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *choiceDict = [choiceDefaults objectForKey:@"choiceNot"];
if (!choiceDict) {
UIButton *checkBoxRight = [UIButton buttonWithType:UIButtonTypeCustom];
checkBoxRight.tag = 11;
checkBoxRight.frame = CGRectMake(30.0, 25, 15, 15);
[checkBoxRight setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
[checkBoxRight setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateSelected];
[checkBoxRight addTarget:self action:@selector(clickToChoiceNot:) forControlEvents:UIControlEventTouchUpInside];
UILabel *action = [[UILabel alloc] initWithFrame:CGRectMake(50, 22, 150, 20)];
action.text = NSLocalizedString(@"下次不再提醒", nil);
action.font = [UIFont systemFontOfSize:13];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
[v addSubview:action];
[v addSubview:checkBoxRight];
UIAlertView *CheckView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"确认取消当前编辑", nil) message:@"" delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
[CheckView setValue:v forKey:@"accessoryView"];
[CheckView show];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
/**
* 监听alertView弹框的点击
*/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
} else {
[self dismissViewControllerAnimated:YES completion:^{
}];
}
}
/**
* 选择下次不再提醒
*/
- (void)clickToChoiceNot:(UIButton *)sender
{
if (sender.selected == YES) {
sender.selected = NO;
}
else
sender.selected = YES;
/**
* 存状态,为什么通过字典呢?可以通过监听同一个方法存取多个按钮状态
*/
NSUserDefaults *choiceDefaults = [NSUserDefaults standardUserDefaults];
NSString *str = [NSString stringWithFormat:@"%ld",sender.tag];
NSNumber *num = [NSNumber numberWithBool:sender.selected];
NSDictionary *choiceDict = @{str:num};
[choiceDefaults setObject:choiceDict forKey:@"choiceNot"];
[choiceDefaults synchronize];
}
/**
* 发微博
*/
- (void)send
{
// // 1.创建请求管理对象
// AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//
// // 2.封装请求参数
// NSMutableDictionary *params = [NSMutableDictionary dictionary];
// params[@"status"] = self.textView.text;
// params[@"access_token"] = [IWAccountTool account].access_token;
//
// // 3.发送请求
// [mgr POST:@"https://api.weibo.com/2/statuses/update.json" parameters:params
// success:^(AFHTTPRequestOperation *operation, id responseObject) {
// [MBProgressHUD showSuccess:@"发送成功"];
// } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// [MBProgressHUD showError:@"发送失败"];
// }];
// 4.关闭控制器
[self dismissViewControllerAnimated:YES completion:nil];
}
@end