Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在UIView上旋转/移动对象_Iphone_Objective C_Uiview_Ios4_Rotation - Fatal编程技术网

Iphone 在UIView上旋转/移动对象

Iphone 在UIView上旋转/移动对象,iphone,objective-c,uiview,ios4,rotation,Iphone,Objective C,Uiview,Ios4,Rotation,当方向改变时,我需要在UIView上旋转和移动对象。为此,我在willRotateToInterfaceOrientation中有以下内容 我的重新定位ObjectAfterRotation:x:y:width:height:看起来像这样,它在设置新边界之前获取对象并更改其边界 - (void)repositionObjectAfterRotation:(UIView *)anObject x:(int)x y:(int)y

当方向改变时,我需要在UIView上旋转和移动对象。为此,我在willRotateToInterfaceOrientation中有以下内容

我的重新定位ObjectAfterRotation: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声明帧和边界是正确的

我是否正确旋转/重新定位对象,或者是否有更简单/首选的方式来支持横向


感谢您在旋转到横向时将高度明确设置为150,但在返回到纵向时不要将其重新调整

我不知道IB中的自动调整大小是如何设置的,因此我无法说明这些对象在自己离开时将如何调整大小/移动。

3点:

你们不需要显式地开始一个动画,除非你们的动画和旋转不同,否则系统无论如何都会这样做。 我认为覆盖布局子视图更方便。每次旋转后都会调用它,[device orientation]会为您提供新的方向。 您可能希望更改这些视图的框架,而不是它们的边界,这是一个内部值,即它只在该视图内起作用。 frame说“把我放在上级的位置”,这就是你们想要的。绑定定义了滚动视图用于其子视图的坐标。你需要的是这个框架

在LayoutSubView中,只需更改帧,所需动画将自动应用:

- (void)layoutSubviews {
    if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
        anObject.frame = landscapeFrame;
    }
    else {
        anObject.frame = portraitFrame;
    }
}

这个框架和superview的坐标有关,不是吗?覆盖layoutSubviews是我以前从未做过的事情。我是否只是覆盖布局子视图并将移动代码放在那里?没什么特别的吗?我想设置动画,因为我的项目不只是旋转,即我有一个滚动视图,旋转时需要从0,80到0100更多。视图旋转时不调用layoutSubviews。。。我试着打电话给[self.view setNeedsLayout];在WillRotateToInterfaceOrientation中,但它仍然没有进入我的方法嗯。。。我每天都在用这个方法。。。在最坏的情况下,调用setNeedsLayout时应该调用LayoutSubview。我假设您已经检查了layoutSubviews是否在视图中正确实现?很抱歉,它确实在纵向、复制和粘贴错误中向后移动。我的错。在IB中,选择所有支柱和弹簧。
- (void)layoutSubviews {
    if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
        anObject.frame = landscapeFrame;
    }
    else {
        anObject.frame = portraitFrame;
    }
}