How to stop and twist a UIView animation

Source: Internet
Author: User

This article reprinted to http://codego.net/576089/

I have it shrinking when I touch the toggle button UIView The animation jumps and it expands back to the original size when touching the button again. Sealed front headlights everything works well. The problem is that the animator notices-for example, 3 seconds. During this time, I still want to be able to interact with the interface. When you touch the jump key again, and the animation is still in progress, the animation should stop where it is and reverse. In Apple q& as I have found the way to all the animations but I don't see the way to twist the animation from here (and omit the rest of the original animation). What should I do?

- (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;  }]; }}

+
This address: codego.net/576089/

--------------------------------------------------------------------------------------------------------------- ----------
1. In addition to the following (here we seize the current state from the presentation layer, stop the animation, resave the current state from the presentation layer, and start the new animation), there are many simple solutions. The DM does a block-based animation, if you want to stop the animation and start a new vitality, an option that will be UIViewAnimationOptionBeginFromCurrentState :

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

You can do this by creating a new stop for the current animator and the animation to start at where the current is not spared. For this codego.net, you can add Quartzcore with quartz2d: Your project, if you haven't. Import the required headers if you already have not:

#import <QuartzCore/QuartzCore.h>

Let your feet stop the existing animations:

[self.subview.layer removeAllAnimations];

Gets the reference current representation layer (that is, the state of the view, because it is precisely in this

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

Reset transform (or frame either) based on the current value presentationLayer :

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

Now, the leader from that (or the transform frame other) is the new value:

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

Put it all together, here's my switching scale transformation twice times OK and back routines:

- (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];}

Or, if you want to switch frame dimensions from 100x100 to 200x200 and back:

- (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];}

By the way, although it is generally not important to really fast animations, slow animations like yours, you may need to set the duration of the animation is to twist how far you have in your current animation has been prematurely rejected (for example, If you are 0.5 seconds to 3 seconds of animation, when in reverse, it is inconceivable not to take 3 seconds to reverse this aspect and do a small part of the jumping Lighthouse Animator, and just 0.5 seconds). This may look like this:

-(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 PR Evious animation we were//and we'll use this for the duration of the reversing animation; If larger than//kanimationduration that means the prior animation is done, so we'll just use//kanimationduration for The length of this animation if (lastanimationstart) duration = MIN (kanimationduration, (currentmediatime-lastanimation Start)); Save our media time for future reference (i.e. invocations of this routine) Lastanimationstart = Currentmediatim E If you want the animations to stay relative the same speed if reversing a ongoing//reversal, you can backdate the LA StanimAtionstart to what the Lastanimationstart//would has been if it is a full animation; If you are repeatedly//reverse a reversal that's 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's now, possibly in the middle of a animation self.subview.layer.transform = Currentlaye R.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.tran      Sform = Newtransform; } Completion:null];}

+
2. There is a trick to do this, but it is necessary to write the contraction (and another similar one spreads):

- (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 seconds each in which contraction of the entire animation of One-tenth now jumps, the key is to reduce the animated animation of 3 seconds into 10 (or 10 more, of course): shrinkALittleBitMatrix . After each animation is completed call only Bourgival continueShrinking when int is true Ivar is size positive (in full-size view. Size = 10, with a view of the smallest size. Will size =0). When you press the button, change Eva continueShrinking to False, and then call expand . This will stop the animation at less than 0.3 seconds. Well, you have to fill in the details, but I hope it can help. +
3. Ability to evaluate and interact with the user interface, while the animation is a

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

+


This address: codego.net/576089/

How to stop and twist a UIView animation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.