1

我有一個UIPanGestureRecognizer。它工作正常。我做了一個if語句,所以當有人觸摸這張圖片時,它會是alpha 0.7,它會大1.5倍。 alpha工作正常,但是當我輸入CAAffineTransformMakeScale方法時,我的圖像不會移動。當我想調整它時,我的PanGestureRecognizer不會移動

這是我的代碼:

- (IBAction)Bloemen:(UIPanGestureRecognizer *)recognizer { 

    CGPoint translation = [recognizer translationInView:self.view]; 
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
            recognizer.view.center.y + translation.y); 
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view]; 

    if (UIGestureRecognizerStateBegan) 
    { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDelay:0.1]; 
     [UIView setAnimationDuration:0.4]; 

     bloemen.alpha = 0.7f; 
     bloemen.transform = CGAffineTransformMakeScale(1.5,1.5); 

     [UIView commitAnimations]; 
    } 
    if (UIGestureRecognizerStateEnded) { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDelay:0.1]; 
     [UIView setAnimationDuration:0.1]; 

     bloemen.alpha = 1.0f; 
     bloemen.transform = CGAffineTransformIdentity; 

     [UIView commitAnimations]; 
    } 
} 
+0

如果您使用自動佈局,我可以告訴你如何動畫約束條件的變化(因爲移動後的視圖會在重新應用約束條件時返回到原始位置,比如當你做最後的動畫時),但是除非你使用自動佈局,否則不要想問題。上面代碼片段中的關鍵問題在於'if'子句,如下所述。 – Rob 2013-04-23 23:18:48

回答

0

關鍵的問題是,你的if語句不檢查state財產。它應該是:

if (recognizer.state == UIGestureRecognizerStateBegan) 
{ 
    // began code here 
} 
else if (recognizer.state == UIGestureRecognizerStateEnded) 
{ 
    // ended code here 
} 

另請注意,此手勢識別器僅在自動佈局關閉時纔有效。如果您使用自動佈局,則必須更改約束。


如果你原諒文體的觀察,我也可能會傾向於建議:

  • 使用基於塊的動畫;

  • 不能引用非局部變量,如果沒有必要(即參考recognizer.view而非bloemen),這使得它更易於重複使用此處理拖動並選擇要添加這個手勢各種UIView物體掉落;和

  • 使用標準的命名約定,使用小寫字母開始方法名稱並遵循verbNoun約定。

無的,這是關鍵,請用或無視你認爲合適的,但是這顯示出一些最佳做法:

- (void)handlePan:(UIPanGestureRecognizer *)recognizer 
{  
    CGPoint translation = [recognizer translationInView:self.view]; 
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
             recognizer.view.center.y + translation.y); 
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view]; 

    if (recognizer.state == UIGestureRecognizerStateBegan) 
    { 
     [UIView animateWithDuration:0.4 
           delay:0.0 // you had 0.1, but seems worthwhile to give immediate feedback 
          options:0 
         animations:^{ 
          recognizer.view.alpha = 0.7f; 
          recognizer.view.transform = CGAffineTransformMakeScale(1.5,1.5); 
         } 
         completion:nil]; 
    } 
    if (recognizer.state == UIGestureRecognizerStateEnded) 
    { 
     [UIView animateWithDuration:0.1 
           delay:0.0 // you had 0.1, but seems worthwhile to give immediate feedback 
          options:0 
         animations:^{ 
          recognizer.view.alpha = 1.0f; 
          recognizer.view.transform = CGAffineTransformIdentity; 
         } 
         completion:nil]; 
    } 
} 
相關問題