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");
}
IBAction
与tap gesture recognizer
。
答案 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;
imgView.userInteractionEnabled = YES;
,UITapGestureRecognizer
对我来说就是故事板。