Difference when converting method from Objective-C to Swift

358 views Asked by At

This is my method in 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;
}

This is how it looks:

enter image description here

This is how I changed it to 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;
}

This is how it looks:

enter image description here

1

There are 1 answers

3
Kaushil Ruparelia On

Check the 3rd last line:

var newTransform = CATransform3DTranslate(transform, scaleOffset * carousel.itemWidth, 0.0, 0.0);

newTransform = CATransform3DScale(newTransform, scale, scale, 1.0)

Cheers