gpt4 book ai didi

iphone - 振动 UI 项目的最佳方式?

转载 作者:行者123 更新时间:2023-12-03 18:42:37 28 4
gpt4 key购买 nike

我正在尝试振动/摇动一个 UI 元素,即一个控件,就好像它被敲击一样,并且正在共振。我可以使用核心动画将其垂直、水平或同时摇动一个像素:

CABasicAnimation* rotationXAnimation;
rotationXAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
rotationXAnimation.fromValue = [NSNumber numberWithFloat: ((UIControl *) sender).center.y-1.0 ];
rotationXAnimation.toValue = [NSNumber numberWithFloat: ((UIControl *) sender).center.y+1.0 ];
rotationXAnimation.duration = 0.2;
rotationXAnimation.cumulative = NO;
rotationXAnimation.repeatCount = 10.0;
rotationXAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

[((UIControl *) sender).layer addAnimation:rotationXAnimation forKey:@"upDownAnimation"];

或者我可以围绕 z 轴来回旋转一个小弧度(-M_PI * 0.02 和 M_PI * 0.02):

CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.fromValue = [NSNumber numberWithFloat: -M_PI * 0.02 ];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 0.02 ];
rotationAnimation.duration = 0.2;
rotationAnimation.cumulative = NO;
rotationAnimation.repeatCount = 10.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

[((UIControl *) sender).layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

这些看起来都不那么令人愉快。有人有一些好的选择吗?我特别感谢尝试很酷的 keyPaths 想法。

谢谢!

更新:

下面我收缩和扩展控制的情况更好,但我仍在寻找其他想法。

CABasicAnimation* scaleAnimation;
scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat: 0.95 ];
scaleAnimation.toValue = [NSNumber numberWithFloat: +1.05 ];
scaleAnimation.duration = 0.1;
scaleAnimation.cumulative = NO;
scaleAnimation.repeatCount = 10.0;
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

[((UIControl *) sender).layer addAnimation:scaleAnimation forKey:@"scaleAnimation"];

最佳答案

在用户输入错误后,我正在摇动我的输入 View 之一:

- (void)earthquake:(UIView*)itemView
{
CGFloat t = 2.0;
CGAffineTransform leftQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, t, -t);
CGAffineTransform rightQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, t);

itemView.transform = leftQuake; // starting point

[UIView beginAnimations:@"earthquake" context:itemView];
[UIView setAnimationRepeatAutoreverses:YES]; // important
[UIView setAnimationRepeatCount:4];
[UIView setAnimationDuration:0.07];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(earthquakeEnded:finished:context:)];

itemView.transform = rightQuake; // end here & auto-reverse

[UIView commitAnimations];
}

- (void)earthquakeEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if ([finished boolValue])
{
UIView* item = (UIView *)context;
item.transform = CGAffineTransformIdentity;
}
}

这看起来很自然,我从一些我不记得的互联网线程中获得了这段代码。

关于iphone - 振动 UI 项目的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1713123/

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