简体   繁体   English

当UIView向下移动屏幕时,将UIView从左向右移动

[英]Move UIView left to right while they move down the screen

I am trying to create an iOS app with blocks floating down the screen(UIViews). 我正在尝试创建一个iOS应用程序,其中的块在屏幕上浮动(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. 我让它们漂浮在屏幕上,但我也希望用户能够在x轴下移动它们。 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 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];

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

I've got a demo project that shows you how to make a "boat" animate down the screen in an s-curve, like this: 我有一个演示项目,向您展示如何使用“S”曲线在屏幕上制作“船”动画,如下所示:

在此输入图像描述

The solution I use is to make a keyframe animation (actually it's a keyframe animation that forms part of a grouped animation, but you might not need the rest of the grouped animation: it is the keyframe animation that makes the curved path shape). 我使用的解决方案是制作关键帧动画 (实际上它是构成分组动画的一部分的关键帧动画,但您可能不需要分组动画的其余部分:它是使曲线路径形状的关键帧动画)。 Perhaps you could adapt what I'm doing, modifying it to your own purposes? 也许你可以调整我正在做的事情,根据你自己的目的修改它?

The code is available for download here: 该代码可在此处下载:

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

It is discussed in detail in my book. 我的书中详细讨论了它。 Keyframe animations generally: 关键帧动画一般:

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

This particular example: 这个特例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM