iPhone 旋转后 UIScrollView 中的 contentOffset
我在 UIScrollView
中有一个 UIImageView
,并且在使用 contentOffset
属性时遇到了一些问题。根据 Apple 的参考,其定义为:
contentOffset:内容视图的原点相对于滚动视图的原点的偏移点。
例如,如果图像位于屏幕的左上角,如下所示,则 contentOffset 将为 (0,0):
_________
|IMG |
|IMG |
| |
| |
| |
| |
---------
对于设备旋转,我有以下设置:
scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
imageView.contentMode = UIViewContentModeCenter;
scrollView.contentMode = UIViewContentModeCenter;
这将使所有内容围绕屏幕中心旋转。 旋转屏幕后,屏幕将如下所示:
______________
| IMG |
| IMG |
| |
--------------
我的问题是,如果我现在读取 contentOffset
,它仍然是 (0,0)。 (如果我在横向模式下移动 UIImage,contentOffset
的值会更新,但它是根据错误的原点计算的。)
有没有办法计算 UIImage 的坐标屏幕的左上角。 contentOffset
似乎仅在屏幕处于视图的初始方向时返回此值。
我尝试读取 self.view.transform 和 scrollView.transform ,但它们始终是身份。
I have an UIImageView
in an UIScrollView
and I have some trouble with the contentOffset
property. From Apple's reference, this is defined as:
contentOffset: The point at which the origin of the content view is offset from the origin of the scroll view.
For example, if the image is at the upper left corner of the screen as below then the contentOffset would (0,0):
_________
|IMG |
|IMG |
| |
| |
| |
| |
---------
For device rotation I have the following setting:
scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
imageView.contentMode = UIViewContentModeCenter;
scrollView.contentMode = UIViewContentModeCenter;
This will make everything rotate around the center of the screen.
After rotating the screen, then screen will then look like this:
______________
| IMG |
| IMG |
| |
--------------
My problem is that if I now read of contentOffset
, it is still (0,0). (If I move the UIImage in landscape mode, the value of contentOffset
is updated, but it is computed with respect to the wrong origin.)
Is there a way to compute the coordinates for the UIImage with respect to the upper left corner of the screen. contentOffset
only seems to return this value when the screen is in the initial orientation of the view.
I have tried to read self.view.transform
and scrollView.transform
, but they are always the identity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种方法:对于滚动视图集,
即使旋转行为不正确,
UIViewContentModeTopRight
模式也会将左上角保持在坐标 (0,0) 处。要获得与 UIViewContentModeCenter 中相同的旋转行为,请添加到
willAnimateRotationToInterfaceOrientation
中。fix
是函数上面的代码将使滚动视图围绕屏幕中心旋转,并确保左上角坐标始终为(0,0)坐标。
Here is one way to do this: For the scrollview set
The
UIViewContentModeTopRight
mode will keep the upper left corner at coordinate (0,0) even if the rotation behaviour is not correct. To get the same rotation behaviour as in UIViewContentModeCenter addinto
willAnimateRotationToInterfaceOrientation
.fix
is the functionThe above code will make the scrollview rotate around the center of the screen and also ensure that the upper left corder is always are coordinates (0,0).