24

iOS 5で新しいscrollViewWillEndDragging:withVelocity:targetContentOffset:UIScrollViewデリゲート呼び出しを使用しようとしていますが、実際に正しく応答するように見えません。targetContentOffset-> xの値を変更していますが、使用されることはありません。その関数のブレークポイントにヒットするため、コードが実行されていることはわかっています。オフセット値をハードコードされた数値に設定しようとしたので、どこに到達するかはわかりますが、機能しません。

誰かがこれを正しく使用して機能させることができましたか?これが機能するために実装する必要がある他のデリゲート呼び出しはありますか?

誰かが何か問題を見つけた場合の私のコードは次のとおりです。

- (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; 
}
4

5 に答える 5

54

次のコードでカスタム ページングを実装できます。

- (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に感謝します。

于 2013-06-21T07:55:46.030 に答える
6

クイックテストを実行して、これを正しく起動し、オブジェクトを必要に応じて停止させることができました。私は次の簡単なテストを使用してこれを行いました:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
     targetContentOffset->x = scrollView.contentOffset.x - 10;    
}

このメソッドはコードの問題ではないようですが、「goodOffsetX」が停止する有効な値を正しく計算していない可能性があります。

于 2012-05-13T03:35:24.357 に答える
-1
public func scrollViewWillEndDragging(_ scrollView: UIScrollView ,withVelocity velocity: CGPoint, targetContentOffset: 
UnsafeMutablePointer<CGPoint>){  

let pageWidth = Float(appWidth + itemSpacing)
    let targetXContentOffset = Float(targetContentOffset.pointee.x)
    var newPage = Float(currentPageIndex)

    // I use this way calculate newPage:
    newPage = roundf(targetXContentOffset / pageWidth);

    //if velocity.x == 0 {
    //    newPage = floor( (targetXContentOffset - Float(pageWidth) / 2) / Float(pageWidth)) + 1.0
    //} else {
    //    newPage = Float(velocity.x > 0 ? newPage + 1 : newPage - 1)
    //    if newPage < 0 {
    //        newPage = 0
    //    }
    //    if (newPage > contentWidth / pageWidth) {
    //        newPage = ceil(contentWidth / pageWidth) - 1.0
    //   }
    //}

    let targetOffsetX = CGFloat(newPage * pageWidth)
    let point = CGPoint (x: targetOffsetX, y: targetContentOffset.pointee.y)
    targetContentOffset.pointee = point
 }
于 2017-07-14T12:02:43.723 に答える