如何在 iOS 11 中像点击状态栏一样调用缓慢滚动到顶部的动画?
Posted
技术标签:
【中文标题】如何在 iOS 11 中像点击状态栏一样调用缓慢滚动到顶部的动画?【英文标题】:How to invoke the slowly scroll to top animation in iOS 11 just like when tapping the status bar? 【发布时间】:2018-02-23 17:27:00 【问题描述】:我希望用户点击标签栏项目让滚动视图滚动到顶部。
如果只是像下面这样设置内容偏移,动画与在 ios 11 中点击状态栏滚动到顶部不同。
scrollView.setContentOffset(CGPoint(x: 0, y: -scrollView.adjustedContentInset.top), animated: true)
我在App Store应用中发现,当用户点击标签栏项目时,页面会滚动到顶部,并且动画与点击状态栏相同,如何存档这个动画?
【问题讨论】:
试试scrollRectToVisible
【参考方案1】:
你可以钩住scrollView的顶部约束并且想要你想要的
self.scrollTopCon.constant = // value
UIView.animate(withDuration: 0.5, animations:
self.view.layoutIfNeeded()
)
【讨论】:
【参考方案2】: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
【讨论】:
请将您的代码格式化为代码,并为您的代码添加一些解释以上是关于如何在 iOS 11 中像点击状态栏一样调用缓慢滚动到顶部的动画?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iOS 中像 UITabBarItem 一样设计 UIButton?