- (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; }]; }}
+UIViewAnimationOptionBeginFromCurrentState
选项:[UIView animateWithDuration:3.0 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction animations:^{ // specify the new `frame`, `transform`, etc. here } completion:NULL];
您可以通过新建停止当前动画师和动画启动在哪里当前的不放过实现这一目标。为此 CodeGo.net,您可以与quartz2D: 添加QuartzCore。你的项目,如果你还没有。 导入所需的头,如果你已经有了不:#import <QuartzCore/QuartzCore.h>
让你的脚停止现有的动画:[self.subview.layer removeAllAnimations];
获取参考电流表示层(即视图的状态,因为它恰恰是在这CALayer *currentLayer = self.subview.layer.presentationLayer;
重置transform
(或者frame
或无论)根据在当前值presentationLayer
: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倍确定并背例程:- (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];}
+ - (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秒。 好了,你必须填写详细资料,但我希望它能帮助。 + [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ //Your animation} completion:^(BOOL finished) {}];
+联系客服