iOS版:CGAffineTransformScale移动我的对象(iOS: CGAffineTra

2019-09-01 09:55发布

建立在一个问题我前面 。

简单的按钮尝试变换的标签。 我想它了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");
        }
    }];
}

Answer 1:

我是从你正在使用自动布局问题假设:在自动布局,如果你有一个领先的和/或顶部约束,你带刻度后CGAffineTransformMakeScale ,领先/顶部约束将重新应用,你的控制将继续前进你为了确保约束仍然满足。

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

  • 等到viewDidAppear (因为被应用在IB定义的约束,以及控制将被放置在那里,我们希望它和它的center物业将是可靠的);

  • 现在,我们有center有问题的控制,取代与领先和顶级的限制NSLayoutAttributeCenterXNSLayoutAttributeCenterY的限制,使用的值center属性设置constantNSLayoutConstraint为如下。

从而:

// 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;
}

您执行的细节可能会有所不同,这取决于你如何定义你想缩放控制的限制(在我的情况,领导和顶部是基于上海华盈,这使得它更容易),但希望它说明了解决方案,消除这些制约因素,并添加基于中心新的。

你可以,如果你不想通过寻找问题的约束迭代,就像我上面做,定义一个IBOutlet的顶部和领导,而不是限制,从而大大简化了过程。 此示例代码从一个项目中,出于各种原因,我无法使用被带到IBOutletNSLayoutConstraint引用。 但是,使用IBOutlet的约束引用肯定是去(如果你坚持使用自动配置)的简单方法。

例如,如果你去Interface Builder中,您可以突出问题和控制 -drag约束的助理编辑,让您的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对象会导致约束重新应用),视图将在您移动。 所以,你也许可以“瞒天过海”如果还原约束之前重新应用层的变换回身份,但它可能是最安全的关闭自动布局或刚修好的约束。



文章来源: iOS: CGAffineTransformScale moves my object