我制作了一款非常简单的游戏。我有一个UIButton和一个UIImageView。我试图做的是,当用户按下按钮时,它会将图像更改为另一个图像,然后返回到原始图像,以模拟动画(角色移动)
我有这段代码:
file.m
{
IBOutlet UIImageView *image1;
IBOutlet UIButton *button;
}
-(IBAction)button:(id)sender;
file.h
-(IBAction)button:(id)sender{
[UIView animateWithDuration:1.0f
animations:^{
image1.alpha = 0.1f;
} completion:^(BOOL finished) {
image1.image = [UIImage imageNamed:@"image2.png"];
[UIView animateWithDuration:0.2f
animations:^{
image1.alpha = 1.0f;
} completion:^ (BOOL finished) {
image1.image = [UIImage imageNamed:@"image1.png"];
}];
}];
}
当我打开iOS模拟器并按下按钮时,图像会消失,然后重新出现然后再动画。问题是我不想让它解散,我只想展示动画。我该如何防止这种情况发生?是因为我使用了alpha属性吗?
谢谢!
答案 0 :(得分:1)
我使用以下代码执行我 认为 所需要的内容(如下所示:https://www.dropbox.com/s/vtcyw0n257tgihx/fade.mov?dl=0):
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(test)];
iv = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];
iv.image = [UIImage imageNamed:@"red"];
[self.view addSubview:iv];
}
- (void)test {
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 0.0f;
} completion:^(BOOL finished) {
iv.image = [UIImage imageNamed:@"blue"];
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 1.0f;
} completion:^ (BOOL finished) {
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 0.0f;
} completion:^ (BOOL finished) {
iv.image = [UIImage imageNamed:@"red"];
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 1.0f;
} completion:nil];
}];
}];
}];
}
您需要将其细分为四个部分。首先,您需要淡出原始图像,将其更改为第二个图像,淡化第二个图像,淡出第二个图像,更改回原始图像,然后淡入。