gpt4 book ai didi

cocoa-touch - 如何在运行时在 UIscrollview 中添加和删除 View ?

转载 作者:行者123 更新时间:2023-12-04 06:20:20 24 4
gpt4 key购买 nike

我的项目需要在运行时向 uiscrollview 添加和删除一个 uiwebview。意思是,当我们在启用分页时水平(向左或向右)滚动它,然后一个新 View 被添加到 uiscrollview 并遍历它。

是否可以在 uiscrollview 中检测到向左或向右滚动?

请告诉我最好的解决方案、示例代码或任何教程。

提前致谢

最佳答案

在这种情况下,我们应该在 ScrollView 中启用分页。

假设您有大小为 320x480 的 ScrollView ,它应该显示 10 页,其中每个页面的大小为 320x480,使 ScrollView 的内容大小为 320*10 x 480。

确定当前页面的最好方法是使用 ScrollView 的内容偏移值。
因此,一开始,当 ScrollView 显示第一页时,其内容偏移量将是 x=0,y=0。

对于第二页 x=320,y=0。
因此我们可以通过将 contentOffset.x 除以页面宽度来获得当前页面值。
因此,0/320 = 0,表示第一页。 320/320 = 1,表示第二页,以此类推。

因此,如果我们有当前页面值,我们可以确定 ScrollView 向哪个方向移动,如下所示:

-(void) scrollViewDidScroll:(UIScrollView *)scrollView{

int currentPageOffset = currentPage * PAGE_WIDTH;
if (self.pageScrollView.contentOffset.x >= currentPageOffset + PAGE_WIDTH) {
// Scroll in right direction. Reached the next page offset.
// Settings for loading the next page.
currentPage = self.pageScrollView.contentOffset.x/PAGE_WIDTH;
}else if (self.pageScrollView.contentOffset.x <= currentPageOffset - PAGE_WIDTH) {
// Scroll in left direction. Reached the previous page offset.
// Setting for loading the previous page.
currentPage = self.pageScrollView.contentOffset.x/PAGE_WIDTH;
}

}

关于cocoa-touch - 如何在运行时在 UIscrollview 中添加和删除 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6663404/

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