2013-11-09 37 views
0

我有一個用戶可以觸摸的圓圈,當您觸摸它時,它將更改大小,然後恢復到其原始大小以指示您已觸摸它。我希望用戶能夠隨時隨地觸摸它,而不管它是否正在進行動畫製作。我更新了我的動畫以使用UIViewAnimationOptionAllowUserInteraction標誌,它允許我在動畫期間觸摸它,但它不像我預期的那樣行事。在用戶交互過程中重置動畫狀態

我想我需要以某種方式停止當前正在播放的動畫,將大小重置爲正常,然後再播放,這是否正確,如果是這樣,我該怎麼做?如果不是,我該怎麼辦?

- (void)pop{ 
    [view.layer removeAllAnimations]; 

    [UIView animateWithDuration:0.2 
         delay:0.0 
        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction // reverse back to original value 
       animations:^{ 
        // scale down 20% 
        self.transform = CGAffineTransformMakeScale(0.8, 0.8); 
       } completion:^(BOOL finished) { 
        self.transform = CGAffineTransformMakeScale(1, 1); 
       }]; 
} 
+0

慣於使動畫看起來光滑,但它不會像用戶按下兩次(因爲它是從當前狀態進行,而不是重置到原來的狀態?) – Chris

回答

0
self.btn = [[UIButton alloc] initWithFrame:CGRectMake(200, 200, 100, 30)]; 
[self.btn addTarget:self action:@selector(tapDown:) forControlEvents:UIControlEventTouchDown]; 
[self.btn addTarget:self action:@selector(tapUp:) forControlEvents:UIControlEventTouchUpInside]; 


- (IBAction)tapDown:(id)sender 
{ 
    [UIView animateWithDuration:0.2 
          delay:0.0 
         options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction 
        animations:^{ 
         // scale down 20% 
         self.btn.transform = CGAffineTransformMakeScale(0.8, 0.8); 
        } completion:nil]; 
} 


- (IBAction)tapUp:(id)sender { 
    [UIView animateWithDuration:0.2 
          delay:0.0 
         options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction // reverse back to original value 
        animations:^{ 
         // scale up to 100% 
         self.btn.transform = CGAffineTransformMakeScale(1, 1); 
        } completion:nil]; 
} 
+0

我只是無意中發現了這一點,似乎沒有給我想要的效果。它有點看起來像沒有發生第二次觸摸 - 我更新了我的代碼 – Chris

+0

我只是編輯答案。我用我的例子按鈕。我希望這個更接近你想實現的效果 – Igor

+0

從UX-pov我會說這是一個更好的解決方案。該按鈕似乎被按下,直到用戶釋放他的手指,它彈回備份... –

0

知道這是不是你想要的?當玩家點擊並彈出被稱爲動畫應重新開始?

- (void)pop{ 
    self.transform = CGAffineTransformMakeScale(1, 1); // If you want the animation to start over you need to reset the size before staring it again 
    [view.layer removeAllAnimations]; 

    [UIView animateWithDuration:0.2 
        delay:0.0 
       options: options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseOut| UIViewAnimationOptionAllowUserInteraction // reverse back to original value 
      animations:^{ 
       // scale down 20% 
       self.transform = CGAffineTransformMakeScale(0.8, 0.8); 
      } completion:^(BOOL finished) { 
       self.transform = CGAffineTransformMakeScale(1, 1); 
      }]; 
} 
+0

這正是我曾經提到過的人刪除所有動畫,但它沒有奏效! – Chris

+0

您忘了將視圖重新調整回原來的狀態...... –