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

objective-c, scrollview, swift, uicollectionview, xcode

Solution

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

Problem

How would one write this in 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 `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]; } ```

Original source