可滚动的 UITableView 在 Swift 中向上滑动到屏幕顶部
Scrollable UITableView slide up to top of screen in Swift
我正在尝试创建一个非常简化的布局,类似于 Stripe 的 iOS 应用,您可以在此处查看:https://stripe.com/img/dashboard_iphone/screencast.mp4
在 0:06s 左右,table 视图向上滑动,并向上移动到 window 的顶部。
Swift 中是否有任何简单的说明来展示这种设计模式我到处都看到它但不知道如何创建它
在UIScrollView.h中:
// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top.
// On iPhone, we execute this gesture only if there's one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.
var scrollsToTop: Bool // default is YES.
初始化您的滚动视图委托(除非您已经拥有,可能在您的 viewDidLoad 中),然后将其设置为 true。
像这样:
myScrollView.scrollsToTop = true
如果您想确保您的滚动视图委托允许这样做,请检查 UIScrollViewDelegate 协议中的此方法:
optional func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool // return a yes if you want to scroll to the top. if not defined, assumes YES
如有任何疑虑,请发表评论。
- 添加一个 UIView
- Subclass 带有自定义 class
的 UIView
在您的自定义 UIView 中,您将需要几个变量和一些重写。确保在故事板的 UIView 上启用了用户交互,然后在自定义 class 中添加:
var startPosition: CGPoint?
var originalHeight: CGFloat?
之后,添加以下内容:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
startPosition = touch?.locationInView(self)
originalHeight = self.frame.height
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let endPosition = touch?.locationInView(self)
let difference = endPosition!.y - startPosition!.y
let newFrame = CGRectMake(self.frame.origin.x, self.frame.origin.y + difference, self.frame.width, self.frame.height - difference)
self.frame = newFrame
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
我正在尝试创建一个非常简化的布局,类似于 Stripe 的 iOS 应用,您可以在此处查看:https://stripe.com/img/dashboard_iphone/screencast.mp4
在 0:06s 左右,table 视图向上滑动,并向上移动到 window 的顶部。
Swift 中是否有任何简单的说明来展示这种设计模式我到处都看到它但不知道如何创建它
在UIScrollView.h中:
// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top.
// On iPhone, we execute this gesture only if there's one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.
var scrollsToTop: Bool // default is YES.
初始化您的滚动视图委托(除非您已经拥有,可能在您的 viewDidLoad 中),然后将其设置为 true。
像这样:
myScrollView.scrollsToTop = true
如果您想确保您的滚动视图委托允许这样做,请检查 UIScrollViewDelegate 协议中的此方法:
optional func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool // return a yes if you want to scroll to the top. if not defined, assumes YES
如有任何疑虑,请发表评论。
- 添加一个 UIView
- Subclass 带有自定义 class 的 UIView
在您的自定义 UIView 中,您将需要几个变量和一些重写。确保在故事板的 UIView 上启用了用户交互,然后在自定义 class 中添加:
var startPosition: CGPoint?
var originalHeight: CGFloat?
之后,添加以下内容:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
startPosition = touch?.locationInView(self)
originalHeight = self.frame.height
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let endPosition = touch?.locationInView(self)
let difference = endPosition!.y - startPosition!.y
let newFrame = CGRectMake(self.frame.origin.x, self.frame.origin.y + difference, self.frame.width, self.frame.height - difference)
self.frame = newFrame
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
}