Facebook open-source animation library POP-POPDecayAnimation application, animationcss animation library

Source: Internet
Author: User

Facebook open-source animation library POP-POPDecayAnimation application, animationcss animation library

The introduction of POPDecayAnimation first references some content written by others, and basically describes some of its points of attention;

Decay Animation is another very special Animation provided by POP, which achieves a attenuation effect. This animation has an important parameter velocity (rate). It is generally not used for spontaneous object animation, but for symbiosis with user interaction. This is very similar to the UIDynamic introduced by iOS7. If you want to achieve some physical effects, this is also a very good choice.

The Decay animation has only fromValue without toValue, and then uses velocity to perform the attenuation operation. This should be the case if we want to make a brake effect.

POPDecayAnimation *anim = [POPDecayAnimation animWithPropertyNamed:kPOPLayerPositionX]; anim.velocity = @(100.0); anim.fromValue =  @(25.0); //anim.deceleration = 0.998; anim.completionBlock = ^(POPAnimation *anim, BOOL finished) {   if (finished) {NSLog(@"Stop!");}}; 

This animation causes the object to slow down at a rate of 25.0 points/s from the point 100 of the X coordinate. It is worth mentioning that velocity must have the same structure as the attribute of your operation. If you operate bounds and want to achieve a diffusion effect from water drops to the desktop, it should be [NSValue valueWithCGRect: CGRectMake (0, 0, 20.0, 20.0)]

If velocity is a negative value, it will decrease in reverse order.

Deceleration (negative acceleration) is a value that you will rarely use. The default value is 0.998 of our Earth. If you develop it for the Martian, this value is more suitable for you to use 0.376.

 

Example 1: Create a POPDecayAnimation animation to accelerate the x-axis movement.

// 1: Initialize the first view block if (self. myView = nil) {self. myView = [[UIView alloc] initWithFrame: CGRectMake (0, 80, 30, 30)]; self. myView. backgroundColor = [UIColor redColor]; [self. view addSubview: self. myView];} // create a POPDecayAnimation animation to slow down the X axis motion. The speed is used to calculate the running distance. The toValue attribute does not exist. ** anSpring = [POPDecayAnimation placement: kPOPLayerPositionX]; anSpring. velocity = @ (500); // rate anSpring. beginTime = CACurrentMediaTime () + 1.0f; [anSpring setCompletionBlock: ^ (POPAnimation * prop, BOOL fint) {if (fint) {NSLog (@ "myView =% @", NSStringFromCGRect (self. myView. frame) ;}}]; [self. myView pop_addAnimation: anSpring forKey: @ "myViewposition"];

The printed value is myView = {247.00485229492188, 80}, {30, 30}, indicating that the X axis is about 247;

Example 2: Create a POPDecayAnimation animation that continuously rotates

// 2: initialize a view block if (self. myRotationView = nil) {self. myRotationView = [[UIView alloc] initWithFrame: CGRectMake (20,130, 30, 30)]; self. myRotationView. backgroundColor = [UIColor redColor]; [self. view addSubview: self. myRotationView];} // create a POPDecayAnimation animation that continuously rotates [self resumimation]; encapsulation method:-(void) policimation {[self. myRotationView. layer pop_removeAllAnimations]; POPDecayAnimation * anRotaion = [POPD EcayAnimation animation]; anRotaion. property = [POPAnimatableProperty propertyWithName: kPOPLayerRotation]; if (self. animated) {anRotaion. velocity = @ (-150);} else {anRotaion. velocity = @ (150); anRotaion. fromValue = @ (25.0);} self. animated =! Self. animated; anRotaion. completionBlock = ^ (POPAnimation * anim, BOOL finished) {if (finished) {[self completed imation] ;}; [self. myRotationView. layer pop_addAnimation: anRotaion forKey: @ "myRotationView"];}

Note that the constant kPOPLayerRotation is used on the layer, so we need to load it to the layer of the corresponding view during use;

Example 3: A drag view is run. After the drag is finished, it has the effect of slowing down. When you click the view, it stops all animation effects. After the animation is stopped, it has a judgment, whether the current view is on the screen or not, and if yes, the view rebounded;

# Import "DecayViewController. h "# import <POP. h> @ interface DecayViewController () <POPAnimationDelegate> @ property (nonatomic) UIControl * dragView;-(void) addDragView;-(void) touchDown :( UIControl *) sender;-(void) handlePan :( UIPanGestureRecognizer *) recognizer; @ end @ implementation DecayViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor whiteColor]; [self addDragVi Ew] ;}# pragma mark-POPAnimationDelegate-(void) pop_animationDidApply :( POPDecayAnimation *) anim {// determine whether the screen wall is BOOL isDragViewOutsideOfSuperView =! CGRectContainsRect (self. view. frame, self. dragView. frame); if (isDragViewOutsideOfSuperView) {CGPoint currentVelocity = [anim. velocity CGPointValue]; CGPoint velocity = CGPointMake (currentVelocity. x,-currentVelocity. y); POPSpringAnimation * positionAnimation = [POPSpringAnimation animationWithPropertyNamed: kPOPLayerPosition]; positionAnimation. velocity = [NSValue valueWithCGPoint: velocity]; positionAnimation. toValue = [NSValue valueWithCGPoint: self. view. center]; [self. dragView. layer pop_addAnimation: positionAnimation forKey: @ "layerPositionAnimation"] ;}# pragma mark-Private Instance methods-(void) addDragView {// drag UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (handlePan :)]; self. dragView = [[UIControl alloc] initWithFrame: CGRectMake (0, 0,100,100)]; self. dragView. center = self. view. center; self. dragView. layer. cornerRadius = CGRectGetWidth (self. dragView. bounds)/2; self. dragView. backgroundColor = [UIColor redColor]; [self. dragView addTarget: self action: @ selector (touchDown :) forControlEvents: UIControlEventTouchDown]; [self. dragView addGestureRecognizer: recognizer]; [self. view addSubview: self. dragView];} // when clicked, all animation effects will be stopped-(void) touchDown :( UIControl *) sender {[sender. layer pop_removeAllAnimations];}-(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]; // gesture to end if (recognizer. state = UIGestureRecognizerStateEnded) {CGPoint velocity = [recognizer velocityInView: self. view]; POPDecayAnimation * positionAnimation = [POPDecayAnimation animationWithPropertyNamed: kPOPLayerPosition]; positionAnimation. delegate = self; positionAnimation. velocity = [NSValue valueWithCGPoint: velocity]; [recognizer. view. layer pop_addAnimation: positionAnimation forKey: @ "layerPositionAnimation"] ;}}@ end

POPAnimationDelegate is also used in the instance.

 <POPAnimatorDelegate> - (void)pop_animationDidStart:(POPAnimation *)anim;- (void)pop_animationDidStop:(POPAnimation *)anim finished:(BOOL)finished;- (void)pop_animationDidReachToValue:(POPAnimation *)anim;- (void)pop_animationDidApply:(POPAnimation *)anim;

 

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.