2014-02-27 65 views
2

在我的觀點之一,我有一個底欄。點擊此視圖可在屏幕中途將臨時購物車動畫化。現在,在臨時表中沒有任何項目的情況下,我將顯示一個沒有數據疊加層,它只是一個帶有空白購物車圖像和下方文本的視圖。動畫自定義視圖半邊屏幕中的圖像 - 突然動畫

問題是:當此表動畫展開和摺疊時,在動畫過程中無數據疊加看起來不太好。看起來,圖像也在縮小/擴大,直到臨時表視圖停止動畫。

所以,我試圖通過玩臨時表視圖的alpha,而這種動畫發生時,看起來並不那麼糟糕。但是這也沒有幫助。最後有一個突然的閃光。

有什麼建議嗎?

self.temporaryTable.backgroundView = self.noDataOverlay; 

[UIView animateWithDuration:0.5 animations:^{ 
    self.temporaryTable.alpha = 0.5; 
} completion:^(BOOL finished) { 
    self.temporaryTable.alpha = 1.0; 
}]; 

這裏是我的繪製圖像代碼:

- (void)drawRect:(CGRect)iRect scalingImage:(BOOL)iShouldScale { 
     CGRect aRect = self.superview.bounds; 
     // Draw our image 
     CGRect anImageRect = iRect; 
     if (self.image) { 
      //scale the image based on the height 
      anImageRect = CGRectZero; 
      anImageRect.size.height = self.image.size.height; 
      anImageRect.size.width = self.image.size.width; 
    #ifdef ACINTERNAL 
      if (iShouldScale) { 
       anImageRect = [self aspectFittedRect:anImageRect max:aRect]; 
      } else { 
       anImageRect.origin.x = (iRect.size.width/2) - (anImageRect.size.width/2); 
       anImageRect.origin.y = kTopMargin; 
      } 
    #else 
      anImageRect.origin.x = (iRect.size.width/2) - (anImageRect.size.width/2); 
      anImageRect.origin.y = kTopMargin; 
    #endif 
      if (self.shouldCenterImage) 
       anImageRect.origin.y = (iRect.size.height/2) - (anImageRect.size.height/2); 

      [self.image drawInRect:anImageRect]; 
     } else { 
      anImageRect.origin.y = (iRect.size.height/6); 
      anImageRect.size.height = (iRect.size.height/6); 
     } 

     // Draw our title and message 
     if (self.title) { 
      CGFloat aTop = anImageRect.origin.y + anImageRect.size.height + kSpacer; 
      CGFloat aWidth = aRect.size.width; 
      UIColor *aColor = [UIColor colorWithRed:96/256.0 green:106/256.0 blue:122/256.0 alpha:1]; 
      [aColor set];  
      CGRect aTextRect = CGRectMake(0, aTop, aWidth, kTitleHeight * 2); 
      UIFont *aTitleFont = [UIFont boldSystemFontOfSize:kTitleFontSize]; 
      [self.title drawInRect:aTextRect withFont:aTitleFont lineBreakMode:NSLineBreakByClipping alignment:NSTextAlignmentCenter]; 

      if (self.subTitle) { 
       UIFont *aSubTitleFont = [UIFont systemFontOfSize:kSubTitleFontSize]; 
       aTextRect = CGRectMake(0, aTop+kSpacer, aWidth, kTitleHeight); 
       [self.subTitle drawInRect:aTextRect withFont:aSubTitleFont lineBreakMode:NSLineBreakByClipping alignment:NSTextAlignmentCenter]; 
      } 
     } 
    } 


- (void)addToView:(UIView *)iView { 
    // setting a unique tag number here so that if the user has any other tag there should not be a conflict 
    UIView *aView = [iView viewWithTag:699]; 
    if (aView) { 
     [aView removeFromSuperview]; 
    } 
    self.tag = 699; 
    [iView addSubview:self]; 
} 


- (void)removeView { 
    [super removeFromSuperview]; 
} 


-(void)setViewFrame:(CGRect) iFrame { 
    CGRect aRect = self.frame; 
    aRect.size.width = iFrame.size.width; 
    aRect.size.height = iFrame.size.height; 
    self.frame = aRect; 
    [self setNeedsDisplay]; 
} 


- (CGRect)aspectFittedRect:(CGRect)iRect max:(CGRect)iMaxRect { 
    float anOriginalAspectRatio = iRect.size.width/iRect.size.height; 
    float aMaxAspectRatio = iMaxRect.size.width/iMaxRect.size.height; 

    CGRect aNewRect = iMaxRect; 
    if (anOriginalAspectRatio > aMaxAspectRatio) { // scale by width 
     aNewRect.size.height = iMaxRect.size.width * iRect.size.height/iRect.size.width; 
    } else { 
     aNewRect.size.width = iMaxRect.size.height * iRect.size.width/iRect.size.height; 
    } 
    aNewRect.origin.y = (iMaxRect.size.height - aNewRect.size.height)/2.0; 
    aNewRect.origin.x = (iMaxRect.size.width - aNewRect.size.width)/2.0; 

    return CGRectIntegral(aNewRect); 
} 

回答

2

一種可能性是固定問題,即一個事實,即空的購物車圖像展開/摺疊的觀點激發。沒有看到你的代碼很難解決這個問題,但在我自己的代碼中,我所做的就是將UIImageView的contentMode設置爲UIViewContentModeBottom。這會使圖像保持相同的大小,即使包含它的圖像視圖可能會隨着動畫的一部分而增長和縮小。

+0

這聽起來很有前途,正是我所期待的。讓我試試這個。 Upvoting! – Abhinav

+0

顯然我們沒有使用UIImageView。我們正在使用核心圖形繪製它。我們在覈心圖形中有contentMode嗎? – Abhinav

+0

即使您正在使用核心圖形,您也必須以某種方式將圖像插入界面。如果您使用自定義的UIView,則答案是_ content_mode,但細節取決於您的繪圖代碼,但仍未顯示。 – matt

2

你看到閃光燈,因爲你動畫阿爾法高達0.5和動畫完成,然後當你把它設置爲從0.51.0。只是動畫阿爾法高達1.0

這裏
[UIView animateWithDuration:0.5 
       animations:^ 
{ 
    self.temporaryTable.alpha = 1.0; 
}]; 
+0

試過這個。當桌子上來時,它看起來不太好,我可以看到它背後的景象。這是因爲阿爾法正在改變。有沒有其他方法可以避免圖像內部圖像的膨脹/縮小? Upvoting! – Abhinav