点击时如何放大UIImageView [英] How To Enlarge UIImageView When Tapped

查看:93
本文介绍了点击时如何放大UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程很新,所以我在尝试这样做时遇到了一些问题。我有一个在UIImageView中显示图像的应用程序,我希望当用户点击它时图像放大。我认为最简单的方法是在UIImageView上隐藏一个半透明按钮,并在用户点击它时触发一个动作。这就是我能得到的,我不知道我的行动方法应该是什么样子。这就是我到目前为止:

I'm very new to programming, so I'm having some issues trying to do this. I have an app that displays an image in a UIImageView, and I would like for the image to enlarge when the user taps on it. I figured that the easiest way to do this would be to hide a translucent button over the UIImageView, and fire off an action when the user taps it. That is about as far as I can get, I have no idea what my action method should look like. This is what I have so far:

- (IBAction)enlargeImage1:(id)sender {
    CGRect myImageRect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 80.0f); 
    UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect]; 
    [myImage setImage:[UIImage imageNamed:@"gradient-253x80.png"]]; 
    [self.view addSubview:myImage];

}

我的UIImageView被命名为imageView,我附上了上面的方法到了over imageView的UIButton。当我点击图像时,它根本不会放大。非常感谢任何帮助或建议,谢谢!

My UIImageView is named imageView, and I attached the above method to the UIButton that is over imageView. When I click on the image, it does not enlarge at all. Any help or advice is much appreciated, thank you!

推荐答案

有两种方法可以解决这个问题。你可以看到一个透明按钮,它会响应一个水龙头,并在下方有图像视图。

There are two ways you could approach this. You could either have a transparent button as you see which responds to a tap and have the imageview underneath.

另一种方法是简单地取消UIImageView并简单地拥有一个UIButton。您可以将xib / storyboard中的按钮类型设置为自定义按钮。然后,您可以在界面构建器中设置按钮的图像或背景图像。您只需将按钮连接到某个操作即可。

Another approach would be to simply do away with the UIImageView and simply have a UIButton. You can set the button type in your xib/storyboard to be a custom button. You can then set the button's image or background image in the interface builder. All you need then is to connect your button to an action.

您的操作可能在您的标题文件中如下所示:

Your action could be something like follows in your header file:

- (IBAction)buttonTapped:(id)sender;

在您的实现中,您只需根据您的方法更改UIImageView或UIButton的框架。

In your implementation you simply need to change the frame of the UIImageView or the UIButton depending on your approach.

- (IBAction)buttonTapped:(id)sender {
    UIButton *button = (UIButton *)sender;
    [button setFrame:CGRectMake(0, 0, 100, 100)];
    // or [yourImageViewName setFrame:CGRectMake(0, 0, 100, 100)];
}

您需要调整CGRectMake中的值以满足您的需求。前两个参数是按钮/图像在其父视图中显示的位置的x和y坐标。后两个参数是按钮/图像的宽度和高度。

You will need to adjust the values in CGRectMake to fit to your needs. The first two arguments are the x and y coordinates of where the button/image appears in its parent view. The second two arguments are the width and height of the button/image.

希望有帮助。

* 编辑
您是否已将UIImageView连接到头文件中的插座?

* EDIT Have you connected the UIImageView to an outlet in your header file?

在您的方法中而不是创建另一个图像,您应该只是改变现有的大小。您可以按如下方式执行此操作:

in your method instead of creating another image, you should simply change the size of your existing one. You can do this as follows:

[self.imageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];

这篇关于点击时如何放大UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆