Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

ios - targetContentOffset in ScrollViewWillEndDragging: is mostly returning zero

The UIScrollView delegate method scrollViewWillEndDragging is sometimes returning zero in targetContentOffset argument.

This is specially happening when i drag the scroll view to approximately in the middle of two views. Because it is returning zero what is happening is that instead of snapping to the next or previous page the scrollView gets stuck in between the two views.

My method implementation is

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

        let pageWidth:CGFloat = self.view.frame.width
        let currentOffset = scrollView.contentOffset.x
        let targetOffset = CGFloat(targetContentOffset.pointee.x)
        var newTargetOffset:CGFloat = 0.0

        if targetOffset > currentOffset {
            newTargetOffset = CGFloat(ceilf(Float((currentOffset / pageWidth) * pageWidth)))
        }
        else {
            newTargetOffset = CGFloat(floorf(Float((currentOffset / pageWidth) * pageWidth)))
        }

        if newTargetOffset < 0.0 {
            newTargetOffset = 0.0
        }
        else if newTargetOffset > scrollView.contentSize.width {
            newTargetOffset = scrollView.contentSize.width
        }
        targetContentOffset.pointee = CGPoint(x: newTargetOffset, y: 0.0)

    }

and this is my viewDidLoad method

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        scrollView.isPagingEnabled = true
        scrollView.contentSize = CGSize(width: self.view.frame.width * 2.0 + 100, height: 273)
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        
        for (index,testFeature) in test.enumerated() {
            var view = UIView(frame: CGRect.zero)
            view.backgroundColor = UIColor(hue: CGFloat(drand48()), saturation: 1, brightness: 1, alpha: 1)
            view.frame.size.width = self.view.frame.size.width
            view.frame.size.height = 273
            view.frame.origin.x = CGFloat(index) * self.view.frame.size.width
            view.frame.origin.y = 0
            view.tag = index
            
            scrollView.addSubview(view)
               
        }
        
        
        loadFeatures()
    }

Any kind of help is greatly appreciated.

question from:https://stackoverflow.com/questions/65929764/targetcontentoffset-in-scrollviewwillenddragging-is-mostly-returning-zero

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...