iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS – 在关闭和滚动手势之间切换大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Line Messenger应用程序(日本的事实上的信使应用程序)中有一种行为,我试图模仿. @H_616_5@ @H_616_5@基本上,它们有一个模态视图控制器,里面有滚动视图.当滚动动作到达其内容的顶部时,视图控制器无缝切换到交互式解雇动画.此外,当手势将视图返回到屏幕顶部时,控制返回到滚动视图.

@H_616_5@这是它的外观的GIF.

@H_616_5@

iOS – 在关闭和滚动手势之间切换

@H_616_5@对于我的生活,我无法弄清楚他们是如何做到的.我尝试了几种不同的方法,但它们都失败了,而且我没有想法.谁能指出我正确的方向?

@H_616_5@EDIT2

@H_616_5@为了澄清,我想要模仿的行为不仅仅是向下拖动窗口.我能做到,没问题.

@H_616_5@我想知道相同的滚动手势(不抬起手指)如何触发解雇转换,然后在将视图拖回原始位置后将控制权转移回滚动视图.

@H_616_5@这是我无法弄清楚的部分.

@H_616_5@结束EDIT2

@H_616_5@EDIT1

@H_616_5@这是我到目前为止所拥有的.我能够使用滚动视图委托方法添加处理常规解雇动画的目标选择器,但它仍然无法按预期工作.

@H_616_5@我创建了一个UIViewController,其UIWebView作为属性.然后我把它放在UINavigationController中,它以模态方式呈现.

@H_616_5@导航控制器使用动画/转换控制器进行常规交互式解雇(可以通过导航栏上的手势来完成).

@H_616_5@从这里开始,一切正常,但无法从滚动视图触发解雇.

@H_616_5@NavigationController.h

@H_616_5@
@interface NavigationController : UINavigationController <UIViewControllerTransitioningDelegate>

@property (nonatomic,strong) UIPanGestureRecognizer *gestureRecog;

- (void)handleGesture:(UIPanGestureRecognizer*)gestureRecognizer;

@end
@H_616_5@NavigationController.m

@H_616_5@
#import "NavigationController.h"
#import "AnimationController.h"
#import "TransitionController.h"

@implementation NavigationController {
    AnimationController *_animator;
    TransitionController *_interactor;
}

- (instanCETypE)init {
    self = [super init];

    self.transitioningDelegate = self;

    _animator = [[AnimationController alloc] init];
    _interactor = [[TransitionController alloc] init];

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // Set the gesture recognizer
    self.gestureRecog = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@SELEctor(handleGesture:)];
    [self.view addGestureRecognizer:_gestureRecog];
}

- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator {
    if (animator == _animator && _interactor.hasStarted) {
        return _interactor;
    }
    return nil;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    if (dismissed == self || [self.viewControllers indexOfObject:dismissed] != NsnotFound) {
        return _animator;
    }
    return nil;
}

- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecog {
    CGFloat threshold = 0.3f;

    CGPoint translation = [gestureRecog translationInView:self.view];
    CGFloat verticalMovement = translation.y / self.view.bounds.size.height;
    CGFloat downWARDR_127_11845@ovement = fmaxf(verticalMovement,0.0f);
    CGFloat downWARDR_127_11845@ovementPercent = fminf(downWARDR_127_11845@ovement,1.0f);

    switch (gestureRecog.statE) {
        case UIGestureRecognizerStateBegan: {
            _interactor.hasStarted = YES;
            [self dismissviewControllerAnimated:YES completion:nil];
            break;
        }
        case UIGestureRecognizerStateChanged: {
            if (!_interactor.hasStarted) {
                _interactor.hasStarted = YES;
                [self dismissviewControllerAnimated:YES completion:nil];
            }
            _interactor.shouldFinish = downWARDR_127_11845@ovementPercent > threshold;
            [_interactor updateInteractiveTransition:downWARDR_127_11845@ovementPercent];
            break;
        }
        case UIGestureRecognizerStateCancelled: {
            _interactor.hasStarted = NO;
            [_interactor cancelInteractiveTransition];
            break;
        }
        case UIGestureRecognizerStateEnded: {
            _interactor.hasStarted = NO;
            if (_interactor.shouldFinish) {
                [_interactor finishInteractiveTransition];
            } else {
                [_interactor cancelInteractiveTransition];
            }
            break;
        }
        default: {
            break;
        }
    }
}

@end
@H_616_5@现在,我必须在滚动视图到达顶部时触发手势处理.所以,这就是我在视图控制器中所做的.

@H_616_5@WebViewController.m

@H_616_5@
#import "WebViewController.h"
#import "NavigationController.h"

@interface WebViewController ()

@property (weak,nonatomiC) IBOutlet UIWebView *webView;
@end

@implementation WebViewController {
    BOOL _isHandlingPan;
    CGPoint _topContentOffset;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.webView.scrollView setDelegate:self];
}    

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ((scrollView.panGestureRecognizer.state == UIGestureRecognizerStateBegan ||
         scrollView.panGestureRecognizer.state == UIGestureRecognizerStateChanged) &&
        ! _isHandlingPan &&
        scrollView.contentOffset.y < self.navigationController.navigationBar.translucent ? -64.0f : 0) {

        NSLog(@"Adding scroll target");

        _topContentOffset = CGPointMake(scrollView.contentOffset.x,self.navigationController.navigationBar.translucent ? -64.0f : 0);
        _isHandlingPan = YES;
        [scrollView.panGestureRecognizer addTarget:self action:@SELEctor(handleGesture:)];
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"Did End Dragging");
    if (_isHandlingPan) {
        NSLog(@"Removing action");
        _isHandlingPan = NO;
        [scrollView.panGestureRecognizer removeTarget:self action:@SELEctor(handleGesture:)];
    }
}
- (void)handleGesture:(UIPanGestureRecognizer*)gestureRecognizer {
    [(NavigationController*)self.navigationController handleGesture:gestureRecognizer];
}
@H_616_5@这仍然无法正常工作.即使在解雇动画期间,滚动视图仍然会以手势滚动.

@H_616_5@结束EDIT1

解决方法

这是一个自定义的交互式过渡. @H_616_5@ @H_616_5@首先,您需要设置UIViewController的transitioningDelegate

@H_616_5@
id<UIViewControllerTransitioningDelegate> transitioningDelegate;
@H_616_5@然后将这两种方法实现

@H_616_5@
//Asks your delegate for the transition animator object to use when dismissing a view controller.
 - animationControllerForDismissedController:
 //Asks your delegate for thE interactive animator object to use when dismissing a view controller.
 - interactionControllerForDismissal:
@H_616_5@当拖动到顶部时,您开始转换,您可以使用UIPercentDrivenInteractiveTransition来控制滚动期间的进度.

@H_616_5@您也可以参ZFDragableModalTransition的源代码

@H_616_5@ZFDragableModalTransition的图片

大佬总结

以上是大佬教程为你收集整理的iOS – 在关闭和滚动手势之间切换全部内容,希望文章能够帮你解决iOS – 在关闭和滚动手势之间切换所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。