2014-08-31 29 views
2

現在,這是一個奇怪的問題。一個部門的結果與objective-c和我的手動解決方案不同。很明顯,我首先認爲我的解決方案是錯誤的,但是我錯過了一些東西或者......不是。Objective-c分區與手動計算不匹配

下面的代碼:

GLfloat t = (gone - delay)/kHyAuthenticationViewControllerAnimationDuration; 
NSLog(@"%f %f %f", (gone - delay), kHyAuthenticationViewControllerAnimationDuration, t); 

這將記錄0.017853 3.500000 3.035706。這意味着0.017853/3.500000應該是3.035706,對嗎?錯誤。它實際上是0.00510085714286。數量不是那麼小,會給出精確度問題,即使如此,它可能會輪到像0.0的東西......我是否錯過了一些東西?

編輯與全碼:

- (void)animateIn:(NSTimer*)timer 
{ 
    NSArray *buttons = @[self.registrationButton];//, self.facebookButton, self.linkedInButton, self.twitterButton]; 
    NSDate *when = [timer userInfo]; 
    NSTimeInterval gone = [[NSDate date] timeIntervalSinceDate:when]; 
    NSUInteger finished = 0; 

    for (int it=0 ; it < [buttons count] ; ++it) { 

     UIButton *button = [buttons objectAtIndex:it]; 
     GLfloat toValue = self.registrationView.frame.origin.y + 37 + button.frame.size.height * it + 10 * it; 
     GLfloat fromValue = toValue + self.view.frame.size.height; 
     GLfloat delay = kHyAuthenticationViewControllerAnimateInDelayFactor * it; 
     CGRect frame; 

     // Have we waited enough for this button? 
     if (gone >= delay) { 

      // Use the timing function 
      GLfloat t = (gone - delay)/kHyAuthenticationViewControllerAnimationDuration; 
      NSLog(@"%f %f %f", (gone - delay), kHyAuthenticationViewControllerAnimationDuration, t); 

//   t = [HyEasing easeOutBounce:t]; 

      // Is the animation finished for this button? 
      if (t >= 1.0f) { 
       t = 1.0f; 
       ++finished; 
       continue; 
      } 

      // Compute current displacement 
      GLfloat displacement = fabs(toValue - fromValue); 
      GLfloat y = toValue + displacement * (1.0f - t); 

      // Create the frame for the animation 
      frame = CGRectMake(button.frame.origin.x, y, button.frame.size.width, button.frame.size.height); 
     } 

     // Make sure the button is at its initial position 
     else frame = CGRectMake(button.frame.origin.x, fromValue, button.frame.size.width, button.frame.size.height); 

     [button setFrame:frame]; 
    } 

    if (finished == [buttons count]) { 
     [timer invalidate]; 
    } 
} 
+0

我猜有什麼東西你不告訴我們。 (例如,這兩條語句之間是否有時間?) – 2014-08-31 02:55:33

+1

顯示'kHyAuthenticationViewControllerAnimationDuration'的定義。它是否是一個預處理器宏,其中沒有用圓括號括起來的複合表達式? – 2014-08-31 03:15:49

+0

該死的 - .-;你是對的!它被定義爲'0.5f + 3'(沒有括號)。我怎麼能忘記這一點。謝謝。 – 2014-08-31 03:17:39

回答

3

kHyAuthenticationViewControllerAnimationDuration是預處理宏與不是括在括號中的化合物的表達。因此,當它被合併到另一個複合表達式中時,kHyAuthenticationViewControllerAnimationDuration的術語與包含表達式的術語相比與其他術語的關聯性更強,從而改變了操作的順序。

也就是說,

(gone - delay)/kHyAuthenticationViewControllerAnimationDuration 

擴展爲:

(gone - delay)/0.5f + 3 

評價的結果,如:

((gone - delay)/0.5f) + 3 
+0

這是正確的。自從我被這件事咬了幾年之後:P謝謝。 – 2014-08-31 12:23:25