将方法从 Objective-C 转换为 Swift 时的区别
Posted
技术标签:
【中文标题】将方法从 Objective-C 转换为 Swift 时的区别【英文标题】:Difference when converting method from Objective-C to Swift 【发布时间】:2015-06-15 09:48:29 【问题描述】:这是我在 Objective-C 中的方法:
- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
//items in the center are scaled by this factor
const CGFloat centerScale = 5.0f;
//the larger the xFactor, the smaller the magnified area
const CGFloat xFactor = 0.65f;
//should the gap also be scaled? or keep it constant.
const BOOL scaleGap = NO;
const CGFloat spacing = [self carousel:carousel valueForOption:iCarouselOptionSpacing withDefault:1.025];
const CGFloat gap = scaleGap?0.0:spacing-1.0;
//counting x offset to keep a constant gap
CGFloat scaleOffset = 0.0;
float x = fabs(offset);
for(;x >= 0.0; x-=1.0)
scaleOffset+=[self scaleForX:x xFactor:xFactor centerScale:centerScale];
scaleOffset+= ((x>=1.0)?gap:x*gap);
scaleOffset -= [self scaleForX:offset xFactor:xFactor centerScale:centerScale]/2.0;
scaleOffset += (x+0.5)*[self scaleForX:(x+(x>-0.5?0.0:1.0)) xFactor:xFactor centerScale:centerScale];
scaleOffset *= offset<0.0?-1.0:1.0;
scaleOffset *= scaleGap?spacing:1.0;
CGFloat scale = [self scaleForX:offset xFactor:xFactor centerScale:centerScale];
transform = CATransform3DTranslate(transform, scaleOffset*carousel.itemWidth, 0.0, 0.0);
transform = CATransform3DScale(transform, scale, scale, 1.0);
return transform;
看起来是这样的:
这就是我将其更改为 Swift 的方式:
func carousel(carousel: iCarousel!, itemTransformForOffset offset: CGFloat, baseTransform transform: CATransform3D) -> CATransform3D
let centerScale : CGFloat = 5.0
let xFactor : CGFloat = 0.65
let scaleGap : Bool = false
let spacing : CGFloat = self.carousel(carousel, valueForOption: iCarouselOption.Spacing, withDefault: 1.025)
let gap : CGFloat = scaleGap ? 0 : spacing - 1.0
var scaleOffset : CGFloat = 0.0
var x : CGFloat = fabs(offset)
for (;x >= 0.0 ; x -= 1.0)
scaleOffset += NumberScroller.scaleForX(x, xFactor: xFactor, centerScale: centerScale)
scaleOffset += ((x >= 1.0) ? gap : x * gap)
scaleOffset -= NumberScroller.scaleForX(offset, xFactor: xFactor, centerScale: centerScale)
scaleOffset += (x + 0.5) * NumberScroller.scaleForX((x + ( x > -0.5 ? 0.0 : 1.0 )), xFactor: xFactor, centerScale: centerScale)
scaleOffset *= offset < 0.0 ? -1.0 : 1.0
scaleOffset *= scaleGap ? spacing : 1.0
var scale : CGFloat = NumberScroller.scaleForX(offset, xFactor: xFactor, centerScale: centerScale)
var newTransform : CATransform3D = transform
newTransform = CATransform3DTranslate(transform, scale, scale, 1.0)
newTransform = CATransform3DScale(transform, scale, scale, 1.0)
return newTransform;
看起来是这样的:
【问题讨论】:
第一个 CATransform3DTranslate(倒数第三行)不同 【参考方案1】:检查倒数第三行:
var newTransform = CATransform3DTranslate(transform, scaleOffset * carousel.itemWidth, 0.0, 0.0);
newTransform = CATransform3DScale(newTransform, scale, scale, 1.0)
干杯
【讨论】:
感谢您的回答,但它仍然无法正常工作.. 看起来一样 您是根据旧的变换值而不是执行CATransform3DTranslate
后生成的新值进行缩放
您好,请问您可以发送 scaleForX 的代码吗?我需要实现同样的事情以上是关于将方法从 Objective-C 转换为 Swift 时的区别的主要内容,如果未能解决你的问题,请参考以下文章
将json调用语法从Objective-C转换为Swift的正确方法是啥?
如何将 .hour() 和 .minute() 从 Swift 转换为 Objective-C?
将 Objective-C 方法转换为 Swift [关闭]
再次将 textKit 从 Objective-C 转换为 Swift