按钮动画像的iOS游戏中心键 [英] Button Animate like ios game center button
问题描述
我试图让我的按钮动画像IOS游戏中心的按钮。他们似乎摇晃和浮约仍像一个泡沫屏幕。我试过随意在屏幕上移动我的按钮,使他们在一个恒定的圆环形路径同时移动,但它是不一样的效果。
I am trying to make my buttons animate like buttons in ios game center. they seem to wobble and float about the screen like a bubble. I've tried moving my buttons randomly on the screen, make them move in a constant cirular path at the same time but it's not the same effect.
我需要摆动样的效果。任何想法AP preciated
I need a wobble kind of effect. any ideas are appreciated
推荐答案
结合了几个 CAKeyframeAnimations
会给你的结果。需要一个用于按照一个圆的位置,和一个用于每一被定时有点不同来实现的摆动效应尺度(X / Y)。看看这个例子:
Combining a few CAKeyframeAnimations
will give you the result. You need one for the position to follow a circle, and one for each scale (x/y) which are timed a bit different to achieve the wobble-effect. Check out the example:
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
view.backgroundColor = [UIColor redColor];
view.layer.cornerRadius = view.frame.size.width/2;
[self.view addSubview:view];
//create an animation to follow a circular path
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//interpolate the movement to be more smooth
pathAnimation.calculationMode = kCAAnimationPaced;
//apply transformation at the end of animation (not really needed since it runs forever)
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
//run forever
pathAnimation.repeatCount = INFINITY;
//no ease in/out to have the same speed along the path
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
pathAnimation.duration = 5.0;
//The circle to follow will be inside the circleContainer frame.
//it should be a frame around the center of your view to animate.
//do not make it to large, a width/height of 3-4 will be enough.
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGRect circleContainer = CGRectInset(view.frame, 23, 23);
CGPathAddEllipseInRect(curvedPath, NULL, circleContainer);
//add the path to the animation
pathAnimation.path = curvedPath;
//release path
CGPathRelease(curvedPath);
//add animation to the view's layer
[view.layer addAnimation:pathAnimation forKey:@"myCircleAnimation"];
//create an animation to scale the width of the view
CAKeyframeAnimation *scaleX = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
//set the duration
scaleX.duration = 1;
//it starts from scale factor 1, scales to 1.05 and back to 1
scaleX.values = @[@1.0, @1.05, @1.0];
//time percentage when the values above will be reached.
//i.e. 1.05 will be reached just as half the duration has passed.
scaleX.keyTimes = @[@0.0, @0.5, @1.0];
//keep repeating
scaleX.repeatCount = INFINITY;
//play animation backwards on repeat (not really needed since it scales back to 1)
scaleX.autoreverses = YES;
//ease in/out animation for more natural look
scaleX.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//add the animation to the view's layer
[view.layer addAnimation:scaleX forKey:@"scaleXAnimation"];
//create the height-scale animation just like the width one above
//but slightly increased duration so they will not animate synchronously
CAKeyframeAnimation *scaleY = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
scaleY.duration = 1.5;
scaleY.values = @[@1.0, @1.05, @1.0];
scaleY.keyTimes = @[@0.0, @0.5, @1.0];
scaleY.repeatCount = INFINITY;
scaleY.autoreverses = YES;
scaleX.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[view.layer addAnimation:scaleY forKey:@"scaleYAnimation"];
这篇关于按钮动画像的iOS游戏中心键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!