xcode 在 Swift 中,您将如何编写 scrollViewWillEndDragging withVelocity targetContentOffset 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28556317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:39:56  来源:igfitidea点击:

In Swift, how would you write the scrollViewWillEndDragging withVelocity targetContentOffset method?

objective-cxcodeswiftuicollectionviewscrollview

提问by Aubrey

How would one write this in Swift?

怎么用Swift写这个?

Update: posted the method I'm trying to convert, originally posted a different method that was not what I was trying to covert

更新:发布了我试图转换的方法,最初发布了一个不同的方法,这不是我试图隐藏的

This works in Objective C, but not Swift. I get conversion errors when I try to use ceilfand floorfwith CGFloat.

这适用于 Objective C,但不适用于 Swift。我得到的转换错误,当我尝试使用ceilf,并floorfCGFloat

    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{

    float pageWidth = 200 + 30; // width + space

    float currentOffset = scrollView.contentOffset.x;
    float targetOffset = targetContentOffset->x;
    float newTargetOffset = 0;

    if (targetOffset > currentOffset)
        newTargetOffset = ceilf(currentOffset / pageWidth) * pageWidth;
    else
        newTargetOffset = floorf(currentOffset / pageWidth) * pageWidth;

    if (newTargetOffset < 0)
        newTargetOffset = 0;
    else if (newTargetOffset > scrollView.contentSize.width)
        newTargetOffset = scrollView.contentSize.width;

    targetContentOffset->x = currentOffset;
    [scrollView setContentOffset:CGPointMake(newTargetOffset, 0) animated:YES];


}

回答by valvoline

Maybe later for the party, but none of the proposed solutions are viable solution if dealing with iOS11 + Swift4, IMHO.

也许晚一些,但如果处理 iOS11 + Swift4,恕我直言,所提出的解决方案都不是可行的解决方案。

Below there is a more suitable answer for the problem, fully working with Swift 4.1:

下面有一个更适合该问题的答案,完全使用Swift 4.1

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

    let pageWidht:CGFloat = 200.0 + 30.0
    let currentOffset = scrollView.contentOffset.x
    let targetOffset = CGFloat(targetContentOffset.pointee.x)
    var newTargetOffset:CGFloat = 0.0

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

    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)
}

回答by Aubrey

To answer from Christian: I was able to rewrite this in Swift. I set everything up as Floatto do the math then converted to CGFloatas Christian mentioned was possible. His answer is technically correct, but showing a newer iOS developer how this is writtenin Swift was what I was looking for.

来自 Christian 的回答:我能够用 Swift 重写它。我把一切都设置为Float做数学然后转换CGFloat为克里斯蒂安提到的可能。他的回答在技术上是正确的,但向新的 iOS 开发人员展示这如何Swift编写的正是我正在寻找的。

func scrollViewWillEndDragging(scrollView: UIScrollView!, withVelocity velocity: CGPoint, targetContentOffset: UnsafePointer<CGPoint>) {

     var pageWidth = Float(200 + 30)
     var currentOffset = Float(scrollView.contentOffset.x)
     var targetOffset = Float(targetContentOffset.memory.x)
     var newTargetOffset = Float(0)
     var scrollViewWidth = Float(scrollView.contentSize.width)

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

     if newTargetOffset < 0 {
         newTargetOffset = 0
     } else if newTargetOffset > currentOffset {
         newTargetOffset = currentOffset
     }

     Float(targetContentOffset.memory.x) == currentOffset

     scrollView.setContentOffset(CGPointMake(CGFloat(newTargetOffset), 0), animated: true)
 }

回答by Christian

You can use ceilor floor. Apple now allows CGFloatas parameter in common functions like these. For example:

您可以使用ceilfloor。Apple 现在允许CGFloat在这些常用函数中作为参数使用。例如:

func ceil(x: CGFloat) -> CGFloat

So in your example:

所以在你的例子中:

ceil(currentOffset/pageWidth)

回答by ruandao

well, that's not work for me, and i get a compromise method, use the scrollViewDidEndDecelerating

好吧,这对我不起作用,我得到了一个折衷的方法,使用 scrollViewDidEndDecelerating

    var targetOffSet:CGPoint?
    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        var offsetX:CGFloat = targetContentOffset.memory.x
        offsetX -= someValue
        self.targetOffSet = CGPoint(x: offsetX, y: 0)
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    if self.targetOffSet != nil {
        scrollView.setContentOffset(self.targetOffSet!, animated: true)
        self.targetOffSet = nil
    }
}