简体   繁体   English

UIImageView在顶部并设置大小

[英]UIImageView on top and set size

1) I have a UIView with a UIImageView on it, i load pictures from the image gallery. 1)我有一个带有UIImageViewUIView ,我从图库中加载图片。 I want to know how I would "center" the picture on the screen and make that the aspect ratio isn't changed. 我想知道如何将图片“居中”在屏幕上,并使纵横比保持不变。 For instance if I load a picture that is on landscape mode, how do I make sure that the picture isn't stretched all over the screen? 例如,如果我加载的是横向模式下的图片,如何确保图片没有在整个屏幕上拉伸? My app is working in portrait mode. 我的应用程序在纵向模式下工作。

2) In that same view I want to add the effect that when the user touches the screen the botton menu fades out and back in when he presses again. 2)在同一视图中,我想添加以下效果:当用户触摸屏幕时,botton菜单会逐渐淡出,并在再次按下时返回。 I'm not sure what that is called and I couldn't get the tap gesture recognizer to work. 我不确定这叫什么,也无法使tap gesture recognizer器正常工作。

EDIT: 编辑:

I set the UIImageView on page load 我在页面加载时设置了UIImageView

- (void)viewDidLoad
{
[super viewDidLoad];
xlabel = [[UILabel alloc] initWithFrame:self.setLablePosition];
xlabel.backgroundColor = [UIColor clearColor];

imgView.image = self.appDelegate.theImg; // now the image is put on a UIImageView that is "full screen"
xlabel.text = @"Some text";
[imgView addSubview:xlabel];
// Do any additional setup after loading the view.
}

and as for Tap gesture : 至于Tap gesture

-(IBAction)screenTapped{
NSLog(@"screen Tapped");
}

the IBAction is linked to the tap gesture recognizer . IBAction链接到tap gesture recognizer IBAction tap gesture recognizer

For centering the UIImageView in the view you would do the following 为了使UIImageView在视图中居中,您可以执行以下操作

//First give imgView the correct size
//You may need to divide the size by some constant if the image is too big
imgView.frame = CGRectMake(0, 0, self.appDelegate.theImg.size.width, self.appDelegate.theImg.size.width);
//Then center it
imgView.center = self.view.center;

For adding the tap gesture recognizer do the following 要添加点击手势识别器,请执行以下操作

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(screenTapped)];
//By deafualt interaction is disabled
imgView.userInteractionEnabled = YES;
[imgView addGestureRecognizer:recognizer];

My solution was 我的解决方案是

1) add imgView.contentMode = UIViewContentModeScaleAspectFit; 1)添加imgView.contentMode = UIViewContentModeScaleAspectFit;

2) like Omar Abdelhafith said i added imgView.userInteractionEnabled = YES; 2)就像Omar Abdelhafith所说,我添加了imgView.userInteractionEnabled = YES; , the UITapGestureRecognizer for me was created it the storyboard. ,为我创建的UITapGestureRecognizer是情节UITapGestureRecognizer

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

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