OGeek|极客世界-中国程序员成长平台

标题: ios - 如何根据我在 ScrollView 中的位置更改文本? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 08:53
标题: ios - 如何根据我在 ScrollView 中的位置更改文本?

我的 View 顶部有一个临时自定义“操作栏”,下方有一个 ScrollView 。

我想知道是否有任何方法来更改顶部栏中显示的文本,具体取决于当前在我的 ScrollView 中当前位置可见的内容。

回答

确保您已将 ViewController 设为 ScrollView 委托(delegate),它应如下所示:

@interface ViewController : UIViewController <UIScrollViewDelegate>

一旦您在 .h 文件中声明了 ScrollView 并将其合成到 .m 文件中,请确保告诉它它是委托(delegate)本身:

ScrollView.delegate = self;

然后使用下面的方法来检测你在当前 ScrollView 中的位置

- (void) scrollViewDidScrollUIScrollView *)scroll {
    if(scroll.contentOffset.y > /*Your Y coordinate*/){
        // What you want to do when it reaches your y coordinate.
    }



Best Answer-推荐答案


实现UIScrollViewDelegate,并使用scrollViewDidScroll函数。这将在每次滚动 ScrollView 时通知您。您可以获取 ScrollView 的当前位置,并相应地更新您的文本。

更多关于 Apple Documentation 的帮助

关于ios - 如何根据我在 ScrollView 中的位置更改文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32606275/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4