ios - UIImageView 位于顶部并设置大小

标签 ios xcode cocoa-touch uiimageview uigesturerecognizer

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

2)在同一个 View 中,我想添加这样的效果:当用户触摸屏幕时,按钮菜单会淡出,并在他再次按下时重新显示。我不确定它叫什么,而且我无法让点击手势识别器工作。

编辑:

我在页面加载时设置了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.
}

至于点击手势:

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

IBAction 链接到点击手势识别器

最佳答案

为了使 UIImageView 在 View 中居中,您需要执行以下操作

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

关于ios - UIImageView 位于顶部并设置大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11002897/

相关文章:

ios - 带有 UINavigationController 的独立 UINavigationBar 中的后退按钮

ios - 段控制位置改变

swift - 查找 UIColor 的色调、饱和度、亮度和 alpha

objective-c - Xcode 应用卡在横向模式

ios - 如何在标签中写多行

iphone - 在 iPhone 中缝合图片

ios - 在 View iOS8 之间切换时的动画/过渡

ios - 如何在swift中实现优化的多个计时器?

ios - 对于键 setDefaultLabelText,此类不符合键值编码。

ios - iOS 应用程序提交的分发验证失败