gpt4 book ai didi

ios - 按钮动画像 ios 游戏中心按钮

转载 作者:可可西里 更新时间:2023-11-01 03:38:41 24 4
gpt4 key购买 nike

我正在尝试让我的按钮像 ios 游戏中心中的按钮一样动画。它们似乎像气泡一样在屏幕上摇晃和漂浮。我试过在屏幕上随机移动我的按钮,让它们同时以恒定的圆形路径移动,但效果不一样。

我需要一种摇摆效果。任何想法表示赞赏

最佳答案

结合一些 CAKeyframeAnimations 会给你结果。您需要一个用于跟随圆圈的位置,以及一个用于每个比例(x/y)的时间有点不同以实现摆动效果。查看示例:

    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 - 按钮动画像 ios 游戏中心按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23927047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com