首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >旋转时放置和移动视图(UIView)

旋转时放置和移动视图(UIView)
EN

Stack Overflow用户
提问于 2011-05-24 09:50:45
回答 4查看 4.2K关注 0票数 16

当应用程序旋转时,准确地放置和移动视图的“正确”方式是什么?也就是说,当UI从纵向旋转到横向(反之亦然)时,我如何对视图的位置、大小和回流进行细粒度控制?我认为我的两个选择是:

  1. 使用两个超级视图(纵向和横向)。在旋转时:在它们之间切换。
  2. 使用一个超级视图。旋转时:更改每个子视图的边框、边界和中心属性。

如果您有两个具有足够不同布局和元素的视图,那么第一种方法可能就足够好了。如果您的两个视图在不同方向上的大小基本相同,那么第二种方法可能是只使用一个视图的更好方法。

我怀疑前者可以用IB来完成,而后者应该通过编程来完成。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-05-24 14:03:51

要在iPhone旋转时对子视图的位置和大小进行细粒度控制,请在UIViewController方法willAnimateRotationToInterfaceOrientation:duration:中更改子视图的框架。此方法在动画块中调用,因此您在动画块中对子视图帧所做的所有更改都是动画的。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        // Portrait frames
        self.subviewA.frame = CGRectMake(x, y, width, height);
        self.subviewB.frame = CGRectMake(x, y, width, height);
        self.subviewC.frame = CGRectMake(x, y, width, height);
    } else {
        // Landscape frames
        self.subviewA.frame = CGRectMake(x, y, width, height);
        self.subviewB.frame = CGRectMake(x, y, width, height);
        self.subviewC.frame = CGRectMake(x, y, width, height);
    }
}
票数 10
EN

Stack Overflow用户

发布于 2011-05-24 10:50:00

这里有一个想法。我担心动画效果会很滑稽,不过你可以试试。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
                                duration:(NSTimeInterval)duration {

    if (toOrientation == UIInterfaceOrientationLandscapeLeft ||
        toOrientation == UIInterfaceOrientationLandscapeRight) {
        [UIView beginAnimations:nil context:NULL]; {
            [UIView setAnimationDuration:1.0];
        }
            [UIView setAnimationDelegate:self];
        [viewA setFrame:CGRectMake(?,?,?,?)];
        [viewB setFrame:CGRectMake(?,?,?,?)];
        [viewC setFrame:CGRectMake(?,?,?,?)];
        } [UIView commitAnimations];    
    } else if  (toOrientation == UIInterfaceOrientationPortrait ||
            toOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        [UIView beginAnimations:nil context:NULL]; {
            [UIView setAnimationDuration:1.0];
        }
        [UIView setAnimationDelegate:self];
        [viewA setFrame:CGRectMake(?,?,?,?)];
        [viewB setFrame:CGRectMake(?,?,?,?)];
        [viewC setFrame:CGRectMake(?,?,?,?)];
        } [UIView commitAnimations];    
    }
票数 0
EN

Stack Overflow用户

发布于 2011-05-25 16:57:09

我有一个想法,这对我来说是正确的,我在一个视图上使用一个小视图,当设备的方向改变时,它会自动旋转。使用一个通知程序,然后在方法中更改方向。

#define DegreesToRadians(x) (M_PI * x / 180.0)

.

[[NSNotificationCenter defaultCenter] addObserver:showIndicator
                                         selector:@selector(setProperOreintation)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

.

-(void)setProperOrientation {

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];


if (orientation == UIDeviceOrientationPortraitUpsideDown)
    self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, DegreesToRadians(180)); 

else if (orientation == UIDeviceOrientationLandscapeLeft)
    self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, DegreesToRadians(90));  

else if (orientation == UIDeviceOrientationLandscapeRight)
    self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, DegreesToRadians(-90));

    [UIView commitAnimations]; }

这里不是旋转,而是平移或缩放

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6104963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档