KCKeyboardImagePicker 0.4.2

KCKeyboardImagePicker 0.4.2

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
Released最后发布2015年10月

Kevin Yufei Chen维护。



  • Kevin Yufei Chen

现在支持3D Touch!

屏幕截图

demo Screenshot2 Screenshot3

文档

点击这里查看完整的文档。

演示

要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install

用法

通用用法

#import <KCKeyboardImagePicker/KCKeyboardImagePickerController.h>
  • 显示

通过传递持有选择器的视图控制器的引用来初始化图像选择器控制器。将一个框架(通常是键盘的框架)设置给 KCKeyboardImagePickerController,并将 imagePickerView 添加到容器中。

self.keyboardImagePickerController = [[KCKeyboardImagePickerController alloc] initWithParentViewController:self];
self.keyboardImagePickerController.keyboardFrame = self.keyboardController.currentKeyboardFrame;
[self.someContainerView addSubview:self.keyboardImagePickerController.imagePickerView];

要显示选择器,

[self.keyboardImagePickerController showKeyboardImagePickerViewAnimated:YES];

要关闭选择器,

[self.keyboardImagePickerController hideKeyboardImagePickerViewAnimated:YES];
  • 3D Touch

KCKeyboardImagePicker 支持 3D Touch 的预览和弹出功能。在图像上强制触摸会弹出一个图像的全尺寸预览(而不是裁剪的方形图像)。预览下面的动作表将包含与选项按钮(见下文“动作”部分)相同的选项。

self.keyboardImagePickerController.forceTouchPreviewEnabled = YES;
  • 动作

以下是一个为选项按钮添加动作的示例。将 forceTouchEnabled 参数传递为 YES 将此选项添加到 3D Touch 动作表中。请注意,您需要首先将 forceTouchPreviewEnabled 设置为 YES 以显示动作表。

self.action = [KCKeyboardImagePickerAction actionWithOptionButtonTag:1 
                                                               title:@"Send" 
                                                   forceTouchEnabled:YES
                                                             handler:^(UIImage *selectedImage) {
    // do something with the `selectedImage`
}];
[self.keyboardImagePickerController addAction:self.action];

要为图像选择器控制器按钮(底左角触发 UIImagePickerController 的按钮)添加动作

self.action = [KCKeyboardImagePickerAction actionactionWithImagePickerControllerButtonHandler:^(UIImage *selectedImage) {
    // do something with the `selectedImage`
}];
[self.keyboardImagePickerController addAction:self.action];

您可以添加最多四个动作(即四个选项按钮)。每个动作都会通过 tag 号与一个可选样式(见下文部分)匹配。

  • 样式

要为选项按钮添加样式

self.style = [KCKeyboardImagePickerStyle styleWithOptionButtonTag:1
                                                       titleColor:[UIColor whiteColor]
                                                  backgroundColor:[UIColor lightGrayColor]];
[self.keyboardImagePickerController addStyle:self.style];

要为图像选择器控制器按钮添加样式

self.style = [KCKeyboardImagePickerStyle styleWithImagePickerControllerButtonBackgroundColor:[UIColor lightGrayColor] 
                                                                                       image:[UIImage imageNamed:@"someImage"]];
[self.keyboardImagePickerController addStyle:self.style];

每个动作只能有一个样式,它通过 tag 号匹配。

对于不同数量的选项按钮,布局将不同。

JSQMessagesViewController 集成

包含的演示展示了如何集成 KCKeyboardImagePicker 与著名的 JSQMessagesViewController

在自己的JSQMessagesViewController子类中,采用JSQMessagesKeyboardControllerDelegate协议并重写JSQKeyboardController对象。

@interface DemoMessagesViewController () <JSQMessagesKeyboardControllerDelegate>
self.keyboardController = [[JSQMessagesKeyboardController alloc] initWithTextView:self.inputToolbar.contentView.textView 
                                                                      contextView:self.view
                                                             panGestureRecognizer:self.collectionView.panGestureRecognizer
                                                                         delegate:self];

接下来,实现JSQMessagesKeyboardControllerDelegate中的方法。第一个if分支的目的是避免当textView失去焦点时,inputToolbar的重定位,以隐藏键盘并显示键盘图片选择器。

- (void)keyboardController:(JSQMessagesKeyboardController *)keyboardController keyboardDidChangeFrame:(CGRect)keyboardFrame {
    if (![self.inputToolbar.contentView.textView isFirstResponder] && self.toolbarBottomLayoutGuide.constant == 0.0f) {
        return;
    }
    // code for inputToolbar reposition... 
    // check the demo for details
}

使用KCKeyboardImagePickerController初始化选择器。您需要通过将当前键盘框架分配给keyboardFrame属性来设置选择器的框架。别忘了将选择器视图添加到keyboardControllercontextView

self.keyboardImagePickerController = [[KCKeyboardImagePickerController alloc] initWithParentViewController:self];
self.keyboardImagePickerController.keyboardFrame = self.keyboardController.currentKeyboardFrame;
[self.keyboardController.contextView addSubview:self.keyboardImagePickerController.imagePickerView];

在显示选择器之前,您可以结束contextView的编辑模式以隐藏键盘。

[self.keyboardController.contextView endEditing:YES];
[self.keyboardImagePickerController showKeyboardImagePickerViewAnimated:YES];

要关闭选择器,

[self.keyboardImagePickerController hideKeyboardImagePickerViewAnimated:YES];

没有默认控制器

或者,您可以有自己的实现来采用KCKeyboardImagePickerViewDataSourceKCKeyboardImagePickerViewDelegate。只需这样做:

#import <KCKeyboardImagePicker/KCKeyboardImagePickerView.h>

这样,您就可以自定义选择器的图像源和其它样式。

安装

KCKeyboardImagePicker通过CocoaPods提供。要安装,只需将以下行添加到您的Podfile中:

pod 'KCKeyboardImagePicker'

作者

Kev1nChen (Kevin Yufei Chen)

许可证

KCKeyboardImagePicker在MIT许可证下可用。更多信息请查看LICENSE文件。