gpt4 book ai didi

ios - 在平移 View 中实时检测 scrollView 位置

转载 作者:行者123 更新时间:2023-11-29 01:03:53 27 4
gpt4 key购买 nike

我在初始启动时有一个平移的 UIViewController,它加载了两个单独的 XIB View ,用户可以滑动它们来遍历它们。我想在用户在总宽度(两个 View 的平移宽度)上滑动 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 的代理

scrollView.delegate = self

并声明你的类实现了UIScrollViewDelegate:

class ViewController: UIViewController, UIScrollViewDelegate

然后实现 scrollViewDidScroll 每次你的 scrollview 的滚动改变时调用它。

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
// ...
}
}

关于ios - 在平移 View 中实时检测 scrollView 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36660342/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com