简体   繁体   English

点击时如何放大UIImageView

[英]How To Enlarge UIImageView When Tapped

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. 我有一个在UIImageView中显示图像的应用程序,我希望当用户点击它时图像放大。 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. 我认为最简单的方法是在UIImageView上隐藏一个半透明按钮,并在用户点击它时触发一个动作。 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];

}

My UIImageView is named imageView, and I attached the above method to the UIButton that is over imageView. 我的UIImageView被命名为imageView,我将上面的方法附加到了imageView上的UIButton。 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. 你可以看到一个透明按钮,它可以响应一个水龙头,并在下方有图像视图。

Another approach would be to simply do away with the UIImageView and simply have a UIButton. 另一种方法是简单地取消UIImageView并简单地使用UIButton。 You can set the button type in your xib/storyboard to be a custom button. 您可以将xib / storyboard中的按钮类型设置为自定义按钮。 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;

In your implementation you simply need to change the frame of the UIImageView or the UIButton depending on your approach. 在您的实现中,您只需根据您的方法更改UIImageView或UIButton的框架。

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

You will need to adjust the values in CGRectMake to fit to your needs. 您需要调整CGRectMake中的值以满足您的需求。 The first two arguments are the x and y coordinates of where the button/image appears in its parent view. 前两个参数是按钮/图像在其父视图中显示的位置的x和y坐标。 The second two arguments are the width and height of the button/image. 后两个参数是按钮/图像的宽度和高度。

Hope that helps. 希望有所帮助。

* EDIT Have you connected the UIImageView to an outlet in your header file? *编辑您是否已将UIImageView连接到头文件中的插座?

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)];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM