Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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


}
like image 300
Aubrey Avatar asked Feb 17 '15 06:02

Aubrey


People also ask

What happens if you don’t write self in Swift?

If you don’t explicitly write self, Swift assumes that you are referring to a property or method of the current instance whenever you use a known property or method name within a method. This assumption is demonstrated by the use of count (rather than self.count) inside the three instance methods for Counter.

What is the difference between Objective-C and Swift?

The fact that structures and enumerations can define methods in Swift is a major difference from C and Objective-C. In Objective-C, classes are the only types that can define methods. In Swift, you can choose whether to define a class, structure, or enumeration, and still have the flexibility to define methods on the type you create.

What is a value type in Swift?

All structures and enumerations are value types in Swift. This means that any structure and enumeration instances you create—and any value types they have as properties—are always copied when they’re passed around in your code.

How do you define a method in Swift?

In Swift, you can define type-level methods for all classes, structures, and enumerations. Each type method is explicitly scoped to the type it supports. Type methods are called with dot syntax, like instance methods. However, you call type methods on the type, not on an instance of that type.


1 Answers

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

Below there is a more suitable answer for the problem, fully working with 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)
}
like image 154
valvoline Avatar answered Oct 08 '22 19:10

valvoline