iPhone UIScrollView Speed Check

后端 未结 8 1628
梦如初夏
梦如初夏 2020-11-30 19:31

I know how to get the contentOffset on movement for a UIScrollView, can someone explain to me how I can get an actual number that represents the current speed of a UIScrollV

相关标签:
8条回答
  • 2020-11-30 20:12

    Here is another smart way to do this in SWIFT :-

    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        if velocity.y > 1.0 || velocity.y < -1.0 && self.sendMessageView.isFirstResponder() {
            // Somthing you want to do when scrollin fast.
            // Generally fast Vertical scrolling.
        }
    }
    

    So if you scrolling vertically you should use velocity.y and also if you are scrolling horizontally you should use velocity.x . Generally if value is more than 1 and less than -1, it represent generally fast scrolling. So you can change the speed as you want. +value means scrolling up and -value means scrolling down.

    0 讨论(0)
  • 2020-11-30 20:17

    You can see PageControl sample code about how to get the contentOffset of scrollview.

    The contentOffset on movement can be obtained from UIScrollViewDelegate method, named - (void)scrollViewDidScroll:(UIScrollView *)scrollView, by querying scrollView.contentOffset. Current speed can be calculated by delta_offset and delta_time.

    • Delta_offset = current_offset - pre_offset;
    • Delta_time = current_time - pre_time;
    0 讨论(0)
提交回复
热议问题