我怎样才能正确改变物体的位置?

时间:2012-06-04 21:16:36

标签: objective-c position frame cgpoint

我在for循环中使用一些函数来计算对象的新位置:

...some code....

for (int i = self.frame.origin.x; i <= 710; ++i) {
    CGPoint newP = CGPointMake(i, 1);
    float posY = a * pow((newP.x - w.x),2) - w.y;

    CGPoint result = CGPointMake(i, posY);
    [NSThread sleepForTimeInterval:1];
    self.frame = CGRectMake(result.x, result.y, self.frame.size.width, self.frame.size.height);
    NSLog(@"[%f, %f]", result.x, result.y);
}

,但对象仍然存在。

控制台打印:

2012-06-04 23:13:19.183 PERT Estimator[326:707] [304.000000, 169.499985]
2012-06-04 23:13:20.195 PERT Estimator[326:707] [305.000000, 165.172806]
2012-06-04 23:13:21.197 PERT Estimator[326:707] [306.000000, 160.856293]
2012-06-04 23:13:22.200 PERT Estimator[326:707] [307.000000, 156.550461]
2012-06-04 23:13:23.225 PERT Estimator[326:707] [308.000000, 152.255295]
2012-06-04 23:13:24.265 PERT Estimator[326:707] [309.000000, 147.970810]

等...

出了什么问题?

1 个答案:

答案 0 :(得分:1)

哇,你真的应该使用动画块。

/* Starting position  */
[self setFrame:CGRectMake(304, 169.499985,self.frame.size.width, self.frame.size.height)];

[UIView animateWithDuration:1.0
                      delay:0.0 
                    options:UIViewAnimationCurveEaseOut 
                 animations:^ 
                 {
                     [self setFrame:CGRectMake(309, 147, 320,110)];
                 } 
                 completion:^(BOOL finished) 
                 {
                     if ( finished )
                         /* Animation is done, do something, or not */
                         NSLog(@"[%f, %f]", result.x, result.y);
                }];