顶部的UIImageView和设置大小

时间:2012-06-12 18:48:00

标签: ios xcode cocoa-touch uiimageview uigesturerecognizer

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

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");
}

IBActiontap gesture recognizer

相关联

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

我的解决方案是

1)添加imgView.contentMode = UIViewContentModeScaleAspectFit;

2)像Omar Abdelhafith说我添加了imgView.userInteractionEnabled = YES;UITapGestureRecognizer对我来说就是故事板。