使用动画效果实现UIView的跳动效果
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];
//scale it down
CABasicAnimation *shrinkAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
shrinkAnimation.delegate = self;
shrinkAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
shrinkAnimation.toValue = [NSNumber numberWithFloat:0.0];
[self.newLabel.layer addAnimation:shrinkAnimation forKey:@"shrinkAnimation"];
//fade it out
CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnimation.toValue = [NSNumber numberWithFloat:0.0];
fadeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[[self.newLabel layer] addAnimation:fadeAnimation forKey:@"fadeAnimation"];
// make it jump a couple of times
CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef positionPath = CGPathCreateMutable();
CGPathMoveToPoint(positionPath, NULL, [self.newLabel layer].position.x, [self.newLabel layer].position.y);
//落下3次
CGPathAddQuadCurveToPoint(positionPath, NULL, [self.newLabel layer].position.x, -[self.newLabel layer].position.y, [self.newLabel layer].position.x, [self.newLabel layer].position.y);
CGPathAddQuadCurveToPoint(positionPath, NULL, [self.newLabel layer].position.x, -[self.newLabel layer].position.y *1.5, [self.newLabel layer].position.x, [self.newLabel layer].position.y);
CGPathAddQuadCurveToPoint(positionPath, NULL, [self.newLabel layer].position.x, -[self.newLabel layer].position.y *1.25, [self.newLabel layer].position.x, [self.newLabel layer].position.y);
positionAnimation.path = positionPath;
positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[[self.newLabel layer] addAnimation:positionAnimation forKey:@"positionAnimation"];
[CATransaction commit];
如果要重复可以使用定时器来做:
[NSTimer scheduledTimerWithTimeInterval:3. target:self selector:@selector(jump) userInfo:nil repeats:YES];