Dan*_*Rak 13 iphone objective-c uiviewcontroller ios uipangesturerecognizer
我一直在尝试新的iOS 7自定义转换API,并查看了我能找到的所有教程/文档,但我似乎无法根据我的具体情况来计算这些内容.
基本上我正在尝试实现的是一个视图上的UIPanGestureRecognizer,我将向上滑动并转换到VC,其视图将从底部向上滑动,而当我将手指向上移动时当前视图将向上滑动.
没有交互转换我没有遇到任何问题,但是一旦我实现了交互(平移手势),我似乎无法完成转换.
这是来自VC的相关代码,它符合出售动画控制器所需的UIViewControllerTransitionDelegate:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"Swipe"]) {
NSLog(@"PREPARE FOR SEGUE METHOD CALLED");
UIViewController *toVC = segue.destinationViewController;
[interactionController wireToViewController:toVC];
toVC.transitioningDelegate = self;
toVC.modalPresentationStyle = UIModalPresentationCustom;
}
}
#pragma mark UIViewControllerTransition Delegate Methods
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController: (UIViewController *)presented presentingController: (UIViewController *)presenting sourceController:(UIViewController *)source {
NSLog(@"PRESENTING ANIMATION CONTROLLER CALLED");
SwipeDownPresentationAnimationController *transitionController = [SwipeDownPresentationAnimationController new];
return transitionController;
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
NSLog(@"DISMISS ANIMATION CONTROLLER CALLED");
DismissAnimatorViewController *transitionController = [DismissAnimatorViewController new];
return transitionController;
}
- (id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator {
NSLog(@"Interaction controller for dimiss method caled");
return interactionController.interactionInProgress ? interactionController:nil;
}
Run Code Online (Sandbox Code Playgroud)
注意:交互滑动仅用于解雇VC,这就是为什么它在interactionControllerForDismissal方法中
这是解雇动画师的代码,当我点击一个按钮来解雇它时,它的工作正常:
#import "DismissAnimatorViewController.h"
@implementation DismissAnimatorViewController
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
return 1.0;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
NSTimeInterval duration = [self transitionDuration:transitionContext];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
CGRect initialFrameFromVC = [transitionContext initialFrameForViewController:fromVC];
UIView *containerView = [transitionContext containerView];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
NSLog(@"The screen bounds is :%@", NSStringFromCGRect(screenBounds));
toVC.view.frame = CGRectOffset(initialFrameFromVC, 0, screenBounds.size.height);
toVC.view.alpha = 0.2;
CGRect pushedPresentingFrame = CGRectOffset(initialFrameFromVC, 0, -screenBounds.size.height);
[containerView addSubview:toVC.view];
[UIView animateWithDuration:duration
delay:0
usingSpringWithDamping:0.6
initialSpringVelocity:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
fromVC.view.frame = pushedPresentingFrame;
fromVC.view.alpha = 0.2;
toVC.view.frame = initialFrameFromVC;
toVC.view.alpha = 1.0;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
@end
Run Code Online (Sandbox Code Playgroud)
这是UIPercentDrivenInteractiveTransition子类的代码,它充当交互控制器:
#import "SwipeInteractionController.h"
@implementation SwipeInteractionController {
BOOL _shouldCompleteTransition;
UIViewController *_viewController;
}
- (void)wireToViewController:(UIViewController *)viewController {
_viewController = viewController;
[self prepareGestureRecognizerInView:_viewController.view];
}
- (void)prepareGestureRecognizerInView:(UIView*)view {
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
gesture.minimumNumberOfTouches = 1.0;
[view addGestureRecognizer:gesture];
}
- (CGFloat)completionSpeed {
return 1 - self.percentComplete;
NSLog(@"PERCENT COMPLETE:%f",self.percentComplete);
}
- (void)handleGesture:(UIPanGestureRecognizer*)gestureRecognizer {
// CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view.superview];
CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view.superview];
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
// 1. Start an interactive transition!
self.interactionInProgress = YES;
[_viewController dismissViewControllerAnimated:YES completion:nil];
break;
case UIGestureRecognizerStateChanged: {
// 2. compute the current position
CGFloat fraction = fabsf(translation.y / 568);
NSLog(@"Fraction is %f",fraction);
fraction = fminf(fraction, 1.0);
fraction = fmaxf(fraction, 0.0);
// 3. should we complete?
_shouldCompleteTransition = (fraction > 0.23);
// 4. update the animation controller
[self updateInteractiveTransition:fraction];
NSLog(@"Percent complete:%f",self.percentComplete);
break;
}
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled: {
// 5. finish or cancel
NSLog(@"UI GESTURE RECOGNIZER STATE CANCELED");
self.interactionInProgress = NO;
if (!_shouldCompleteTransition || gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
[self cancelInteractiveTransition];
NSLog(@"Interactive Transition is cancled.");
}
else {
NSLog(@"Interactive Transition is FINISHED");
[self finishInteractiveTransition];
}
break;
}
default:
NSLog(@"Default is being called");
break;
}
}
@end
Run Code Online (Sandbox Code Playgroud)
再次,当我现在运行代码并且我没有一直滑动以有目的地取消转换时,我只是得到一个闪光灯并且呈现了我想要滑动到的视图控制器.无论转换是完成还是取消,都会发生这种情况.
但是,当我通过按钮解除时,我得到了动画师视图控制器中指定的转换.
Col*_*inE 22
我可以在这里看到几个问题 - 虽然我不能确定这些会解决你的问题!
首先,动画控制器的UIView动画完成块具有以下内容:
[transitionContext completeTransition:YES];
Run Code Online (Sandbox Code Playgroud)
而它应该根据交互控制器的结果返回完成,如下所示:
[transitionContext completeTransition:![transitionContext transitionWasCancelled]]
Run Code Online (Sandbox Code Playgroud)
此外,我发现如果你告诉UIPercentDrivenInteractiveTransition
转换是100%完成,它不会调用动画控制器完成块.作为一种解决方法,我将它限制在~99.9%
https://github.com/ColinEberhardt/VCTransitionsLibrary/issues/4
我在这里创建了许多示例交互和动画控制器,您可能会发现它们很有用:
https://github.com/ColinEberhardt/VCTransitionsLibrary