【问题标题】:iOS: Changing a UIImageView by pressing an UIButtoniOS:通过按下 UIButton 更改 UIImageView
【发布时间】:2014-10-16 22:34:21
【问题描述】:

我正在制作一个非常简单的游戏。我有一个 UIButton 和一个 UIImageView。我正在尝试做的是当用户按下按钮时,它将图像更改为另一个图像然后返回原始图像,以模拟动画(字符移动)

我有这个代码:

文件.m

{

IBOutlet UIImageView *image1;
IBOutlet UIButton *button;

}

-(IBAction)button:(id)sender;

文件.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 属性吗?

谢谢!

【问题讨论】:

  • 你不想让它溶解,但你想要动画是什么意思?您设置动画的唯一属性是 alpha,它会产生淡入淡出效果
  • 您正在为 alpha 的变化设置动画,当然它会显示溶解过渡。
  • 我希望图像在没有淡入淡出效果的情况下做动画。我删除了 alpha 属性,但现在动画没有显示它应该显示的方式
  • 那么,您希望图像交换而不褪色吗?
  • 我修好了,谢谢你的帮助!我不得不删除 alpha 属性并进行一些更改。

标签: ios objective-c iphone uiimageview uibutton


【解决方案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];
            }];
        }];

    }];
}

你需要把它分成四个部分。首先,您需要将原始图像淡出,将其更改为第二个图像,将第二个图像淡入,将第二个图像淡出,更改回原始图像,然后将其淡入。

【讨论】:

    猜你喜欢
    • 2014-10-16
    • 2013-04-24
    • 2014-10-10
    • 2020-04-07
    • 2012-02-16
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多