将 Stephen Poletto SPUserResizableView [Objective C] 翻译成 Swift 3.0

Translating Stephen Poletto SPUserResizableView [Objective C] to Swift 3.0

使用 iOS 10.20 & Swift 3.0 想在我的代码中使用这段由 Stephen Poletto 编写的优秀代码,但在 Swift 3.0 中确实需要它。

https://github.com/spoletto/SPUserResizableView

今天拖了三个小时,没信心能成功,但我必须尝试,一定比重新发明轮子更容易不;无论如何都卡在了一些结构上,我希望我能在 SO 中找到一些帮助。

我需要翻译这个...

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

我在 Swift 中得到了它,但我担心与 objective C 不同,您似乎不能像他在这里所做的那样更改参数的值?

我得到了..

  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
        }

它会产生一个错误,在这种情况下不能更改 let 属性,touchPoint!还有一些,但这两个特别让我分阶段...

在Swift中,您不能更改输入参数。在 Swift 3 之前,您可以在参数名称前添加一个 var,然后您将拥有一个可以修改的副本。 Swift 3 方法是在函数顶部添加 var variableName = variableName

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
        }

我还删除了不需要的类型声明,if 语句中的 (),以及 ;.