gpt4 book ai didi

ios - 使用 scrollViewWillEndDragging 自定义 UIScrollView 分页

转载 作者:IT王子 更新时间:2023-10-29 07:56:51 26 4
gpt4 key购买 nike

我正在尝试在 iOS 5 中使用新的 scrollViewWillEndDragging:withVelocity:targetContentOffset: UIScrollView 委托(delegate)调用,但我似乎无法让它真正正确地响应我。我正在更改 targetContentOffset->x 值,但它永远不会被使用。我知道代码正在运行,因为它会在该函数中遇到断点。我什至尝试将偏移值设置为硬编码数字,这样我就知道它最终会在哪里,但它永远不会起作用。

有没有人能够正确使用它并使它工作?是否有任何其他必须实现的委托(delegate)调用才能使其工作?

这是我的代码,以防有人发现它有问题:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
// goodOffsetX returns the contentOffset i want the scrollView to stop at
CGFloat goodOffsetX = [self _horizontalContentOffsetForTargetHorizontalContentOffset:(*targetContentOffset).x velocity:velocity.x];

NSLog( @" " );
NSLog( @"scrollViewWillEndDragging" );
NSLog( @" velocity: %f", velocity.x );
NSLog( @" currentX: %f", scrollView.contentOffset.x );
NSLog( @" uikit targetX: %f", (*targetContentOffset).x );
NSLog( @" pagedX: %f", goodOffsetX );

targetContentOffset->x = goodOffsetX;
}

最佳答案

您可以使用此代码实现自定义分页:

- (float) pageWidth {
return ((UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout).itemSize.width +
((UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout).minimumInteritemSpacing;
}

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

CGFloat pageWidth = self.collectionView.frame.size.width + 10 /* Optional Photo app like gap between images. Or use [self pageWidth] in case if you want the next page be also visible */;

_currentPage = floor((self.collectionView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

NSLog(@"Dragging - You are now on page %i", _currentPage);
}

- (void) scrollViewWillEndDragging:(UIScrollView*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint*)targetContentOffset {

CGFloat pageWidth = self.collectionView.frame.size.width + 10; // [self pageWidth]

int newPage = _currentPage;

if (velocity.x == 0) { // slow dragging not lifting finger
newPage = floor((targetContentOffset->x - pageWidth / 2) / pageWidth) + 1;
}
else {
newPage = velocity.x > 0 ? _currentPage + 1 : _currentPage - 1;

if (newPage < 0)
newPage = 0;
if (newPage > self.collectionView.contentSize.width / pageWidth)
newPage = ceil(self.collectionView.contentSize.width / pageWidth) - 1.0;
}

NSLog(@"Dragging - You will be on %i page (from page %i)", newPage, _currentPage);

*targetContentOffset = CGPointMake(newPage * pageWidth, targetContentOffset->y);
}

当然你必须设置pagingEnabled = NO。_currentPage 是一个 iVar 类。感谢http://www.mysamplecode.com/2012/12/ios-scrollview-example-with-paging.html指出正确的方法。

关于ios - 使用 scrollViewWillEndDragging 自定义 UIScrollView 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9367600/

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