0

当用户单击某个按钮时,我需要我的应用程序来移动图像。这完美地工作:

视图控制器.m

#import "ndpViewController.h"

@interface ndpViewController ()

@property (nonatomic, retain) IBOutlet UIImageView *image;

-(void)movetheball;

@end


@implementation ndpViewController

@synthesize image;
int ballx, bally;


-(void)movetheball {
[UIView beginAnimations: @"MovingTheBallAround" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];

image.frame =  CGRectMake(ballx,bally,image.frame.size.width,image.frame.size.height);

[UIView commitAnimations];
}

- (void)viewDidLoad {
   }


- (IBAction)calculatePush:(id)sender {
    ballx = 600; 
    bally = 800; 
    [self movetheball];
}
@end

但是一旦应用程序必须执行其他操作(当用户单击按钮时),例如向标签添加文本,我会这样做:

 _lizfwLabel.text=[NSString stringWithFormat:@"%.2f",lizfw];

然后应用程序将仅在第二次单击按钮时移动图像。

知道为什么会这样吗?谢谢!

4

1 回答 1

0

尝试使用另一种方法UIView在 iOS 中为 's 设置动画。苹果建议使用[UIView animate...]而不是旧样式[UIView begin/commitAnimations];

在您的情况下,您应该将方法代码替换movetheball为该方法代码:

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    CGRect frame = image.frame;
    frame.origin.x = ballx;
    frame.origin.y = bally;
    image.frame = frame;
} completion:nil];
于 2012-10-07T22:02:41.950 回答