自定义发动态的Controller(有工具条,自定义textView,和photosView)

来源:互联网 发布:广联达结算软件5.0 编辑:程序博客网 时间:2024/06/04 19:36

//

//  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 [[ZZComposeControlleralloc] init];

}


- (void)viewDidLoad {

    [superviewDidLoad];


    // 设置导航栏属性

    [selfsetupNavBar];

    

    // 添加textView

    [selfsetupTextView];

    

    // 添加toolbar

    [selfsetupToolbar];

    

    // 添加photosView

    [selfsetupPhotosView];

}


/**

 *  添加textView

 */

- (void)setupTextView

{

    // 1.添加

   ZZTextView *textView = [[ZZTextViewalloc] init];

    textView.font = [UIFontsystemFontOfSize:15];

    textView.frame =self.view.bounds;

    //垂直方向上永远可以拖拽

    textView.alwaysBounceVertical =YES;

    textView.delegate =self;

    textView.placeholder =@"分享新鲜事...";

    [self.viewaddSubview:textView];

   self.textView = textView;

    

    // 2.监听textView文字改变的通知

    [ZZNotificationCenteraddObserver:selfselector:@selector(textDidChange)name:UITextViewTextDidChangeNotificationobject:textView];

    

    // 3.监听键盘的通知

    [ZZNotificationCenteraddObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];

    [ZZNotificationCenteraddObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];

}


/**

 *  监听文字改变

 */

- (void)textDidChange

{

    self.navigationItem.rightBarButtonItem.enabled = (self.textView.text.length != 0);

}


- (void)dealloc

{

    [ZZNotificationCenterremoveObserver:self];

}


/**

 *  设置导航栏属性

 */

- (void)setupNavBar

{

    self.view.backgroundColor = [UIColorwhiteColor];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc] initWithTitle:@"取消"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(cancel)];

    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc] initWithTitle:@"哈哈"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(send)];


#pragma warning ---- 为何???

    self.navigationItem.rightBarButtonItem.title =@"发送";

    self.navigationItem.rightBarButtonItem.enabled =NO;


   self.title =@"发微博";

}



/**

 *  添加photosView

 */

- (void)setupPhotosView

{

    ZZComposePhotosView *photosView = [[ZZComposePhotosViewalloc] 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.textViewaddSubview:photosView];

   self.photosView = photosView;

}


/**

 *  添加toolbar

 */

- (void)setupToolbar

{

    ZZComposeToolbar *toolbar = [[ZZComposeToolbaralloc] 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.viewaddSubview:toolbar];

   self.toolbar = toolbar;

}


#pragma mark - toolbar的代理方法

- (void)composeToolbar:(ZZComposeToolbar *)toolbar didClickedButton:(ZZComposeToolbarButtonType)buttonType

{

   switch (buttonType) {

        caseZZComposeToolbarButtonTypeCamera: //相机

            [selfopenCamera];

           break;

            

        caseZZComposeToolbarButtonTypePicture: //相册

            [selfopenPhotoLibrary];

           break;

            

       default:

           break;

    }

}


/**

 *  打开相机

 */

- (void)openCamera

{

    UIImagePickerController *ipc = [[UIImagePickerControlleralloc] init];

    ipc.sourceType =UIImagePickerControllerSourceTypeCamera;

    ipc.delegate =self;

    [selfpresentViewController:ipc animated:YEScompletion:nil];

}


/**

 *  打开相册

 */

- (void)openPhotoLibrary

{

    UIImagePickerController *ipc = [[UIImagePickerControlleralloc] init];

    ipc.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

    ipc.delegate =self;

    [selfpresentViewController:ipc animated:YEScompletion:nil];

}


#pragma mark - 图片选择控制器的代理

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    // 1.销毁picker控制器

    [picker dismissViewControllerAnimated:YEScompletion:nil];

    

    // 2.去的图片

    UIImage *image = info[UIImagePickerControllerOriginalImage];

    [self.photosViewaddImage:image];

}


/**

 *  键盘即将显示的时候调用

 */


- (void)keyboardWillShow:(NSNotification *)note

{

    // 1.取出键盘的frame

    CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];

    

    // 2.取出键盘弹出的时间

    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];

    

    // 3.执行动画

    [UIViewanimateWithDuration:duration animations:^{

        self.toolbar.transform =CGAffineTransformMakeTranslation(0, -keyboardF.size.height);

    }];

}


/**

 *  键盘即将退出的时候调用

 */

- (void)keyboardWillHide:(NSNotification *)note

{

    // 1.取出键盘弹出的时间

    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];

    

    // 2.执行动画

    [UIViewanimateWithDuration:duration animations:^{

        self.toolbar.transform =CGAffineTransformIdentity;

    }];

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    [self.viewendEditing:YES];

}


- (void)viewDidAppear:(BOOL)animated

{

    [superviewDidAppear:animated];

    

    [self.textViewbecomeFirstResponder];

}



/**

 *  取消

 */

- (void)cancel

{

    if (self.textView.text.length) {

       /**

         *  根据用户选择来取

         */

       NSUserDefaults *choiceDefaults = [NSUserDefaultsstandardUserDefaults];

       NSDictionary *choiceDict = [choiceDefaults objectForKey:@"choiceNot"];

       if (!choiceDict) {

           UIButton *checkBoxRight = [UIButtonbuttonWithType:UIButtonTypeCustom];

            checkBoxRight.tag =11;

            checkBoxRight.frame =CGRectMake(30.0,25, 15, 15);

            [checkBoxRight setImage:[UIImageimageNamed:@"check"]forState:UIControlStateNormal];

            [checkBoxRight setImage:[UIImageimageNamed:@"checked"]forState:UIControlStateSelected];

            [checkBoxRight addTarget:selfaction:@selector(clickToChoiceNot:)forControlEvents:UIControlEventTouchUpInside];

            

           UILabel *action = [[UILabelalloc] initWithFrame:CGRectMake(50,22, 150, 20)];

            action.text =NSLocalizedString(@"下次不再提醒",nil);

            action.font = [UIFontsystemFontOfSize:13];

            

           UIView *v = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 200, 50)];

            [vaddSubview:action];

            [vaddSubview:checkBoxRight];

            

            UIAlertView *CheckView = [[UIAlertViewalloc] initWithTitle:NSLocalizedString(@"确认取消当前编辑",nil) message:@""delegate:selfcancelButtonTitle:NSLocalizedString(@"取消",nil) otherButtonTitles:NSLocalizedString(@"确定",nil), nil];

            

            [CheckViewsetValue:v  forKey:@"accessoryView"];

            [CheckViewshow];

        }else {

            [selfdismissViewControllerAnimated:YEScompletion:nil];

        }

    }else {

        [selfdismissViewControllerAnimated:YEScompletion:nil];

    }

}


/**

 *  监听alertView弹框的点击

 */

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

   if (buttonIndex == 0) {

        

    }else {

        [selfdismissViewControllerAnimated:YEScompletion:^{

            

        }];

    }

}


/**

 *  选择下次不再提醒

 */

- (void)clickToChoiceNot:(UIButton *)sender

{

   if (sender.selected ==YES) {

        sender.selected =NO;

    }

   else

        sender.selected =YES;

    

   /**

     * 存状态,为什么通过字典呢?可以通过监听同一个方法存取多个按钮状态

     */

    NSUserDefaults *choiceDefaults = [NSUserDefaultsstandardUserDefaults];

   NSString *str = [NSStringstringWithFormat:@"%ld",sender.tag];

   NSNumber *num = [NSNumbernumberWithBool:sender.selected];

   NSDictionary *choiceDict = @{str:num};

    [choiceDefaultssetObject:choiceDict forKey:@"choiceNot"];

    [choiceDefaultssynchronize];

}

/**

 *  发微博

 */

- (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.关闭控制器

    [selfdismissViewControllerAnimated:YEScompletion:nil];

}


@end



1 0
原创粉丝点击