2013-04-29 72 views
13

建立在我有earlier的問題上。iOS:CGAffineTransformScale移動我的對象

試圖轉換標籤的簡單按鈕。我希望它縮小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

是否有一個原因,你爲什麼使用核心動畫的旋轉和核心圖形的規模?我會嘗試在標籤的圖層上執行比例,看看是否有幫助。 – 2013-04-30 00:19:44

+0

@ 0x7fffffff我剛剛測試過它:如果將'CATransform3DMakeScale'應用於圖層,它將不會自動應用這些約束,因此,如果將'CGAffineTransformMakeScale'應用於視圖,您將看不到它的移動。但是如果你做任何事情來重新應用約束('setNeedsLayout'或者對任何'UIView'對象做任何修改都會導致約束被重新應用),那麼視圖就會移動。因此,如果您在重新應用約束之前將圖層的變換恢復爲身份,則可能會「偷偷摸摸」,但關閉自動佈局或僅修復約束可能最安全。 – Rob 2013-04-30 03:03:38

回答

18

我從您使用自動佈局問題假設:在自動佈局,如果你有一個領先的和/或頂部約束,你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作爲頂層約束和主要約束,這大大簡化了過程。此示例代碼取自一個項目,出於各種原因,我無法使用IBOutlet作爲NSLayoutConstraint引用。但使用IBOutlet引用的約束絕對是一個更簡單的方法去(如果你堅持自動佈局)。

例如,如果你去Interface Builder中,您可以突出顯示有問題的約束和控制 -drag的助理編輯,讓您的IBOutlet

make constraint IBOutlet

如果你這樣做,而不是通過所有的約束迭代,你現在就可以說,例如:

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

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

坦率地說,我希望界面生成器有能力ŧ o直接定義這些約束條件(即而不是一個「超級控制左邊的控制引導」約束,一個「超級控制左邊的控制中心」約束),但我認爲它不能在IB中完成,所以我正在以編程方式改變我的約束。但通過這個過程,我現在可以擴大控制範圍,並且不會因爲限制而在我身邊移動。


正如指出的爲0x7FFFFFFF,如果你申請一個CATransform3DMakeScale到層,它不會自動應用的限制,所以你不會看到它移到如果你申請CGAffineTransformMakeScale到視圖等。但是,如果您執行任何操作來重新應用約束條件(setNeedsLayout或對任何UIView對象所做的任何更改都可能導致約束被重新應用),則該視圖將隨您移動。因此,如果您在重新應用約束之前將圖層的變換恢復爲身份,則可能會「偷偷摸摸」,但關閉自動佈局或僅修復約束可能最安全。

+0

非常全面的答案!謝謝!我會花一些時間來了解整個事情,但謝謝你花時間回答! – JoshDG 2013-04-30 19:40:19

+0

非常hepfull答案!你救了我的一天! – Amnysia 2013-11-07 17:31:39

+0

我剛剛在故事板中改變了頂端和主要限制條件,但類似的問題仍然存在,預計子視圖不會跳到左側,而是不會粘到超級視圖的右側(它應該是),它會一致地移動到它的位置,但在動畫期間它總是處於錯誤的位置=/ – 2014-03-23 06:01:35