- (void)viewDidLoad
{
[super viewDidLoad];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint abc =chem1.center;
abc.y= 480;
chem1.center=abc;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint def =chem2.center;
def.y= 480 ;
chem2.center=def;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint hij =nat1.center;
hij.y= 480;
nat1.center=hij;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint klm =nat2.center;
klm.y= 480;
nat2.center=klm;
}
非常感谢您的帮助
最佳答案
您的问题分为两部分:如何点击动画图像和如何拖动图像。
如何点按动画图像。
背景
当您为 View (或更准确地说它的图层)设置动画时,您更改的属性(在您的情况下为 position
(通过设置 center
))更改为它的最终值(value)马上。如果你只是制作动画(比如使用 CABasicAnimation,那么 properly 可能根本不会改变。
这意味着如果您尝试点击 View (或层),它将位于您将其移动到的位置(它的最终位置(无论动画的持续时间或重复次数如何)。
HitTest 移动层
为了能够对正在移动的东西进行 HitTest ,您需要针对 presentationLayer
进行 HitTest 您尝试点击的 View 层。
CGPoint touchPoint = [gestureRecognizer locationInView:[self view]];
if ([[[[self movingView] layer] presentationLayer] hitTest:touchPoint]) {
// Yeah, you hit the moving (animating) layer!
}
现在用户已经点击了移动 View (在您的案例中是图像),您可以开始处理拖动。
如何拖动图片
当您拖动图像时,您会将图层移动到用户手指拖动图像的位置。
移至移动 View 的动画
首先,您应该取消该 View 正在进行的动画(为动画命名可以让您只取消一个动画。
[[[self movingView] layer] removeAllAnimations];
或者如果您为运动动画设置了名称。
[[[self movingView] layer] removeAnimationForKey:@"nameOfMovingAnimation"];
将 View 移动到用户手指的位置
接下来,您应该(连续)在用户移动手指时更新 View 的位置。您不应该为此设置动画,因为它会增加手指移动和 View 之间的时间,并且会使您的应用程序看起来像是滞后的。 在拖动时设置动画不会使其看起来平滑。
注意:因为您的问题没有说明一旦用户拖动图像您想要做什么,或者当他们放下图像时会发生什么。我无法进一步解释。
需要更多帮助?
在网络(或 StackOverflow)中搜索类似“iOS 拖动 View ”的内容,您可能会找到有关如何拖动 imageView 的更多信息。
否则,一旦你走到这一步,就在 StackOverflow 上问另一个问题(即你可以点击移动的图像,它们就会停止移动)。
关于objective-c - 如何触摸和拖动动画图像?这是我的动画代码 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10152625/