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

标签 objective-c swift translation

使用 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 语句中的 ();

关于objective-c - 将 Stephen Poletto SPUserResizableView [ Objective-C ] 翻译成 Swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42056697/

相关文章:

ios - 使用 xcode 中的 Core Plot 将 UIView 中的轴居中

swift - macOS 应用程序中的表单验证问题

ios - 将项目从 ObjC 移植到 Swift 后应用程序无法启动

ios - 扩展不会 swift 进入派生类

symfony - Twig Symfony 5 中的复数

.net - 翻译大量文本数据的最佳方法是什么?

ios - UITextField 文本更改事件

ios - 在 'levelCompleted' 类型的对象上找不到属性 'id'

ios - 如何从iOS单元测试中获取不同的info-plist?

r - 格式化 lubridate 的 as.period() 的输出