我從您使用自動佈局問題假設:在自動佈局,如果你有一個領先的和/或頂部約束,你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;
}
你實現的細節可能會有所不同,這取決於你如何定義要縮放(在我的情況,導致控制的約束和頂部均基於超級觀點,這使得它更容易),但希望它說明了解決方案,消除這些限制,並添加新的基於中心的。
如果您不想通過查找有問題的約束進行迭代(如上面所做的那樣),則可以定義IBOutlet
作爲頂層約束和主要約束,這大大簡化了過程。此示例代碼取自一個項目,出於各種原因,我無法使用IBOutlet
作爲NSLayoutConstraint
引用。但使用IBOutlet
引用的約束絕對是一個更簡單的方法去(如果你堅持自動佈局)。
例如,如果你去Interface Builder中,您可以突出顯示有問題的約束和控制 -drag的助理編輯,讓您的IBOutlet
:
如果你這樣做,而不是通過所有的約束迭代,你現在就可以說,例如:
if (self.imageViewVerticalConstraint)
{
[self.view removeConstraint:self.imageViewVerticalConstraint];
// create the new constraint here, like shown above
}
坦率地說,我希望界面生成器有能力ŧ o直接定義這些約束條件(即而不是一個「超級控制左邊的控制引導」約束,一個「超級控制左邊的控制中心」約束),但我認爲它不能在IB中完成,所以我正在以編程方式改變我的約束。但通過這個過程,我現在可以擴大控制範圍,並且不會因爲限制而在我身邊移動。
正如指出的爲0x7FFFFFFF,如果你申請一個CATransform3DMakeScale
到層,它不會自動應用的限制,所以你不會看到它移到如果你申請CGAffineTransformMakeScale
到視圖等。但是,如果您執行任何操作來重新應用約束條件(setNeedsLayout
或對任何UIView
對象所做的任何更改都可能導致約束被重新應用),則該視圖將隨您移動。因此,如果您在重新應用約束之前將圖層的變換恢復爲身份,則可能會「偷偷摸摸」,但關閉自動佈局或僅修復約束可能最安全。
來源
2013-04-30 01:40:53
Rob
是否有一個原因,你爲什麼使用核心動畫的旋轉和核心圖形的規模?我會嘗試在標籤的圖層上執行比例,看看是否有幫助。 – 2013-04-30 00:19:44
@ 0x7fffffff我剛剛測試過它:如果將'CATransform3DMakeScale'應用於圖層,它將不會自動應用這些約束,因此,如果將'CGAffineTransformMakeScale'應用於視圖,您將看不到它的移動。但是如果你做任何事情來重新應用約束('setNeedsLayout'或者對任何'UIView'對象做任何修改都會導致約束被重新應用),那麼視圖就會移動。因此,如果您在重新應用約束之前將圖層的變換恢復爲身份,則可能會「偷偷摸摸」,但關閉自動佈局或僅修復約束可能最安全。 – Rob 2013-04-30 03:03:38