2013-10-04 32 views
4

我想通過iCarousel庫爲我的iPad應用程序帶來像放大效果的碼頭。有了這個,我可以用下面的一段代碼放大傳送帶的中心項目,但試圖縮放中心項目的相鄰項目,縮放級別略低於中心項目。Mac Dock像放大的iPad

- (CATransform3D)carousel:(iCarousel *)_carousel itemTransformForOffset: 
         :(CGFloat)offset baseTransform:(CATransform3D)transform 
{ 
    CGFloat MAX_SCALE = 1.95f; //max scale of center item 
    CGFloat MAX_SHIFT = 40.0f; //amount to shift items to keep spacing the same 

    CGFloat shift = fminf(1.0f, fmaxf(-1.0f, offset)); 
    CGFloat scale = 1.0f + (1.0f - fabs(shift)) * (MAX_SCALE - 1.0f); 
    transform = CATransform3DTranslate(transform, 
    offset * _carousel.itemWidth * 1.08f + shift * MAX_SHIFT, 0.0f, 0.0f); 
    return CATransform3DScale(transform, scale, scale, scale); 
} 

期待任何形式的幫助。謝謝。

回答

13

這個功能可能是你的答案:

enter image description here

其圖(用於scaleMax = 3,xFactor = 1):

enter image description here

此功能被直接用於計算從轉盤偏移比例因子。此外,您需要將元素左右移動,以避免重疊(就像您已經做過的那樣)。這可以通過將函數的積分移動來完成,這可以起作用,但是中心的差距非常大。或者可以通過獲取所有縮放項目的總和來手動計算。差距可以保持不變,或者可以分開調整。

請注意,比例尺在中心等於1,並通過邊緣下降到1/scale_max。這是因爲縮小並不會產生不良的像素化效果。讓您的項目視圖按照您希望的方式顯示在中心,並且邊緣視圖將縮小。

這可能是用法:

-(CGFloat) scaleForX:(CGFloat)x xFactor:(CGFloat)xFactor centerScale:(CGFloat)centerScale 
{ 
    return (1+1/(sqrtf(x*x*x*x*xFactor*xFactor*xFactor*xFactor+1))*(centerScale-1.0))/centerScale; 
} 

- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform 
{ 
    //items in the center are scaled by this factor 
    const CGFloat centerScale = 4.0f; 
    //the larger the xFactor, the smaller the magnified area 
    const CGFloat xFactor = 1.5f; 
    //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; 
} 

與結果: enter image description here

你可以試着改變爲不同的行爲常量。另外將指數改變爲另一個偶數可以進一步擴大峯值並將下降銳化到最小比例。

+0

太棒了!謝謝! – XiOS

+0

當我再次查看底座放大倍數時,該功能應該具有更寬的峯值。你可以嘗試去混淆它,間距應該始終保持不變。 – burax

0

你需要觀看第219集從WWDC 2012 - 高級集合視圖和建築物自定義佈局。我知道它涉及到收集的意見,但我相信你會找到一個方法來適應這些代碼:)