【问题标题】:How to Calculate Scrollview width and height on every scroll in iOS如何计算 iOS 中每个滚动条的 Scrollview 宽度和高度
【发布时间】:2014-05-13 04:50:09
【问题描述】:

我有 Scrollview 用于显示图像以及两个按钮 previous 和 next。

现在我的问题是当用户滚动图像时,我想确定它是从右到左滚动还是反之亦然。为此,我必须计算滚动视图contentOffset 和滚动视图frame size,但我不知道如何计算这些值。 现在我还在用:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0){}

这个方法。

需要任何建议。

【问题讨论】:

    标签: ios uiscrollview cgpoint cgfloat


    【解决方案1】:
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView1 willDecelerate:(BOOL)decelerate;
    {
        CGFloat pageWidth = scrollView.frame.size.width;
    
        float fractionalPage = scrollView.contentOffset.x / pageWidth;
    
        NSInteger page = lround(fractionalPage);
    
        if (previousPage != page){
    
            if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].x > 0){
    
                NSLog(@"-1");
            } else {
    
                NSLog(@"+1");
            }
        }
    }
    

    【讨论】:

    • 您好,感谢您的解决方案但是当用户完全拖动时,如果用户在到达下一个图像之前停止拖动,它不应该执行该操作,但使用此代码它会执行此操作。我正在使用 count 变量 +/- 基于从左到右的动作。所以如果用户停止拖动最初的计数值也会发生变化。我希望你能理解我的问题。
    • @siri 请检查答案
    • NSInteger previousPage; .h 文件
    • 知道了..谢谢@Sunny Shah
    【解决方案2】:

    嗨,如果你是水平滚动,你会知道你是下一个还是上一个,这取决于velocity.x

    velocity.x > 0 ?(int) _currentIndex + 1 :(int) _currentIndex - 1;

    -(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    
        CGFloat xAxisVisible= self.uiScrollDetails.contentOffset.x;
        self.currentIndex=xAxisVisible/320;
    
        NSLog(@"Dragging - You are now on page %i",(int) self.currentIndex);
    }
    

    【讨论】:

    • 感谢您的回复。让我知道如何获取 currentIndex 值。
    猜你喜欢
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    相关资源
    最近更新 更多