0

I am trying to create an iOS app with blocks floating down the screen(UIViews). I have them floating down the screen but I also want the user to be able to move them on the x axis as they are falling. I tried to do it with the code below but they just fall and don't move left to right. My problem is I am trying to move it with my finger left to right as it is already moving town the screen. How can I adapt the code below to work?

Note: I was able to move the views left to right without them moving down the screen and I was able to move them down the screen without moving them left to right. The problem arises when I combine both.

ANIMATION FOR Y AXIS

[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];

ANIMATION FOR TOUCH

- (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];
}
4

1 回答 1

2

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

在此处输入图像描述

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

代码可在此处下载:

https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/ch17p501groupedAnimation

我的书中对此进行了详细讨论。关键帧动画通常:

http://www.aeth.com/iOSBook/ch17.html#_keyframe_animation

这个特殊的例子:

http://www.aeth.com/iOSBook/ch17.html#_grouped_animations

于 2013-04-14T16:37:56.917 回答