0

1)我有一个UIViewUIImageView我从图片库加载图片。我想知道如何将图片“居中”在屏幕上并使纵横比不变。例如,如果我加载一张处于横向模式的图片,我如何确保图片不会在整个屏幕上拉伸?我的应用程序在纵向模式下工作。

2)在同一个视图中,我想添加这样的效果,即当用户触摸屏幕时,按钮菜单会在他再次按下时淡出并重新出现。我不确定那叫什么,我无法让它tap gesture recognizer工作。

编辑:

我设置了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.
}

至于Tap gesture

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

IBAction链接到tap gesture recognizer.

4

2 回答 2

1

为了使 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;

要添加点击手势识别器,请执行以下操作

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(screenTapped)];
//By deafualt interaction is disabled
imgView.userInteractionEnabled = YES;
[imgView addGestureRecognizer:recognizer];
于 2012-06-12T20:14:10.853 回答
0

我的解决方案是

1) 添加imgView.contentMode = UIViewContentModeScaleAspectFit;

2) 就像 Omar Abdelhafith 说我添加的那样imgView.userInteractionEnabled = YES;UITapGestureRecognizer为我创建了故事板。

于 2012-06-13T09:03:30.180 回答