SHPKeyboardAwareness 3.1.0

SHPKeyboardAwareness 3.1.0

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最后发布2017年6月

由以下人员维护: Mikkel Selsoe SorensenSoren UlrikkeholmPhilip BrucePeter Gammelgaard PoulsenJonas Lysgaard-HansenMarcus MattssonJakob Vinther-Larsen



  • Philip Bruce

SHPKeyboardAwareness 可以让您知道何时出现键盘,并准确地告诉您有关第一响应者被遮挡程度、哪个视图是第一响应者、键盘动画曲线、持续时间等多方面的信息。

SHPKeyboardAwareness 不会劫持您任何视图的代理方法,它既不派生也不会要求您派生任何内容。SHPKeyboardAwareness 不会对您的代码进行任何妥协。

它如何工作

启用 SHPKeyboardAwareness 的同时,每当键盘出现、消失或第一响应者发生变化时,您都会收到通知。 SHPKeyboardAwareness 跟踪第一响应者,并在通知中告诉您视图是否被键盘遮挡。如果需要,您也可以指定要跟踪的特定视图,而不是当前的第一响应者。

示例项目

如果冲突的视图是 UITextView,将计算所需的偏移量用于光标(插入点)

请参阅 Example/ 文件夹中包含的示例项目。

使用方法

默认使用

对于不需要自行处理的默认实现,使用

[SHPKeyboardAwarenessObserver ObserveView:view verticalConstraint:bottomConstraint conflictingViewPadding:20];
[SHPKeyboardAwarenessObserver ObserveScrollView:scrollView conflictingViewPadding:20];

如果需要更专业的实现,您可以使用代理方法。

委托

SHPKeyboardAwareness 继承自 NSObject,易于使用。要设置 SHPKeyboardAwareness,只需调用

[SHPKeyboardAwarenessObserver ObserveWithDeledgate:self];

这将订阅 self 的任何键盘事件。

Note that when self deallocates it is automatically unsubscribed from further keyboard events.

第一响应者(UITextFieldUITextView)将作为通知的一部分传入。要接收通知,请遵守协议 SHPKeyboardAwarenessClient 并实现所需的方法

- (void)keyboardTriggeredEvent:(SHPKeyboardEvent *)keyboardEvent

对立的观点

您通过代理方法收到的 SHPKeyboardEvent 包含对任何对立视图的引用,该引用位于属性中

@property (nonatomic, readonly) UIView *conflictingView;

视图被键盘遮挡的程度在属性中指定

@property (nonatomic, readonly) CGFloat requiredViewOffset;

这些信息可以用来自动移动视图,以便它不再被遮挡

示例 1 - ViewController 管理集合视图

在这个示例中,我们将视图控制器订阅到可能与应用程序页脚冲突的任何第一响应者 UITextFieldUITextView 的通知

@import SHPKeyboardAwareness;

@interface ViewController ()
@property (nonatomic, strong) SHPKeyboardAwarenessObserver *keyboardAwareness;
@end

@implementation ViewController
...
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Subscribe to keyboard events. The receiver (self) will be automatically unsubscribed when deallocated
    self.keyboardAwareness = [SHPKeyboardAwarenessObserver ObserveWithDelegate:self];
}

- (void)keyboardTriggeredEvent:(SHPKeyboardEvent *)keyboardEvent {
    
    CGFloat offset = 0;
    switch (keyboardEvent.keyboardEventType) {
        case SHPKeyboardEventTypeShow:
        
            // Keyboard will appear. Calculate the new offset from the provided offset
            offset = collectionView.contentOffset.y - keyboardEvent.requiredViewOffset;
            
            // Save the current view offset into the event to retrieve it later
            keyboardEvent.originalOffset = collectionView.contentOffset.y;
            
            break;
        case SHPKeyboardEventTypeHide:
              
            // Keyboard will hide. Reset view offset to its state before keyboard appeared
            offset = keyboardEvent.originalOffset;
                
            break;
        default:
            break;
    }
        
    // Animate the offset change with the provided curve and duration
    [UIView animateWithDuration:keyboardEvent.keyboardAnimationDuration 
                          delay:0 
                        options:keyboardEvent.keyboardAnimationOptionCurve 
                     animations:^{
        self.collectionView.contentOffset = CGPointMake(collectionView.contentOffset.x, offset);

				self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, keyboardEvent.keyboardFrame.size.height, 0);
        self.collectionView.scrollIndicatorInsets = self.collectionView.contentInset;
    } completion:nil];
}
...
@end

上面的示例中的 SHPKeyboardEvent 会告诉你

  • 与键盘冲突的视图(一个 UITextFieldUITextView 实例)
  • 需要将视图垂直移动多少才能消除键盘中断(你需要自己移动视图)
  • 键盘 frame 和可见屏幕 frame
  • 键盘动画的曲线和持续时间
  • 键盘是即将显示还是即将隐藏

此外

  • 在事件对象上,您可以在键即将显示时保存视图的原始偏移量。当它即将隐藏时,您得到相同的事件对象,参数更新。检索原始偏移量以将视图重置到先前的状态。
  • 所有坐标都相对于设备旋转进行归一化

示例 2 - 当特定视图实例遮挡时收到通知

在这个示例中,我们订阅到当特定视图被键盘遮挡时的通知。例如,您可能有一个容器视图围绕 UITextFieldUITextView,您希望保持清晰,例如,因为容器视图包含一个 UIButton,它必须完全可见。

@import SHPKeyboardAwareness;

@interface ViewController ()
@property (nonatomic, strong) SHPKeyboardAwarenessObserver *keyboardAwareness;
@end


@implementation ViewController
...
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Subscribe to keyboard events. The receiver (self) will be automatically unsubscribed when deallocated
    self.keyboardAwareness = [SHPKeyboardAwarenessObserver ObserveView: _myView withDelegate:self];
}

- (void)keyboardTriggeredEvent:(SHPKeyboardEvent *)event {

    // This event fires when and only when any part of the provided view (containerView) conflicts 
    // with the keyboard that is about to appear. Events for other textFields etc are ignored.

    // For the sake of simplicity, we assume the containerFrame's origin.y is 0 when keyboard is not visible
    // But you can use the originalOffset property as shown above to preserve its origin.

    CGRect containerFrame = containerView.frame;
    containerFrame.origin.y += event.requiredViewOffset;
    
    // Animate the offset change with the provided curve and duration
    [UIView animateWithDuration:event.keyboardAnimationDuration 
                          delay:0 
                        options:event.keyboardAnimationOptionCurve 
                     animations:^{
        containerView.frame = containerFrame;
    } completion:nil];
}
...
@end

更多内容,请参见以下链接

如果您发现错误或对功能有建议,请创建拉取请求或打开问题,指定您的问题/功能请求。

我们希望您喜欢使用 SHPKeyboardAwareness 并且它能帮助您避免在应用程序中的令人讨厌的键盘遮挡。