在 UIView 上旋转/移动对象
当方向改变时,我需要旋转和移动 UIView 上的对象。为此,我在 willRotateToInterfaceOrientation
中添加了以下内容,
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
[self repositionObjectAfterRotation:scrollView x:0 y:100 width:480 height:150];
[self repositionObjectAfterRotation:pageControl x:50 y:430 width:100 height:30];
}
我的 repositionObjectAfterRotation:x:y:width:height:
看起来像这样,它在设置新的界限。
- (void)repositionObjectAfterRotation:(UIView *)anObject x:(int)x y:(int)y
width:(int)width height:(int)height
{
[UIView animateWithDuration:kAnimationDuration animations:^{
CGRect boundsRect = anObject.bounds;
boundsRect.origin.x = x;
boundsRect.origin.y = y;
boundsRect.size.width = width;
boundsRect.size.height = height;
anObject.bounds = boundsRect;
} completion:^ (BOOL finished){
if (finished) {
[UIView animateWithDuration:kAnimationDuration animations:^{
anObject.alpha = 1.0;
}];
}
}];
}
当视图旋转回纵向时,它只显示滚动视图的一半,即使我的 NSLogs 声明框架和视图都显示为滚动视图。边界是正确的...
我是否正确旋转/重新定位对象,或者是否有更简单/首选的方式来支持景观?
谢谢
I need to rotate and move objects on a UIView when the orientation changes. To that end I have the following in willRotateToInterfaceOrientation
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
[self repositionObjectAfterRotation:scrollView x:0 y:100 width:480 height:150];
[self repositionObjectAfterRotation:pageControl x:50 y:430 width:100 height:30];
}
My repositionObjectAfterRotation:x:y:width:height:
looks like this, which takes the object and alters its bounds, before setting the new bounds.
- (void)repositionObjectAfterRotation:(UIView *)anObject x:(int)x y:(int)y
width:(int)width height:(int)height
{
[UIView animateWithDuration:kAnimationDuration animations:^{
CGRect boundsRect = anObject.bounds;
boundsRect.origin.x = x;
boundsRect.origin.y = y;
boundsRect.size.width = width;
boundsRect.size.height = height;
anObject.bounds = boundsRect;
} completion:^ (BOOL finished){
if (finished) {
[UIView animateWithDuration:kAnimationDuration animations:^{
anObject.alpha = 1.0;
}];
}
}];
}
When the view rotates back to portrait, it only displays half of the scrollview, even though my NSLogs state that the frame & bounds are correct...
Am I rotating / repositioning objects correctly or is there an easier/preferred way to support landscape?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您旋转到横向时,您明确将高度设置为“150”,但当您返回纵向时,不要将其调整回来。
我不知道你的自动调整大小是如何在 IB 中设置的,所以我无法说明这些对象在单独放置时将如何调整大小/移动。
You are explicitly setting the height to "150" when you rotate to Landscape, but don't adjust it back when you go back to portrait.
I don't know how your auto-sizing is set-up in IB, so I can't state how these objects will resize/move when left by themselves.
3点:
layoutSubviews
更方便。它在每次旋转后调用,并且[设备方向]
为您提供新的方向。框架
,而不是它们的边界
。内部值(即仅在该视图“内部”起作用)表示“将我置于 superivew 的这个位置”,这就是您想要的定义。例如,scrollView 用于其子视图的坐标是
在
layoutSubviews
中,只需更改框架,您想要的动画就会自动应用:3 points:
layoutSubviews
is more convenient. It's called after each rotation, and[device orientation]
gives you the new orientation.frame
of these views, not theirbounds
, which is an internal value (i.e. it matters only "inside" that view).frame
says 'put me in this position of the superivew', which is what you want.bound
defines the coordinate your, say, scrollView uses for its subviews. what you need here is the frame.In
layoutSubviews
, just change the frame, the animation you want will apply automatically: