user3069232 user3069232 - 4 years ago 158
Objective-C Question

Translating Stephen Poletto SPUserResizableView [Objective C] to Swift 3.0

Using iOS 10.20 & Swift 3.0
Want to use this excellent piece of code written by Stephen Poletto a while back in my code, but need it in Swift 3.0 really.

https://github.com/spoletto/SPUserResizableView

Spent three hours on it today plodding thru, no confidence it will work, but I have to try, must be easier then reinventing the wheel no; anyway got stuck on a few constructs I hope I can find some help on in SO.

I need to translate this ...

- (void)resizeUsingTouchLocation:(CGPoint)touchPoint {
// (1) Update the touch point if we're outside the superview.
if (self.preventsPositionOutsideSuperview) {
CGFloat border = kSPUserResizableViewGlobalInset + kSPUserResizableViewInteractiveBorderSize/2;
if (touchPoint.x < border) {
touchPoint.x = border;
}


I got it in Swift, but I fear unlike objective C it seems you cannot change the value of a parameter as he seems to be doing here?

I got ..

func resizeUsingTouchLocation(touchPoint: CGPoint) {
// (1) Update the touch point if we're outside the superview.
if (self.preventsPositionOutsideSuperview) {
let border:CGFloat = CGFloat(kSPUserResizableViewGlobalInset) + CGFloat(kSPUserResizableViewInteractiveBorderSize) / 2.0;
if (touchPoint.x < border) {
touchPoint.x = border
}


It generates an error, cannot change a let property, touchPoint in this case! Got a few more, but these two in particular are phasing me ...

Answer Source

In Swift, you can not change the input parameters. Before Swift 3, you could add a var before the parameter name and then you'd have a copy that you can modify. The Swift 3 way is to add var variableName = variableName at the top of your function:

func resizeUsingTouchLocation(touchPoint: CGPoint) {
    var touchPoint = touchPoint

    // (1) Update the touch point if we're outside the superview.
    if self.preventsPositionOutsideSuperview {
        let border = CGFloat(kSPUserResizableViewGlobalInset) + CGFloat(kSPUserResizableViewInteractiveBorderSize) / 2.0
        if touchPoint.x < border {
            touchPoint.x = border
        }

I also removed unneeded type declarations, () in if statements, and a ;.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download