如何中止和扭轉UIView的動畫

本文轉載至  http://codego.net/576089/app

 

我有它收縮時碰到切換按鈕UIView的動畫跳和它擴展恢復到原來的大小當再次接觸到按鈕。密封式前大燈一切都工做得很好。問題是,動畫師注意到-例如3秒鐘。在此期間,我仍是但願可以進行交互的接口。當再次接觸到跳躍鍵,而動畫仍在進行中的動畫應該就停在哪裏是和反向。 在蘋果Q&正如我已經找到了方法,全部的動畫 但我沒有看到從這裏扭轉動畫(並省略原動畫的其他部分)的方式。我該怎麼辦呢?ide

- (IBAction)toggleMeter:(id)sender {
 if (self.myView.hidden) {  
  self.myView.hidden = NO;
  [UIView animateWithDuration:3 animations:^{
   self.myView.transform = expandMatrix;
  } completion:nil];
 } else {
  [UIView animateWithDuration:3 animations:^{
   self.myView.transform = shrinkMatrix;
  } completion:^(BOOL finished) {
   self.myView.hidden = YES;
  }];
 }
}

+
本文地址 :CodeGo.net/576089/ 動畫

-------------------------------------------------------------------------------------------------------------------------
1. 除了如下(在此咱們抓住當前狀態從表現層,中止動畫,從新保存從表現層的當前狀態,並啓動新的動畫),有不少簡單的解決方案。 DM作的基於塊的動畫,若是你想中止動畫並啓動一個新的活力,一種能將UIViewAnimationOptionBeginFromCurrentState選項:
this

[UIView animateWithDuration:3.0
      delay:0.0
     options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
     animations:^{
      // specify the new `frame`, `transform`, etc. here
     }
     completion:NULL];

您能夠經過新建中止當前動畫師和動畫啓動在哪裏當前的不放過實現這一目標。爲此 CodeGo.net,您能夠與quartz2D: 添加QuartzCore。你的項目,若是你尚未。 導入所需的頭,若是你已經有了不:spa

#import <QuartzCore/QuartzCore.h>

讓你的腳中止現有的動畫:.net

[self.subview.layer removeAllAnimations];

獲取參考電流表示層(即視圖的狀態,由於它偏偏是在這code

CALayer *currentLayer = self.subview.layer.presentationLayer;

重置transform(或者frame或不管)根據在當前值presentationLayerorm

self.subview.layer.transform = currentLayer.transform;

如今,領導者從該transform(或者frame或其餘)爲新值:接口

[UIView animateWithDuration:1.0
      delay:0.0
     options:UIViewAnimationOptionAllowUserInteraction
     animations:^{
      self.subview.layer.transform = newTransform;
     }
     completion:NULL];

把全部的一塊兒,這裏就是個人切換尺度變換的2.0倍肯定並背例程:ci

- (IBAction)didTouchUpInsideAnimateButton:(id)sender
{
 CALayer *currentLayer = self.subview.layer.presentationLayer;
 [self.subview.layer removeAllAnimations];
 self.subview.layer.transform = currentLayer.transform;
 CATransform3D newTransform;
 self.large = !self.large;
 if (self.large)
  newTransform = CATransform3DMakeScale(2.0, 2.0, 1.0);
 else
  newTransform = CATransform3DIdentity;
 [UIView animateWithDuration:1.0
       delay:0.0
      options:UIViewAnimationOptionAllowUserInteraction
      animations:^{
       self.subview.layer.transform = newTransform;
      }
      completion:NULL];
}

或者,若是你想切換frame尺寸從100×100到200×200和背部:

- (IBAction)didTouchUpInsideAnimateButton:(id)sender
{
 CALayer *currentLayer = self.subview.layer.presentationLayer;
 [self.subview.layer removeAllAnimations];
 CGRect newFrame = currentLayer.frame;
 self.subview.frame = currentLayer.frame;
 self.large = !self.large;
 if (self.large)
  newFrame.size = CGSizeMake(200.0, 200.0);
 else
  newFrame.size = CGSizeMake(100.0, 100.0);
 [UIView animateWithDuration:1.0
       delay:0.0
      options:UIViewAnimationOptionAllowUserInteraction
      animations:^{
       self.subview.frame = newFrame;
      }
      completion:NULL];
}

順便說一句,雖然通常並不重要的真快動畫,緩慢動畫像你這樣的,你可能須要設置動畫的持續時間是扭轉多遠你在你目前的動畫已經提早拒絕(例如,若是你是0.5秒到3.0秒的動畫,當反向,難以想象不要想拿3.0秒扭轉這一方面作了跳躍燈塔動畫師的小部分,而僅僅0.5秒)。這可能看起來像:

- (IBAction)didTouchUpInsideAnimateButton:(id)sender
{
 CFTimeInterval duration = kAnimationDuration;    // default the duration to some constant
 CFTimeInterval currentMediaTime = CACurrentMediaTime(); // get the current media time
 static CFTimeInterval lastAnimationStart = 0.0;   // media time of last animation (zero the first time)
 // if we previously animated, then calculate how far along in the previous animation we were
 // and we'll use that for the duration of the reversing animation; if larger than
 // kAnimationDuration that means the prior animation was done, so we'll just use
 // kAnimationDuration for the length of this animation
 if (lastAnimationStart)
  duration = MIN(kAnimationDuration, (currentMediaTime - lastAnimationStart));
 // save our media time for future reference (i.e. future invocations of this routine)
 lastAnimationStart = currentMediaTime;
 // if you want the animations to stay relative the same speed if reversing an ongoing
 // reversal, you can backdate the lastAnimationStart to what the lastAnimationStart
 // would have been if it was a full animation; if you don't do this, if you repeatedly
 // reverse a reversal that is still in progress, they'll incrementally speed up.
 if (duration < kAnimationDuration)
  lastAnimationStart -= (kAnimationDuration - duration);
 // grab the state of the layer as it is right now
 CALayer *currentLayer = self.subview.layer.presentationLayer;
 // cancel any animations in progress
 [self.subview.layer removeAllAnimations];
 // set the transform to be as it is now, possibly in the middle of an animation
 self.subview.layer.transform = currentLayer.transform;
 // toggle our flag as to whether we're looking at large view or not
 self.large = !self.large;
 // set the transform based upon the state of the `large` boolean
 CATransform3D newTransform;
 if (self.large)
  newTransform = CATransform3DMakeScale(2.0, 2.0, 1.0);
 else
  newTransform = CATransform3DIdentity;
 // now animate to our new setting
 [UIView animateWithDuration:duration
       delay:0.0
      options:UIViewAnimationOptionAllowUserInteraction
      animations:^{
       self.subview.layer.transform = newTransform;
      }
      completion:NULL];
}


2. 有一招作到這一點,但它是必要寫的收縮(和另外一個相似的一個利差):

- (void) shrink { 
 [UIView animateWithDuration:0.3
      animations:^{
      self.myView.transform = shrinkALittleBitMatrix;
      }
      completion:^(BOOL finished){
       if (continueShrinking && size>0) {
        size=size-1;
        [self shrink];  
       }
      }];
}

0.3秒每個在其中收縮整個動畫的十分之一如今跳,關鍵是要縮小動畫動畫的3秒打入10(或10多,固然):shrinkALittleBitMatrix。在每一個動畫完成後調用只當布爾伊瓦continueShrinking當int是真實的Ivarsize爲正(在全尺寸的視圖。將大小=10,以最小的尺寸的視圖。將大小=0)。當您按下按鈕更改伊瓦continueShrinking爲false,而後調用expand。這將中止動畫在小於0.3秒。 好了,你必須填寫詳細資料,但我但願它能幫助。 + 
3. 才能評價與互動的用戶界面,而動畫是一個

[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
 //Your animation
} completion:^(BOOL finished) {
}];

+

 

本文地址 :CodeGo.net/576089/

相關文章
相關標籤/搜索