diff --git a/MFSlidingView/MFSlidingView.h b/MFSlidingView/MFSlidingView.h index 48dd306..b330292 100644 --- a/MFSlidingView/MFSlidingView.h +++ b/MFSlidingView/MFSlidingView.h @@ -48,9 +48,9 @@ typedef enum { offScreenPosition:(SlidingViewOffScreenPosition)offScreenPosition title:(NSString *)title options:(SlidingViewOptions)options - doneBlock:(void (^)())doneBlock - cancelBlock:(void (^)())cancelBlock; + doneBlock:(void (^)(void))doneBlock + cancelBlock:(void (^)(void))cancelBlock; + (void) slideOut; -@end \ No newline at end of file +@end diff --git a/MFSlidingView/MFSlidingView.m b/MFSlidingView/MFSlidingView.m index c64e1aa..2570b5e 100644 --- a/MFSlidingView/MFSlidingView.m +++ b/MFSlidingView/MFSlidingView.m @@ -105,8 +105,8 @@ + (MFSlidingView *) slideView:(UIView *)view offScreenPosition:(SlidingViewOffScreenPosition)offScreenPosition title:(NSString *)title options:(SlidingViewOptions)options - doneBlock:(void (^)())doneBlock - cancelBlock:(void (^)())cancelBlock { + doneBlock:(void (^)(void))doneBlock + cancelBlock:(void (^)(void))cancelBlock { MFSlidingView *slidingView = [[MFSlidingView alloc] initWithContentView:view]; slidingView.doneBlock = doneBlock; @@ -251,13 +251,13 @@ - (CGPoint) onScreenCoordinates { switch (self.finalPosition) { case TopOfScreen: - finalCoordinates.y = 0.0; + finalCoordinates.y = self.containerView.safeAreaInsets.top; break; case MiddleOfScreen: finalCoordinates.y = (self.containerView.bounds.size.height - frame.size.height)/2; break; case BottomOfScreen: - finalCoordinates.y = self.containerView.bounds.size.height - frame.size.height; + finalCoordinates.y = self.containerView.bounds.size.height - frame.size.height - self.containerView.safeAreaInsets.bottom; break; } @@ -292,6 +292,12 @@ - (void) slideIntoView:(UIView *)wrapper selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(orientationDidChange:) + name:UIDeviceOrientationDidChangeNotification object:nil]; + + if(self.contentView.frame.size.width < self.containerView.frame.size.width) { bodyView.backgroundColor = [UIColor blackColor]; @@ -317,14 +323,15 @@ - (void) slideIntoView:(UIView *)wrapper [bodyView addSubview:self.contentView]; // slide in animation - [UIView beginAnimations:@"slideIn" context:nil]; - [UIView setAnimationDelegate:self]; - - frame.origin = [self onScreenCoordinates]; - bodyView.frame = frame; - self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.55]; - - [UIView commitAnimations]; + [UIView animateWithDuration:0.25 animations:^{ + CGRect newFrame = frame; + newFrame.origin = [self onScreenCoordinates]; + self->bodyView.frame = newFrame; + self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.55]; + + } completion:^(BOOL finished) { + + }]; framePriorToKeyboardMovement = bodyView.frame; } @@ -333,25 +340,15 @@ - (void) slideOut { [[NSNotificationCenter defaultCenter] removeObserver:self]; - [UIView beginAnimations:@"slideOut" context:nil]; - [UIView setAnimationDelegate:self]; - [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; - - CGRect frame = bodyView.frame; - frame.origin = [self offScreenCoordinates]; - bodyView.frame = frame; - self.backgroundColor = [UIColor clearColor]; - - [UIView commitAnimations]; -} + [UIView animateWithDuration:0.25 animations:^{ + CGRect frame = self->bodyView.frame; + frame.origin = [self offScreenCoordinates]; + self->bodyView.frame = frame; + self.backgroundColor = [UIColor clearColor]; -- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context -{ - if ([animationID isEqualToString:@"slideOut"]) { + } completion:^(BOOL finished) { if(self.superview) [self removeFromSuperview]; - } else if ([animationID isEqualToString:@"slideIn"]) { - - } + }]; } - (void) cancelPressed:(id)sender { @@ -386,12 +383,29 @@ - (BOOL) adjustToFirstResponder { frame.origin.y = idealPosition - firstResponderBounds.origin.y; // Shrink view's height by the keyboard's height, and scroll to show the text field/view being edited - [UIView beginAnimations:nil context:NULL]; - bodyView.frame = frame; - [UIView commitAnimations]; + + [UIView animateWithDuration:0.25 animations:^{ + self->bodyView.frame = frame; + + } completion:^(BOOL finished) { + }]; + return YES; } +static inline UIViewAnimationOptions animationOptionsWithCurve(UIViewAnimationCurve curve) +{ + switch (curve) { + case UIViewAnimationCurveEaseInOut: + return UIViewAnimationOptionCurveEaseInOut; + case UIViewAnimationCurveEaseIn: + return UIViewAnimationOptionCurveEaseIn; + case UIViewAnimationCurveEaseOut: + return UIViewAnimationOptionCurveEaseOut; + case UIViewAnimationCurveLinear: + return UIViewAnimationOptionCurveLinear; + } +} - (void)keyboardWillShow:(NSNotification*)notification { UIView *firstResponder = [self findFirstResponderBeneathView:self]; @@ -415,14 +429,12 @@ - (void)keyboardWillShow:(NSNotification*)notification { NSInteger animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; - // Shrink view's height by the keyboard's height, and scroll to show the text field/view being edited - [UIView beginAnimations:nil context:NULL]; - [UIView setAnimationCurve:animationCurve]; - [UIView setAnimationDuration:animationDuration]; - - bodyView.frame = frame; - - [UIView commitAnimations]; + [UIView animateWithDuration:animationDuration delay:0 options:animationOptionsWithCurve(animationCurve) animations:^{ + self->bodyView.frame = frame; + + } completion:^(BOOL finished) { + + }]; } - (void)keyboardWillHide:(NSNotification*)notification { @@ -432,13 +444,12 @@ - (void)keyboardWillHide:(NSNotification*)notification { objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; // Restore dimensions to prior size - [UIView beginAnimations:nil context:NULL]; - [UIView setAnimationCurve:animationCurve]; - [UIView setAnimationDuration:animationDuration]; - - bodyView.frame = framePriorToKeyboardMovement; - - [UIView commitAnimations]; + [UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{ + self->bodyView.frame = self->framePriorToKeyboardMovement; + + } completion:^(BOOL finished) { + + }]; } -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event @@ -452,4 +463,22 @@ -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event } } +- (void)orientationDidChange:(NSNotification *)notification { + CGRect frame; + + frame.size = self.containerView.frame.size; + frame.origin = CGPointMake(0, 0); + self.frame = frame; + + [UIView animateWithDuration:0.25 animations:^{ + CGRect newFrame = self.bodyFrame; + newFrame.origin = [self onScreenCoordinates]; + self->bodyView.frame = newFrame; + + + } completion:^(BOOL finished) { + + }]; +} + @end