使用 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/