更改并在ios

时间:2016-02-22 10:53:44

标签: objective-c uiimageview uiimage

我正在编写一个应用程序,我以编程方式显示背景图像,然后在顶部更新一些文本。

在某些事件(例如点按)上,我想更改文字和背景图片。

我能够成功更改文字,但图像不会改变。

我使用以下代码设置图片,其中viewimages1images2为:

UIImageView* view;
UIImage* images1;
UIImage* images2;

view = [[UIImageView alloc] initWithImage: image1];
view.frame = CGRectMake(0, 0, screenWidth, screenHeight);
[view setUserInteractionEnabled:YES];
[self.view addSubview:view];
[self.view setContentMode:UIViewContentModeScaleToFill];
[self.view sendSubviewToBack:view];

然后我尝试用:

更改背景
view.image = image2;

但图像没有变化,有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用此代码更改点击手势上的图像

首先,你必须像这样在头文件中定义UIImageView。

@interface ChangeImageViewController ()
UIImageView *Imgview;
@end
@implementation ChangeImageViewController


Imgview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
Imgview.image=[UIImage imageNamed:@"firstImageName.png"];
[Imgview setUserInteractionEnabled:YES];
[self.view addSubview:Imgview];
[self.view sendSubviewToBack:Imgview];

    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TaponImgview:)];
tapGesture.numberOfTapsRequired=1;
[Imgview addGestureRecognizer:tapGesture];

并使用Tap手势识别器方法。

-(void)TaponImgview:(UITapGestureRecognizer *)recognizer
{
   Imgview.image=[UIImage imageNamed:@"secondImage.png"]; // Your second image that you want to change it

}

所以最后你的图像会在UIImageview上改变。谢谢