菜鸟教程小白 发表于 2022-12-12 08:53:25

ios - 如何根据我在 ScrollView 中的位置更改文本?


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

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

<p><strong><em>回答</em></strong></p>

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

<pre><code>@interface ViewController : UIViewController &lt;UIScrollViewDelegate&gt;
</code></pre>

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

<pre><code>ScrollView.delegate = self;
</code></pre>

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

<pre><code>- (void) scrollViewDidScroll:(UIScrollView *)scroll {
    if(scroll.contentOffset.y &gt; /*Your Y coordinate*/){
      // What you want to do when it reaches your y coordinate.
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>实现<code>UIScrollViewDelegate</code>,并使用<code>scrollViewDidScroll</code>函数。这将在每次滚动 ScrollView 时通知您。您可以获取 ScrollView 的当前位置,并相应地更新您的文本。</p>

<p>更多关于 <a href="https://developer.apple.com/library/prerelease/ios//documentation/UIKit/Reference/UIScrollViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIScrollViewDelegate/scrollViewDidScroll:" rel="noreferrer noopener nofollow">Apple Documentation</a> 的帮助</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何根据我在 ScrollView 中的位置更改文本?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32606275/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32606275/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何根据我在 ScrollView 中的位置更改文本?