在平移视图中实时检测 scrollView 位置
Live Detection of scrollView position in panned view
我在初始启动时有一个平移的 UIViewController,它加载了两个单独的 XIB 视图,用户可以通过滑动来遍历它们。我想在用户在总宽度(两个视图的平移宽度)上滑动 2/3 时触发代码,但我的检测未生效。
到目前为止,这是我的代码...
let vc0 = ViewController0(nibName: "ViewController0", bundle: nil)
let vc1 = ViewController1(nibName: "ViewController1", bundle: nil)
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.addChildViewController(vc0)
self.scrollView.addSubview(vc0.view)
vc0.didMoveToParentViewController(self)
var frame1 = vc1.view.frame
frame1.origin.x = self.view.frame.size.width
vc1.view.frame = frame1
self.addChildViewController(vc1)
self.scrollView.addSubview(vc1.view)
vc1.didMoveToParentViewController(self)
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height - 66)
// This doesn't work
scrollView.delegate = self
// If the user user swipes 2/3 in (or it can be any other offset that's practical)
if (scrollView.contentOffset.x > (self.view.frame.size.width*2)*(2/3)) {
// Code I'd like to execute
// ...
}
}
Swift 这怎么可能?
您应该使用
设置滚动视图的委托
scrollView.delegate = self
并声明您的 class 实现 UIScrollViewDelegate
:
class ViewController: UIViewController, UIScrollViewDelegate
然后实现 scrollViewDidScroll
,每次滚动视图的滚动发生变化时都会调用它。
func scrollViewDidScroll(scrollView: UIScrollView) {
// If the user user swipes 2/3 in (or it can be any other offset that's practical)
if (scrollView.contentOffset.x > (self.view.frame.size.width*2)/(2/3)) {
// Code I'd like to execute
// ...
}
}
我在初始启动时有一个平移的 UIViewController,它加载了两个单独的 XIB 视图,用户可以通过滑动来遍历它们。我想在用户在总宽度(两个视图的平移宽度)上滑动 2/3 时触发代码,但我的检测未生效。
到目前为止,这是我的代码...
let vc0 = ViewController0(nibName: "ViewController0", bundle: nil)
let vc1 = ViewController1(nibName: "ViewController1", bundle: nil)
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.addChildViewController(vc0)
self.scrollView.addSubview(vc0.view)
vc0.didMoveToParentViewController(self)
var frame1 = vc1.view.frame
frame1.origin.x = self.view.frame.size.width
vc1.view.frame = frame1
self.addChildViewController(vc1)
self.scrollView.addSubview(vc1.view)
vc1.didMoveToParentViewController(self)
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height - 66)
// This doesn't work
scrollView.delegate = self
// If the user user swipes 2/3 in (or it can be any other offset that's practical)
if (scrollView.contentOffset.x > (self.view.frame.size.width*2)*(2/3)) {
// Code I'd like to execute
// ...
}
}
Swift 这怎么可能?
您应该使用
设置滚动视图的委托scrollView.delegate = self
并声明您的 class 实现 UIScrollViewDelegate
:
class ViewController: UIViewController, UIScrollViewDelegate
然后实现 scrollViewDidScroll
,每次滚动视图的滚动发生变化时都会调用它。
func scrollViewDidScroll(scrollView: UIScrollView) {
// If the user user swipes 2/3 in (or it can be any other offset that's practical)
if (scrollView.contentOffset.x > (self.view.frame.size.width*2)/(2/3)) {
// Code I'd like to execute
// ...
}
}