我从您正在使用自动布局的问题中推测:在自动布局中,如果您有一个前导和/或顶部约束,在您使用 缩放后CGAffineTransformMakeScale
,将重新应用前导/顶部约束并且您的控件将移动到您身上以确保仍然满足约束。
您可以关闭自动布局(这是简单的答案),也可以:
因此:
// 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
:
如果你这样做,而不是遍历所有约束,你现在可以说,例如:
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
对象进行任何更改都可能导致重新应用约束),则视图将在您身上移动。因此,如果您在重新应用约束之前将图层的转换恢复为身份,您可能能够“潜入”,但关闭自动布局或仅修复约束可能是最安全的。