需要更正我的应用移动图像的方式

时间:2012-10-07 18:59:57

标签: ios xcode

当用户点击某个按钮时,我需要我的应用移动图片。这非常有效:

ViewController.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];

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

有关为什么会发生这种情况的任何想法?谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用另一种方法在iOS中为UIView制作动画。 Apple建议使用[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];