gpt4 book ai didi

ios - UIView摇动动画

转载 作者:IT王子 更新时间:2023-10-29 07:31:14 26 4
gpt4 key购买 nike

我正在尝试在按下按钮时使 UIView 摇动。

我正在调整我在 http://www.cimgf.com/2008/02/27/core-animation-tutorial-window-shake-effect/ 上找到的代码.

但是,通过尝试修改以下代码来摇动 UIView,它不起作用:

- (void)animate {
const int numberOfShakes = 8;
const float durationOfShake = 0.5f;
const float vigourOfShake = 0.1f;

CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animation];

CGRect frame = lockView.frame;

CGMutablePathRef shakePath = CGPathCreateMutable();
CGPathMoveToPoint(shakePath, NULL, CGRectGetMinX(frame), CGRectGetMinY(frame));

for (int index = 0; index < numberOfShakes; ++index) {
CGPathAddLineToPoint(shakePath, NULL, CGRectGetMinX(frame) - frame.size.width * vigourOfShake, CGRectGetMinY(frame));

CGPathAddLineToPoint(shakePath, NULL, CGRectGetMinX(frame) + frame.size.width * vigourOfShake, CGRectGetMinY(frame));
}

CGPathCloseSubpath(shakePath);

shakeAnimation.path = shakePath;
shakeAnimation.duration = durationOfShake;


[lockView.layer addAnimation:shakeAnimation forKey:@"frameOrigin"];

}

最佳答案

那篇文章是我写的。这对于 UIView 来说太过分了,而且参数是针对 OSX 应用程序的。改为这样做。

CABasicAnimation *animation = 
[CABasicAnimation animationWithKeyPath:@"position"];
[animation setDuration:0.05];
[animation setRepeatCount:8];
[animation setAutoreverses:YES];
[animation setFromValue:[NSValue valueWithCGPoint:
CGPointMake([lockView center].x - 20.0f, [lockView center].y)]];
[animation setToValue:[NSValue valueWithCGPoint:
CGPointMake([lockView center].x + 20.0f, [lockView center].y)]];
[[lockView layer] addAnimation:animation forKey:@"position"];

您必须在 from 和 to 值中使用 duration 和 repeatCount 参数以及距中心的 x 距离,但它应该能满足您的需要。我希望这有帮助。如果您有任何问题,请告诉我。

---


swift 3.0

let midX = lockView.center.x
let midY = lockView.center.y

let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.06
animation.repeatCount = 4
animation.autoreverses = true
animation.fromValue = CGPoint(x: midX - 10, y: midY)
animation.toValue = CGPoint(x: midX + 10, y: midY)
layer.add(animation, forKey: "position")

关于ios - UIView摇动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3844557/

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