- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试在 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/
我在 TabelViewCell 中有 CollectionView,我正在尝试为 CollectionView 的单元格进行分页和居中。但是当我使用方法时 func scrollViewWillEn
我有一个 View 覆盖在 UITableView 的顶部。但是当用户在顶部 View 上拖动时,我需要触发 UITableView 事件的滚动事件。 我在 View 上添加了 UIPanGestur
我找遍了所有地方,但找不到答案。所以,开始吧。我创建了一个长 ScrollView ,里面有 568 像素(全屏) View 。当用户向下滚动时,我希望 ScrollView 捕捉到特定位置。这是我的
我正在为我的 UICollectionView 创建自定义分页。我希望底部的一些单元格卡在屏幕边缘,但是,通过常规分页,滚动到下一页意味着如果页面底部的一半单元格显示,它只会显示另一半在下一页。我想让
documentation声明它是 points(每秒,我假设)?但是,我得到的值在 (.5, 3.5) 范围内。然后 ScrollView 在停止之前移动了数百个点。实际初速度应该相差几个数量级。
这是我的覆盖代码 - 它只是计算捕捉到的位置: - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(C
我正在为 targetContentOffset 设置一个新值在 scrollViewWillEndDragging(_:withVelocity:targetContentOffset:)在 UIT
我希望我的 UIScrollView 以特定的增量值自然地滑动到结尾,对应于水平滚动 UIScrollView 的每 50 个宽度点。为此,我定制的 scrollViewWillEndDragging
我正在尝试在 iOS 5 中使用新的 scrollViewWillEndDragging:withVelocity:targetContentOffset: UIScrollView 委托(deleg
我在一个 View Controller 上有两个 UITableViews( View Controller 是他们的代表)。其中之一将取决于 scrollViewWillEndDragging:w
我在同一个 View Controller 上使用 UITableView 和 UICollectionView。 我想改变 UICollectionView 的滚动方式,所以我在扩展中添加了一个 s
我正在尝试使用 scrollViewWillEndDragging: 方法来滚动我自己的分页 UICollectionView,在两侧都有预览,类似于 App Store 应用程序。但是,如果我停止无
我正在尝试实现一个自定义标签栏,它是可滚动的并且在每个标签栏项目上都有分页。为此,我正在使用代理 scrollViewWillEndDragging:withVelocity:targetConten
如何用 Swift 编写此代码? 更新:发布了我试图转换的方法,最初发布了一个不同的方法,这不是我试图隐藏的方法 这适用于 Objective C,但不适用于 Swift。当我尝试将 ceilf 和
我希望我的垂直 UIScrollView 仅在用户向上滚动时停在最大内容偏移处,或者在用户向下滚动时停在最小内容偏移处。 我正在使用以下两个函数 func targetContentOffset(fo
我是一名优秀的程序员,十分优秀!