单词从iPhone屏幕顶部掉落 [英] Words falling from the top of screen in iphone

查看:62
本文介绍了单词从iPhone屏幕顶部掉落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个单词查找应用程序.在这里,我想让一些单词从iPhone屏幕的顶部掉下来.

Im developing a word finding app.Here i want some words to fall from the top of screen of iphone.

屏幕底部将出现一条红线.

There will be a red line at the bottom of screen.

用户在到达红线之前必须选择正确的单词.

User have to select the correct word before reaching a red line.

那么我该如何在iphone中实现下降效果(即单词下降效果).

So how can i implement the falling effect in iphone.(i.e words falling effect).

有人可以帮我吗?

谢谢.

推荐答案

您可以使用UIView动画实现此目的.此示例代码全部在视图控制器中,并且在视图中添加了单个标签fallingLabel.您可以扩展它以包含多个标签.

You can implement this using UIView animations. This sample code is all within a view controller, with a single label, fallingLabel added to the view. You can expand it to include multiple labels.

关键点是导入QuartzCore框架.这使您可以访问视图的presentationLayer,该视图保存了动画视图的当前状态.您可以在Xcode的项目摘要部分中将框架添加到您的项目中,然后将以下内容放在视图控制器的实现文件的顶部:

The key point is to import the QuartzCore framework. This gives you access to the presentationLayer of a view, which holds the current state of an animating view. You add the framework to your project in the project summary part of Xcode, then put the following in the top of your view controller's implementation file:

#import <QuartzCore/QuartzCore.h>

以下代码对标签的删除进行了动画处理. UIViewAnimationOptionCurveEaseIn设置会降低它的速度,给您令人信服的重力效果.

The following code animates the dropping of the label. It will increase in speed as it falls due to the UIViewAnimationOptionCurveEaseIn setting, giving you a convincing gravity effect.

self.fallingLabel.center = CGPointMake(160,50);

[UIView animateWithDuration:3
                      delay:0 
                    options:UIViewAnimationOptionCurveEaseIn 
                 animations:^{self.fallingLabel.center = CGPointMake(160,400);}
                 completion:nil];

现在,您需要实现触摸处理.此操作未在标签上完成,因为在内部它会认为它已经在屏幕底部了.您可以在视图控制器中处理触摸操作:

Now, you need to implement touch handling. This is not done on the label, as internally it will think it is already at the bottom of the screen. You handle the touches in your view controller:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    if ([self.fallingLabel.layer.presentationLayer hitTest:touchLocation])
    {
        NSLog(@"Label was touched");
        CALayer *presLayer = self.fallingLabel.layer.presentationLayer;
        [UIView animateWithDuration:0 
                              delay:0 
                            options:UIViewAnimationOptionBeginFromCurrentState 
                         animations:^{self.fallingLabel.frame = presLayer.frame;} 
                         completion:nil];
    }
}

在这里,正在发生的事情是正在查询表示层,以查看触摸是否落在当前范围之内.如果是这样,我们然后从当前状态开始一个新的动画-在这种情况下,我要做的就是停止标签的掉落,但是您可以在此处实现任何您喜欢的东西.

Here, what is happening is that the presentation layer is being queried to see if the touch falls within it's current bounds. If so, we then start a new animation, which starts from the current state - in this case all I have done is stop the falling of the label, but you could implement anything you like here.

如果您有多个下降标签,则可以在每个标签的表示层上尝试使用hitTest,直到找到被触摸的标签.

If you have multiple falling labels you can try the hitTest on the presentation layer of each one until you find the one that has been touched.

这篇关于单词从iPhone屏幕顶部掉落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆