简体   繁体   English

Autolayout破坏了UIView Animation

[英]Autolayout breaks UIView Animation

Struggling to make my app work on both the iPhone 5 and the smaller iPhone's prior. 努力使我的应用程序在iPhone 5和较小的iPhone之前工作。 Would be happy to require iOS6 and use Autolayout because that works great. 很高兴要求iOS6并使用Autolayout因为它很好用。

However, the app has 4 buttons that regularly swap positions, so I assign each button a different location from an array and then animate the movement with something like this: 但是,应用程序有4个按钮,可以定期交换位置,因此我为每个按钮分配一个与数组不同的位置,然后使用以下内容为动画设置动画:

[UIView animateWithDuration:0.5 animations:^{
    button1.center = [[locations objectAtIndex:0] CGPointValue];
    button2.center = [[locations objectAtIndex:1] CGPointValue];
    button3.center = [[locations objectAtIndex:2] CGPointValue];
    button4.center = [[locations objectAtIndex:3] CGPointValue];
}];

This doesn't work with Autolayout. 这不适用于Autolayout。 Nothing changes. 没有什么变化。 The best I've gotten is that it will animate to a position then immediately snap back. 我得到的最好的是它会动画到一个位置,然后立即回弹。

When I remove Autolayout however all the buttons are scrunched up on the smaller iPhones, and because I'm using points set by the iPhone 5 size they end up lower down, putting the bottom row off the screen. 当我删除Autolayout但是所有的按钮都在较小的iPhone上被碾碎,并且由于我使用的是iPhone 5尺寸设置的点,它们最终会降低,将底部行放在屏幕上。 I original had different CGPoint numbers that I got from Interface Builder, but they were "wrong", though I'm thinking they were "wrong" because they were the numbers for Autolayout to use. 我原来有来自Interface Builder的不同的CGPoint数字,但它们是“错误的”,虽然我认为它们是“错误的”,因为它们是Autolayout使用的数字。 The array just so you can see: 数组就这样你可以看到:

buttonLocations = [NSArray arrayWithObjects:
        [NSValue valueWithCGPoint:CGPointMake(57, 523)],
        [NSValue valueWithCGPoint:CGPointMake(109, 523)],
        [NSValue valueWithCGPoint:CGPointMake(57, 471)],
        [NSValue valueWithCGPoint:CGPointMake(109, 471)],
        nil];

What should I do to fix this problem? 我该怎么做才能解决这个问题? Setting different points for each sized device doesn't seem very efficient. 为每个尺寸的设备设置不同的点似乎不是很有效。

The solution is that you animate the items transform property, not its position. 解决方案是您为items属性设置动画,而不是它的位置。

The position is going to be set by autolayout and you don't want to fight it. 该位置将由autolayout设置,你不想打它。 The actual position on the screen, though, is going to be its "correct" (set by auto layout) position, offset by its transform property. 但是,屏幕上的实际位置将是其“正确”(由自动布局设置)位置,由其变换属性抵消。

So you can say button.transform = CGTransformMake(x,y) and it will appear x and y off from it's center position. 所以你可以说button.transform = CGTransformMake(x,y),它将从它的中心位置出现x和y。

There are a variety of CGWhatever macros to make these transforms easily, and you can just change them in an animation block to move the items around. 有很多CGWhatever宏可以轻松地进行这些变换,你可以在动画块中更改它们来移动项目。

One way would be to use the auto layout system, and animate the "constant" value of the constraint. 一种方法是使用自动布局系统,并为约束的“常量”值设置动画。 In the example code below, I have three buttons in a horizontal row, each with a leading edge constraint to the superview, which is what I animate. 在下面的示例代码中,我在水平行中有三个按钮,每个按钮都有超前视图的前沿约束,这就是我的动画。 side1, side2, and side3 are IBOutlets to those constraints, and button1, button2, and button3 are the outlets to the 3 buttons. side1,side2和side3是这些约束的IBOutlets,button1,button2和button3是3个按钮的出口。

-(void)animateButton {
    [self.view removeConstraint:self.side1];
    [self.view removeConstraint:self.side2];
    [self.view removeConstraint:self.side3];

    NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self.button1 attribute:NSLayoutAttributeLeading relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:114];
    NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self.button2 attribute:NSLayoutAttributeLeading relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:213];
    NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self.button3 attribute:NSLayoutAttributeLeading relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:20];

    [self.view addConstraints:@[con1,con2,con3]];
    [UIView animateWithDuration:2 animations:^{
        [self.view layoutIfNeeded];
    }];
}

You can fix this problem with very little changes in your code. 您可以通过代码中的少量更改来解决此问题。 Instead of swapping the button locations, swap their layout constrains. 而不是交换按钮位置,交换他们的布局约束。 You can use the cocoapod SBP to swap the layout constrains by importing this category 您可以使用cocoapod SBP通过导入此类别来交换布局约束

    #import "UIViewController+SBP.h"

Then using this method 然后使用这种方法

    - (void)switchViewConst:(UIView*)firstView secondView:(UIView*)secondView durationInSeconds:(double)durationInSeconds

Here is a video tutorial . 这是一个视频教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM