如何在iOS 11中调用慢速滚动到顶部动画,就像点击状态栏一样?

时间:2018-02-23 17:27:01

标签: ios uitableview animation uiscrollview ios11

我希望用户点击标签栏项目,让滚动视图滚动到顶部。

如果仅设置内容偏移量,则动画与点击状态栏以在iOS 11中滚动到顶部不同。

scrollView.setContentOffset(CGPoint(x: 0, y: -scrollView.adjustedContentInset.top), animated: true)

我在App Store应用程序中找到,当用户点击标签栏项目时,页面将滚动到顶部,动画与点击状态栏相同,如何存档此动画?

2 个答案:

答案 0 :(得分:0)

你可以挂钩scrollView的顶级约束,并希望你想要

self.scrollTopCon.constant = // value

UIView.animate(withDuration: 0.5, animations: {

    self.view.layoutIfNeeded()

})

答案 1 :(得分:-1)

extension UIScrollView {

    @objc public func extension_scrollToTopIfPossible(animated: Bool) {
        let selector = NSSelectorFromString("_scrollToTopIfPossible:")
        guard self.responds(to: selector) else {
            return
        }
        self.performSelector(onMainThread: selector, with: animated ? true : nil, waitUntilDone: true)
    }
}

OR

@implementation UIWindow (SCROLL)

- (void)performScrollToTop {

    SEL selector = NSSelectorFromString(@"_scrollToTopViewsUnderScreenPointIfNecessary:resultHandler:");

    if ([self respondsToSelector:selector] == false) {
        return;
    }

    NSMethodSignature *signature = [UIWindow instanceMethodSignatureForSelector:selector];

    if (signature == nil) {
        return;
    }

    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIWindow instanceMethodSignatureForSelector:selector]];

    [invocation setTarget:self];

    [invocation setSelector:selector];

    CGRect statusBarFrame = UIApplication.sharedApplication.statusBarFrame;

    CGPoint point = CGPointMake(statusBarFrame.size.width / 2.0, statusBarFrame.size.height + 1.0);

    [invocation setArgument:&point atIndex:2];

    [invocation invoke];
}

@end