【问题标题】:ios: Moving images based on orientationios:基于方向的移动图像
【发布时间】:2013-03-20 20:15:27
【问题描述】:

当我的应用处于横向与纵向模式时,我试图在不同位置显示一组图像。我已经搜索并找到了对willAnimateRotationToInterfaceOrientation:duration 方法的引用,我在其中添加了如下代码:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
        toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.getStartedImage1.frame = CGRectMake(5, 100, 155, 115);
        self.getStartedImage2.frame = CGRectMake(160, 100, 155, 115);
        self.getStartedImage3.frame = CGRectMake(315, 100, 155, 115);
    }
    else
    {
        self.getStartedImage1.frame = CGRectMake(238, 96, 155, 115);
        self.getStartedImage2.frame = CGRectMake(238, 219, 155, 115);
        self.getStartedImage3.frame = CGRectMake(238, 342, 155, 115);
    }
}

这很有效,直到我意识到如果您在已经处于横向模式时进入视图,该方法将不会运行。这是有道理的,因为我在不旋转任何东西的情况下打开视图。但是,这对我的困境没有帮助。所以我继续搜索并找到了方法viewWillLayoutSubviews。我在这个方法中添加了相同的主体,它被调用了,但可能为时已晚,因为对图像帧的更改没有出现在模拟器中。如果我尝试将代码移动到viewWillAppear,我也会遇到同样的问题。我见过一些涉及多个视图的复杂解决方案,我宁愿避免。有什么建议吗?

【问题讨论】:

    标签: xcode ios6 landscape


    【解决方案1】:

    听起来你在和autolayout 打架。我能想到三种解决方案。

    1. 关闭autolayout。如果您已经自己管理所有视图框架,那么这可能是正确的路径。

    2. 将您的布局代码移至-viewDidLayoutSubviews。这是hacky但很快。它还允许autolayout 做它大部分的事情。

    3. 换出适用的旋转约束。这将是最多的工作,但从长远来看,对于国际化等事情来说,可以说是最灵活的。

    关于第三点:

    约束 (NSLayoutConstraints) 是可以在 IB 和代码中检查和编辑的真实对象。由于布局可能非常复杂,这里有一个非常简单的示例。假设我的视图控制器视图中有两个按钮,一个在另一个之上(连接到两个插座buttonOnebuttonTwo),并且我想在横向方向反转它们的位置。像这样的代码(虽然不建议采用这种形式)确实有效,并提供了如何交换约束的示例。

    @implementation MyAutoViewController {
        NSArray *_portraitConstraints;
        NSArray *_landscapeConstraints;
    }
    -(void)viewDidLoad{
        [super viewDidLoad];
    
        // Clear constraints from Nib. 
        // PLEASE DO NOT DO THIS PART IN REAL LIFE
        NSArray *current = self.view.constraints;
        for (NSLayoutConstraint *contsra in current) {
            [self.view removeConstraint:contsra];
        }
    
        // Set up views dictionary
        UIButton *buttonOne = self.buttonOne;
        UIButton *buttonTwo = self.buttonTwo;
        NSDictionary *viewsDict = NSDictionaryOfVariableBindings(buttonOne, buttonTwo);
    
        // Set distance from left to both buttons to standard space
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[buttonOne]" options:0 metrics:nil views:viewsDict]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[buttonTwo]" options:0 metrics:nil views:viewsDict]];
    
        // HERE IS WHERE IT GETS INTERESTING
        // ### Create portrait constraints ###
        _portraitConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[buttonOne]-[buttonTwo]" options:0 metrics:nil views:viewsDict];
    
        // ### Create landscape constraints ###
        _landscapeConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[buttonTwo]-[buttonOne]" options:0 metrics:nil views:viewsDict];
    
        // Add the appropriate constraints
        [self.view addConstraints:(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))?_portraitConstraints:_landscapeConstraints];
    
    }
    -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
        if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
            [self.view removeConstraints:_landscapeConstraints];
            [self.view addConstraints:_portraitConstraints];
        } else {
            [self.view removeConstraints:_portraitConstraints];
            [self.view addConstraints:_landscapeConstraints];
        }
    }
    

    我真的建议观看来自 WWDC2012 的 autolayout 上的三个视频。 Autolayout 有很多值得关注的地方。

    【讨论】:

    • 谢谢。我现在选择#2。我不能关闭自动布局,因为它对我的其他视图很重要,而且我不确定我是否完全理解 #3。您的意思是只删除相关图像的约束吗?
    • @CherylYaeger 我添加了有关选项 3 的更多信息。
    • 感谢您添加此额外信息!
    猜你喜欢
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多