在 Swift 中,您将如何编写 scrollViewWillEndDragging withVelocity targetContentOffset 方法? [英] In Swift, how would you write the scrollViewWillEndDragging withVelocity targetContentOffset method?

查看:114
本文介绍了在 Swift 中,您将如何编写 scrollViewWillEndDragging withVelocity targetContentOffset 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用 Swift 写这个?

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

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

这适用于 Objective C,但不适用于 Swift.当我尝试将 ceilffloorfCGFloat 一起使用时,出现转换错误.

This works in Objective C, but not Swift. I get conversion errors when I try to use ceilf and floorf with CGFloat.

    - (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];


}

推荐答案

Christian 的回答:我能够用 Swift 重写它.我将所有东西都设置为 Float 来做数学运算,然后像 Christian 提到的那样转换为 CGFloat 是可能的.他的回答在技术上是正确的,但向新的 iOS 开发人员展示是如何用 Swift 编写正是我想要的.

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

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

这篇关于在 Swift 中,您将如何编写 scrollViewWillEndDragging withVelocity targetContentOffset 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆