13

基于我之前提出的一个问题。

尝试转换标签的简单按钮。我希望它缩小 0.5,这是可行的,但由于某种原因,它也会像它那样移动对象。标签向上和向左跳跃,然后变换。

- (IBAction)btnTest:(id)sender
{

    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        lblTest.transform = CGAffineTransformScale(lblTest.transform, 0.5f,0.5f);
    }completion:^(BOOL finished) {
        if(finished){
            NSLog(@"DONE");
        }
    }];
}
4

1 回答 1

18

我从您正在使用自动布局的问题中推测:在自动布局中,如果您有一个前导和/或顶部约束,在您使用 缩放后CGAffineTransformMakeScale,将重新应用前导/顶部约束并且您的控件将移动到您身上以确保仍然满足约束。

您可以关闭自动布局(这是简单的答案),也可以:

  • 等到viewDidAppear(因为应用了 IB 中定义的约束,并且控件将放置在我们想要的位置,并且其center属性将是可靠的);

  • 现在我们有了所center讨论的控件,将前导和顶部约束替换为NSLayoutAttributeCenterXNSLayoutAttributeCenterY约束,使用center属性的值设置constant如下NSLayoutConstraint

因此:

// don't try to do this in `viewDidLoad`; do it in `viewDidAppear`, where the constraints
// have already been set

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self replaceLeadingAndTopWithCenterConstraints:self.imageView];
}

// Because our gesture recognizer scales the UIView, it's quite important to make
// sure that we don't have the customary top and leading constraints, but rather
// have constraints to the center of the view. Thus, this looks for leading constraint
// and if found, removes it, replacing it with a centerX constraint. Likewise if it
// finds a top constraint, it replaces it with a centerY constraint.
//
// Having done that, we can now do `CGAffineTransformMakeScale`, and it will keep the
// view centered when that happens, avoiding weird UX if we don't go through this
// process.

- (void)replaceLeadingAndTopWithCenterConstraints:(UIView *)subview
{
    CGPoint center = subview.center;

    NSLayoutConstraint *leadingConstraint = [self findConstraintOnItem:subview
                                                             attribute:NSLayoutAttributeLeading];
    if (leadingConstraint)
    {
        NSLog(@"Found leading constraint");

        [subview.superview removeConstraint:leadingConstraint];

        [subview.superview addConstraint:[NSLayoutConstraint constraintWithItem:subview
                                                                      attribute:NSLayoutAttributeCenterX
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:subview.superview
                                                                      attribute:NSLayoutAttributeTop
                                                                     multiplier:1.0
                                                                       constant:center.x]];
    }

    NSLayoutConstraint *topConstraint = [self findConstraintOnItem:subview
                                                         attribute:NSLayoutAttributeTop];

    if (topConstraint)
    {
        NSLog(@"Found top constraint");

        [subview.superview removeConstraint:topConstraint];

        [subview.superview addConstraint:[NSLayoutConstraint constraintWithItem:subview
                                                                      attribute:NSLayoutAttributeCenterY
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:subview.superview
                                                                      attribute:NSLayoutAttributeLeft
                                                                     multiplier:1.0
                                                                       constant:center.y]];
    }
}

- (NSLayoutConstraint *)findConstraintOnItem:(UIView *)item attribute:(NSLayoutAttribute)attribute
{
    // since we're looking for the item's constraints to the superview, let's
    // iterate through the superview's constraints

    for (NSLayoutConstraint *constraint in item.superview.constraints)
    {
        // I believe that the constraints to a superview generally have the
        // `firstItem` equal to the subview, so we'll try that first.

        if (constraint.firstItem == item && constraint.firstAttribute == attribute)
            return constraint;

        // While it always appears that the constraint to a superview uses the
        // subview as the `firstItem`, theoretically it's possible that the two
        // could be flipped around, so I'll check for that, too:

        if (constraint.secondItem == item && constraint.secondAttribute == attribute)
            return constraint;
    }

    return nil;
}

您的实现细节可能会有所不同,具体取决于您如何定义要扩展的控件的约束(在我的情况下,leading 和 top 基于 superview,这使得它更容易),但希望它说明了解决方案,删除这些约束并基于中心添加新约束。

如果您不想像我在上面所做的那样遍历寻找有问题的约束,则可IBOutlet以为顶部和前导约束定义一个,这大大简化了过程。此示例代码取自一个项目,由于各种原因,我无法将IBOutlet用于NSLayoutConstraint引用。但是使用IBOutlet约束的引用绝对是一种更简单的方法(如果你坚持使用自动布局)。

例如,如果您转到 Interface Builder,您可以突出显示有问题的约束并control- 拖动到助手编辑器以使您的IBOutlet

制作约束 IBOutlet

如果你这样做,而不是遍历所有约束,你现在可以说,例如:

if (self.imageViewVerticalConstraint)
{
    [self.view removeConstraint:self.imageViewVerticalConstraint];

    // create the new constraint here, like shown above
}

坦率地说,我希望 Interface Builder 能够开箱即用地定义这些约束(即,而不是“控制引导到父视图左侧”约束,“控制中心到父视图左侧”约束),但我不要认为它可以在 IB 中完成,所以我正在以编程方式更改我的约束。但是通过这个过程,我现在可以缩放控件并且不会因为限制而让它在我身上移动。


正如 0x7fffffff 所指出的,如果您将 a 应用CATransform3DMakeScale到图层,它不会自动应用约束,因此您不会看到它像应用CGAffineTransformMakeScale到视图时那样移动。但是,如果您执行任何操作来重新应用约束(setNeedsLayout或对任何UIView对象进行任何更改都可能导致重新应用约束),则视图将在您身上移动。因此,如果您在重新应用约束之前将图层的转换恢复为身份,您可能能够“潜入”,但关闭自动布局或仅修复约束可能是最安全的。

于 2013-04-30T01:40:53.837 回答