界面视图.如何将缩放限制为仅一维 [英] UIView. How do I constrain scaling to one dimension only

查看:18
本文介绍了界面视图.如何将缩放限制为仅一维的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 UIView 实例的 UIScrollview 实例.UIView 只是 UIImageView 实例的水平数组的容器.- 缩放由 UIScrollView 和 UIScrollViewDelegate 提供.我想限制缩放仅沿水平轴发生,根本没有垂直缩放.我该怎么做?

I have an instance of UIScrollview containing an instance of UIView. The UIView is just a container for a horizonal array of UIImageView instances. - Zooming is provided by UIScrollView and UIScrollViewDelegate. I would like to constrain zooming to occur only along the horizontal axis with no vertical scalling at all. How to I do this?

有没有办法,例如,子类化 UIView 并覆盖适当的方法来防止垂直缩放?我喜欢这种方法,但我不清楚要覆盖哪个方法以及该覆盖的方法实际上应该做什么.

Is there a way, for example, to subclass UIView and override the appropriate method to prevent vertical scaling? I like this approach but I am unclear on which method to override and what that overridden method should actually do.

干杯,道格

推荐答案

类似于我在 这个答案,您可以创建一个 UIView 子类并覆盖 -setTransform: 访问器方法来调整 UIScrollView 将尝试应用于您的 UIView 的转换.将此 UIView 设置为托管您的内容子视图并使其成为 UIScrollView 的子视图.

Similar to what I describe in this answer, you can create a UIView subclass and override the -setTransform: accessor method to adjust the transform that the UIScrollView will try to apply to your UIView. Set this UIView to host your content subviews and make it the subview of the UIScrollView.

在覆盖的 -setTransform: 中,您需要接受 UIScrollView 想要应用的变换并对其进行调整,以便缩放仅在一个方向上生效.来自 关于如何构造 CGAffineTransform 矩阵的文档,我相信以下实现应将您的缩放限制为仅沿水平方向:

Within your overridden -setTransform:, you'll need to take in the transform that the UIScrollView would like to apply and adjust it so that the scaling only takes effect in one direction. From the documentation on how CGAffineTransform matrices are constructed, I believe the following implementation should constrain your scaling to be just along the horizontal direction:

- (void)setTransform:(CGAffineTransform)newValue;
{
 CGAffineTransform constrainedTransform = CGAffineTransformIdentity;
 constrainedTransform.a = newValue.a;
 [super setTransform:constrainedTransform];
}

这篇关于界面视图.如何将缩放限制为仅一维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆