gpt4 book ai didi

iOS:CGAffineTransform Scale 移动我的对象

转载 作者:技术小花猫 更新时间:2023-10-29 10:23:36 24 4
gpt4 key购买 nike

基于我的问题 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");
}
}];
}

最佳答案

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

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

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

  • 现在我们有了相关控件的 center,将 leading 和 top 约束替换为 NSLayoutAttributeCenterXNSLayoutAttributeCenterY 约束,使用 center 属性的值来设置 NSLayoutConstraintconstant,如下所示。

因此:

// 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:

make constraint IBOutlet

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

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

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

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


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

关于iOS:CGAffineTransform Scale 移动我的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16289895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com