// MyPlusController.h
// 新浪微博
//
// Created by jose on 15-3-26.
// Copyright (c) 2015年 jose. All rights reserved.
// Plus控制器
#import <UIKit/UIKit.h>
#import "MyTextToolBar.h"
@interface MyPlusController :UIViewController<UITextViewDelegate,MyTextToolBarDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@end
********************************************************************************************************************************************************
*******************************************************************************************************************************************************
*******************************************************************************************************************************************************
//
// MyPlusController.m
// 新浪微博
//
// Created by jose on 15-3-26.
// Copyright (c) 2015年 jose. All rights reserved.
//
#import "MyPlusController.h"
#import "MyTextView.h"
#import "MyTextToolBar.h"
#import "UIView+Extension.h"
#import "Global.h"
#import "MyPictureTool.h"
#include "AFNetworking.h"
#import "MyAccountTool.h"
#import "MyAccountModel.h"
#import "MBProgressHUD.h"
@interface MyPlusController ()
@property(nonatomic,weak)MyTextView *mytextview;
@property(nonatomic,strong)MyTextToolBar *mytoolbar;
@property(nonatomic,strong)MyPictureTool *mypicture;
@end
@implementation MyPlusController
- (void)viewDidLoad {
[super viewDidLoad];
[self SetNavigation];
[self SetInputView];
[self SetToolBarView];
[self SetPicture];
//监听键盘弹出
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//监听键盘隐藏
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//设置文本框的工具栏
-(void)SetToolBarView{
MyTextToolBar *toolbar=[[MyTextToolBar alloc]init];
toolbar.width=self.view.width;
toolbar.height=40;
toolbar.delegate=self;
self.mytoolbar=toolbar;
toolbar.y=ScreenBounds.size.height-40;
[self.view addSubview:toolbar];
}
//设置输入文本框
-(void)SetInputView{
//创建输入文本框
MyTextView *textview=[[MyTextView alloc]init];
textview.frame=self.view.bounds;
self.mytextview=textview;
textview.delegate=self;
textview.placeholder=@"趣多多";
textview.placeholdercolor=[UIColor grayColor];
textview.font=[UIFont systemFontOfSize:18];
//设置垂直方向具有弹簧效果
textview.alwaysBounceVertical=YES;
[textview becomeFirstResponder];
[textview.layer setCornerRadius:10];
[self.view addSubview:textview];
}
//设置导航条
-(void)SetNavigation{
self.title=@"发消息";
self.view.backgroundColor=[UIColor whiteColor];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"取消 " style: UIBarButtonItemStylePlain target:self action:@selector(Cancel)];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"发消息" style: UIBarButtonItemStylePlain target:self action:@selector(Send)];
self.navigationItem.rightBarButtonItem.enabled=NO;
}
//设置图片显示
-(void)SetPicture{
MyPictureTool *picture=[[MyPictureTool alloc]init];
picture.width=self.mytextview.width;
picture.height=self.mytextview.height;
picture.y=50;
self.mypicture=picture;
[self.mytextview addSubview:picture];
}
//打开照相机
-(void)OpenCamera{
//如果不能打开,则返回
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
UIImagePickerController *camera=[[UIImagePickerController alloc]init];
camera.sourceType=UIImagePickerControllerSourceTypeCamera;
camera.delegate=self;
[self presentViewController:camera animated:YES completion:nil];
}
//打开相册
-(void)OpenPicture{
//如果不能打开,则返回
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
UIImagePickerController *picture=[[UIImagePickerController alloc]init];
picture.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
picture.delegate=self;
[self presentViewController:picture animated:YES completion:nil];
}
//发送
-(void)Send{
if (_mypicture.SaveImages.count) {
[self SendWeiboImage];
}
else{
[self SendWeibo];
}
//推出控制器
//[self dismissViewControllerAnimated:YES completion:nil];
}
//取消
-(void)Cancel{
[self.mytextview resignFirstResponder];
[self dismissViewControllerAnimated:YES completion:nil];
}
//发送文本消息
-(void)SendWeibo{
MBProgressHUD *hud=[[MBProgressHUD alloc]initWithView:self.view];
hud.mode=MBProgressHUDModeText;
AFHTTPRequestOperationManager *mgr=[AFHTTPRequestOperationManager manager];
NSMutableDictionary *params=[NSMutableDictionary dictionary];
params[@"access_token"]=[MyAccountTool ReadAccount].access_token;
params[@"status"]=_mytextview.text;
[mgr POST:@"https://api.weibo.com/2/statuses/update.json" parameters:params success:^(AFHTTPRequestOperation *operation,NSDictionary *response){
hud.labelText=@"发送成功";
[self.view addSubview:hud];
[hud showAnimated:YES whileExecutingBlock:^{
sleep(1);
[self dismissViewControllerAnimated:YES completion:nil];
} completionBlock:^{
[hud removeFromSuperview];
}];
} failure:^(AFHTTPRequestOperation *operation,NSError *error){
hud.labelText=@"发送失败";
[self.view addSubview:hud];
[hud showAnimated:YES whileExecutingBlock:^{
sleep(1);
[self dismissViewControllerAnimated:YES completion:nil];
} completionBlock:^{
[hud removeFromSuperview];
}];
NSLog(@"%@",error);
}];
}
//发送微博图片和文本
-(void)SendWeiboImage{
MBProgressHUD *hud=[[MBProgressHUD alloc]initWithView:self.view];
hud.mode=MBProgressHUDModeText;
AFHTTPRequestOperationManager *mgr=[AFHTTPRequestOperationManager manager];
NSMutableDictionary *params=[NSMutableDictionary dictionary];
params[@"access_token"]=[MyAccountTool ReadAccount].access_token;
params[@"status"]=_mytextview.text;
[mgr POST:@"https://upload.api.weibo.com/2/statuses/upload.json" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>picturedata){
UIImage *img=[_mypicture.SaveImages firstObject];
NSData *data=UIImageJPEGRepresentation(img, 1.0);
[picturedata appendPartWithFileData:data name:@"pic" fileName:@"picture.jpg" mimeType:@"img/jpeg"];
} success:^(AFHTTPRequestOperation *operation,NSDictionary *response){
hud.labelText=@"发送成功";
[self.view addSubview:hud];
[hud showAnimated:YES whileExecutingBlock:^{
sleep(1);
[self dismissViewControllerAnimated:YES completion:nil];
} completionBlock:^{
[hud removeFromSuperview];
}];
} failure:^(AFHTTPRequestOperation *operation,NSError *error){
hud.labelText=@"发送失败";
[self.view addSubview:hud];
[hud showAnimated:YES whileExecutingBlock:^{
sleep(1);
[self dismissViewControllerAnimated:YES completion:nil];
} completionBlock:^{
[hud removeFromSuperview];
}];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark
//实现代理的方法
-(void)MyToolBarClick:(MyTextToolBar *)toolbar Click:(MyTextToolButton)tag{
switch (tag) {
case MyCamera:
[self OpenCamera];
NSLog(@"打开相机");
break;
case MyEmotion:
NSLog(@"我的表情");
break;
case MyMention:
NSLog(@"我关联的人");
break;
case MyPicture:
[self OpenPicture];
NSLog(@"我的图片");
break;
case MyTrend:
NSLog(@"我的话题");
break;
default:
break;
}
}
//view显示完毕弹出键盘,避免卡住
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//弹出键盘
[self.mytextview becomeFirstResponder];
}
//拖动响应该方法
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//关闭键盘
[self.mytextview endEditing:YES];
}
//当文本有输入内容时,响应该方法
-(void)textViewDidChange:(UITextView *)textView{
self.navigationItem.rightBarButtonItem.enabled=_mytextview.text.length!=0;
}
//键盘将要显示的时候调用
-(void)KeyBoardWillShow:(NSNotification *)note{
//获取键盘弹出的时候
CGFloat duration=[note.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
[UIView animateWithDuration:duration animations:^{
CGRect keyboardsize=[note.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
//显示文本框工具栏
CGFloat keyboardh=keyboardsize.size.height;
self.mytoolbar.transform=CGAffineTransformMakeTranslation(0,-keyboardh);
}];
}
//键盘将要影藏的时候调用
-(void)KeyBoardWillHide:(NSNotification *)note{
CGFloat duration=[note.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
[UIView animateWithDuration:duration animations:^{
self.mytoolbar.transform=CGAffineTransformIdentity;
}];
}
//响应添加图片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[picker dismissViewControllerAnimated:YES completion:nil];
//取出选中的图片
UIImage *image=info[UIImagePickerControllerOriginalImage];
//添加图片到相册中
[self.mypicture AddImage:image];
}
@end