1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| -(void)gruc_subscribeKeyboardWithAnimations:(GRUCAnimationsWithKeyboardBlock)animations completion:(GRUCCompletionKeyboardAnimationsBlock)completion{ [self gruc_subscribeKeyboardWithBeforeAnimations:nil animations:animations completion:completion]; } -(void)gruc_subscribeKeyboardWithBeforeAnimations:(GRUCBeforeAnimationsWithKeyboardBlock)beforeAnimations animations:(GRUCAnimationsWithKeyboardBlock)animations completion:(GRUCCompletionKeyboardAnimationsBlock)completion{ [self gruc_unsubscribeKeyboard]; objc_setAssociatedObject(self, GRUCBeforeAnimationsBlockAssociationKey, beforeAnimations, OBJC_ASSOCIATION_COPY_NONATOMIC); objc_setAssociatedObject(self, GRUCAnimationsBlockAssociationKey, animations, OBJC_ASSOCIATION_COPY_NONATOMIC); objc_setAssociatedObject(self, GRUCAnimationsCompletionBlockAssociationKey, completion, OBJC_ASSOCIATION_COPY_NONATOMIC); [self gruc_subscribeUIkeyboardNotification]; } -(void)gruc_subscribeUIkeyboardNotification{ [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(gruc_keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; } -(void)gruc_unsubscribeKeyboard{ [self gruc_beforeUnsubscribeKeyboard:nil]; } -(void)gruc_unsubscribeUIKeyboardNotification{ [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil]; } -(void)gruc_beforeUnsubscribeKeyboard:(GRUCCompletionBeforeUnsubscribeKeyboardBlcok)completion{ if (completion) { completion(); } objc_setAssociatedObject(self, GRUCBeforeAnimationsBlockAssociationKey, nil, OBJC_ASSOCIATION_COPY_NONATOMIC); objc_setAssociatedObject(self, GRUCAnimationsBlockAssociationKey, nil, OBJC_ASSOCIATION_COPY_NONATOMIC); objc_setAssociatedObject(self, GRUCAnimationsCompletionBlockAssociationKey, nil, OBJC_ASSOCIATION_COPY_NONATOMIC); [self gruc_unsubscribeUIKeyboardNotification]; }
#pragma mark - #pragma mark -- private methods
-(void)gruc_keyboardWillChangeFrame:(NSNotification *)notification{ BOOL isShowing; NSDictionary *info = [notification userInfo]; CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; UIViewAnimationCurve curve = (UIViewAnimationCurve)[[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]; CGFloat offset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y; self.yOffset += offset; isShowing = endKeyboardRect.origin.y >= kScreenHeight ?NO:YES; GRUCAnimationsWithKeyboardBlock animationsBlock = objc_getAssociatedObject(self, GRUCAnimationsBlockAssociationKey); GRUCBeforeAnimationsWithKeyboardBlock beforeAnimationsBlock = objc_getAssociatedObject(self, GRUCBeforeAnimationsBlockAssociationKey); GRUCCompletionKeyboardAnimationsBlock completionBlock = objc_getAssociatedObject(self, GRUCAnimationsCompletionBlockAssociationKey); if (beforeAnimationsBlock) { beforeAnimationsBlock(endKeyboardRect,offset,duration,isShowing); } [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ [UIView setAnimationCurve:curve]; if (animationsBlock) { animationsBlock(endKeyboardRect,offset,duration,isShowing); } } completion:completionBlock]; } -(CGFloat)yOffset{ return [objc_getAssociatedObject(self, externVarableKey) doubleValue]; } -(void)setYOffset:(CGFloat)yOffset{ objc_setAssociatedObject(self, externVarableKey, @(yOffset), OBJC_ASSOCIATION_COPY_NONATOMIC); }
|