iOS:CGAffineTransformScale移动我的对象

时间:2013-04-29 23:29:05

标签: ios cocoa-touch core-graphics

基于我有earlier的问题。

尝试转换标签的简单按钮。我希望它缩小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");
        }
    }];
}

1 个答案:

答案 0 :(得分:18)

我假设您正在使用自动布局:在自动布局中,如果您有一个前导和/或上限,在使用CGAffineTransformMakeScale缩放后,前导/上限约束将是重新应用,您的控制权将继续向您移动,以确保仍然满足约束条件。

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

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

    < / LI>
  • 现在我们有问题控件的center,使用NSLayoutAttributeCenterX的值替换NSLayoutAttributeCenterYcenter约束的前导约束和上限约束}属性为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;
}

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

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

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

make constraint IBOutlet

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

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

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

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


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