Ios 在屏幕向下移动时,从左向右移动UIView

Ios 在屏幕向下移动时,从左向右移动UIView,ios,uiview,quartz-graphics,Ios,Uiview,Quartz Graphics,我正在尝试创建一个iOS应用程序,其中块在屏幕上浮动(UIView)。我让它们漂浮在屏幕上,但我也希望用户能够在它们下落时在x轴上移动它们。我试着用下面的代码来做,但它们只是掉了下来,没有从左向右移动。我的问题是我试图用手指从左向右移动它,因为它已经在屏幕上移动了。我如何使下面的代码适应工作 注意:我可以将视图从左向右移动,而不必在屏幕上向下移动,也可以在屏幕上向下移动,而不必从左向右移动。当我将两者结合起来时,问题就出现了 Y轴动画 [UIView beginAnimations:nil co

我正在尝试创建一个iOS应用程序,其中块在屏幕上浮动(UIView)。我让它们漂浮在屏幕上,但我也希望用户能够在它们下落时在x轴上移动它们。我试着用下面的代码来做,但它们只是掉了下来,没有从左向右移动。我的问题是我试图用手指从左向右移动它,因为它已经在屏幕上移动了。我如何使下面的代码适应工作


注意:我可以将视图从左向右移动,而不必在屏幕上向下移动,也可以在屏幕上向下移动,而不必从左向右移动。当我将两者结合起来时,问题就出现了

Y轴动画

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:letView.speed];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

letView.layer.frame = CGRectMake(letView.layer.frame.origin.x, [[UIScreen mainScreen] bounds].size.height, letView.layer.frame.size.width, letView.layer.frame.size.height);

[UIView commitAnimations];
触摸动画

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    //Goes through an array of views to see which one to move
    for (LetterView * view in _viewArray) {
        if (CGRectContainsPoint(view.frame, touchLocation)) {
            dragging = YES;
            currentDragView = view;
            [currentDragView.layer removeAllAnimations];
        }
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    if (dragging) {
        CGPoint location = touchLocation;
        currentDragView.center = CGPointMake(location.x, currentDragView.center.y);
    }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    dragging = NO;
    [self checkForCorrectWord];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:currentDragView.speed];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    currentDragView
    .layer.frame = CGRectMake(currentDragView.layer.frame.origin.x, [[UIScreen mainScreen] bounds].size.height, currentDragView.layer.frame.size.width, currentDragView.layer.frame.size.height);

    [UIView commitAnimations];
}

我有一个演示项目,向您展示如何使“船”在屏幕上以s曲线向下移动,如下所示:

我使用的解决方案是制作关键帧动画(实际上,它是构成分组动画一部分的关键帧动画,但您可能不需要分组动画的其余部分:制作弯曲路径形状的是关键帧动画)。也许你可以调整我正在做的事情,根据你自己的目的修改它

代码可在此处下载:

这在我的书中有详细的讨论。关键帧动画通常:

这个特别的例子:


您是否已调试以检查您的“CGRectContainsPoint”是否匹配?_viewArray中的视图都是self.view的直接子类吗?我可以从左到右移动视图而不必在屏幕上向下移动,也可以在屏幕上向下移动视图而不必移动它们。当我将两者结合起来时,问题就出现了。屏幕下的动画是如何工作的?触摸处理代码正在从触摸视图层删除所有动画。你的问题只提供了一半信息的细节。如果可能的话,我不想阻止动画在屏幕上向下移动。接下来,你尝试将它们结合起来了吗?停下来凝视一个新的动画?离开现有动画?还有什么问题吗?我的问题是,当它在屏幕上移动时,我用手指移动它。对不起,如果你用手指移动它,你根本不会使用动画。您只需要让视图跟随手指(对于UIPangestureRecognitor来说很简单)。或者您是说希望视图在设置动画时可以触摸???这是一个非常困难的问题,因为在动画过程中,视图实际上并不位于您看到它的位置。此外,当视图的位置处于动画状态时,视图位置的任何更改都会终止动画。这就是为什么我停止动画并重新启动它,但它不起作用。