如何更改图像并重置?

时间:2014-08-22 07:21:25

标签: ios objective-c

我正在尝试在图片之间切换。 凝视图像为“Standing.png”,当您点击屏幕时,它会变为“Kicking.png”,然后在0.15秒后重置为“Standing.png”。

是否有UIImage代码或UIView?

4 个答案:

答案 0 :(得分:2)

试试这个

在你的班级中添加这样的方法

-(void)loadImageWithName:(NSString*)imageName
    {
       if(imageName)
          yourImageView.image = [UIImage imageNamed:imageName];
       else
          yourImageView.image = [UIImage imageNamed:@"Standing.png"];
    }

并在viewDidLoad方法中,将UITapGestureRecognizer添加到视图中,并使用第一个图像调用loadImageWithName方法

 - (void)viewDidLoad
 {
         UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeImage:)];
         tapGesture.delegate =self;
         [self.view addGestureRecignizer: tapGesture];

         [self loadImageWithName:@"Standing.png" ];
 }

并在方法changeImage:

-(void)changeImage:(UITapGestureRecognizer*)sender
{

  [self loadImageWithName:@"Kicking.png"];
   NSTimer * timer = [NSTimer timerWithTimeInterval:0.15 target:self selector:@selector(loadImageWithName:) userInfo:nil repeats:NO];
}

答案 1 :(得分:2)

Try this code

-(void)viewDidLoad
{
 UITapGestureRecognizer *objTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];

[imageview setImageWithName:@"Standing.png" ];
[imageview addGestureRecognizer:objTap];
[imageview setUserInteractionEnabled:YES]

}

-(void)oneTap :(id)Sender
{

[imageview setImageWithName:@"Kicking.png"];

 [NSTimer scheduledTimerWithTimeInterval:0.15f
                                                  target:self
                                                selector:@selector(timerM)
                                                userInfo:nil
                                                 repeats:NO];

}

-(void)timerM
{
[imageview setImageWithName:@"Standing.png" ];

}

答案 2 :(得分:1)

您只需使用

即可
[UIImageViewObj setImage:@"Standing.png"]; 

然后更改您可以使用点击手势检测到的屏幕并设置

[UIImageViewObj setImage:@"Kicking.png"];

然后在这里,计时器将启动,然后是时间选择器方法:

[UIImageViewObj setImage:@"Standing.png"];

尝试一次。

答案 3 :(得分:1)

-(void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *myButton = [[UIButton alloc] initWithFrame:self.view.frame];
    [myButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [myButton setImage:[UIImage imageNamed:@"standing.png"] forState:UIControlStateNormal];

    [self.view addSubview:myButton];
}

-(void)buttonTapped:(id)sender
{
    UIButton *myButton = (UIButton *)sender;

    [myButton setImage:[UIImage imageNamed:@"kicking.png"] forState:UIControlStateNormal];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [myButton setImage:[UIImage imageNamed:@"standing.png"] forState:UIControlStateNormal];
    });
}