2014-08-31 34 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