gpt4 book ai didi

ios - scrollViewWillEndDragging 吸附到 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:23 25 4
gpt4 key购买 nike

我找遍了所有地方,但找不到答案。所以,开始吧。我创建了一个长 ScrollView ,里面有 568 像素(全屏) View 。当用户向下滚动时,我希望 ScrollView 捕捉到特定位置。这是我的代码:

    -(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
if (aboutScroller.contentOffset.y >= 468 && aboutScroller.contentOffset.y <= 668) {
targetContentOffset->y = 568;
} else if (aboutScroller.contentOffset.y >= 1036 && aboutScroller.contentOffset.y <= 1236) {
targetContentOffset->y = 1136;
} else if (aboutScroller.contentOffset.y >= 1604 && aboutScroller.contentOffset.y <= 1804) {
targetContentOffset->y = 1704;
}
}

问题是它没有惯性地捕捉(当我抬起手指时),但是当我滑动滚动时,它不会捕捉。我猜我可以使用 velocity 来处理这个问题,但我不知道该怎么做。任何帮助让它快速滑动的帮助都将不胜感激!

最佳答案

对于需要解决此问题的任何其他人,Daniel 已在上面进行了更正,但我只想说出我自己的话。

首先,如果用户不随惯性滚动,则添加设置 contentOffset 的方法:

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
if (_aboutScroller.contentOffset.y >= 468 && _aboutScroller.contentOffset.y <= 668) {
targetContentOffset->y = 568;
} else if (_aboutScroller.contentOffset.y >= 1036 && _aboutScroller.contentOffset.y <= 1236) {
targetContentOffset->y = 1136;
} else if (_aboutScroller.contentOffset.y >= 1604 && _aboutScroller.contentOffset.y <= 1804) {
targetContentOffset->y = 1704;
} else if (_aboutScroller.contentOffset.y >= 2172 && _aboutScroller.contentOffset.y <= 2372) {
targetContentOffset->y = 2272;
}
}

接下来,如果用户确实以惯性滚动,则添加设置 contentOffset 的方法:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (_aboutScroller.contentOffset.y >= 468 && _aboutScroller.contentOffset.y <= 668) {
[_aboutScroller setContentOffset:CGPointMake(0, 568) animated:YES];
} else if (_aboutScroller.contentOffset.y >= 1036 && _aboutScroller.contentOffset.y <= 1236) {
[_aboutScroller setContentOffset:CGPointMake(0, 1136) animated:YES];
} else if (_aboutScroller.contentOffset.y >= 1604 && _aboutScroller.contentOffset.y <= 1804) {
[_aboutScroller setContentOffset:CGPointMake(0, 1704) animated:YES];
} else if (_aboutScroller.contentOffset.y >= 2172 && _aboutScroller.contentOffset.y <= 2372) {
[_aboutScroller setContentOffset:CGPointMake(0, 2272) animated:YES];
}
}

关于ios - scrollViewWillEndDragging 吸附到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23047816/

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