动画 UIImageView 应该识别其上的点击(在 UIImageView 所在的每个位置)

发布于 2024-10-18 13:54:55 字数 928 浏览 6 评论 0原文

我正在制作一个 iPhone 应用程序,其中涉及从屏幕底部生成并向上移动到屏幕顶部的气球(UIImageViews)。

目的是设置 UIImageViews,以便在气球上点击手势会导致气球从屏幕上移除。

我已经设置了 GestureRecognizer 并且正在识别点击。

我遇到的问题是,我正在使用的动画方法不提供有关气球在特定时间点绘制位置的信息,并将其视为已经到达提到的最终点。

这会导致仅在屏幕顶部(这是气球的最终目的地)识别点击的问题。

我在这里粘贴了我的动画代码:

UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionAllowUserInteraction;
[UIView animateWithDuration:2.0 delay:0.0 options:options animations:^{ 
balloonImageView.center = p;
} completion:^(BOOL finished) {
if (finished) {
  balloonImageView.hidden= TRUE;
  balloonImageView.image=nil;
}

我通过给 UIImageView 提供一个唯一的标签并检查是否gesture.view.tag = anyBalloonObject.tag来跟踪图像的位置,如果是的话我销毁气球。

我的基本问题是,还有其他我可以使用的动画工具吗?我知道我可以将气球的终点设置为略高于屏幕上的位置,直到它到达顶部,但这似乎非常消耗内存。 您有什么建议吗?我浏览了核心动画参考,没有找到任何适合我需要的方法。

此外,UIViewAnimation 的多线程方面会影响点击系统的准确性吗?

谢谢你,

维克

I'm making an iPhone App which involves balloons (UIImageViews) being spawned from the bottom of the screen and moving up to the top of the screen.

The aim is to have the UIImageViews set up so that a tap gesture on a balloon causes the balloons to be removed from the screen.

I have set up the GestureRecognizer and taps are being recognized.

The problem I am having is that the animation method I am using does not provide information about where the balloon is being drawn at a specific point in time, and treats it as already having reached the final point mentioned.

This causes the problem of the taps being recognized only at the top of the screen(which is the balloon's final destination).

I have pasted my code for animation here:

UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionAllowUserInteraction;
[UIView animateWithDuration:2.0 delay:0.0 options:options animations:^{ 
balloonImageView.center = p;
} completion:^(BOOL finished) {
if (finished) {
  balloonImageView.hidden= TRUE;
  balloonImageView.image=nil;
}

I keep track of where an image is by giving a unique tag to a UIImageView and checking if gesture.view.tag = anyBalloonObject.tag and if it does I destroy the balloon.

My basic question is, is there another animation tool I can use? I understand that I can set a balloon's finish point to slightly higher than where it is on screen until it reaches the top but that seems like it is very taxing on memory.
Do you have any recommendations? I looked through the Core Animation reference, and didn't find any methods that suited my needs.

In addition will the Multi-threading aspects of UIViewAnimation affect the accuracy of the tap system?

Thank You,

Vik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-10-25 13:54:55

您不应该像这样为游戏对象(在移动时与之交互的对象)设置动画。
您应该使用 NSTimer 来移动气球。

-(void)viewDidLoad {

//Set up a timer to run the update-function
timer =  [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:NULL repeats:YES];

}


-(void) update {

for (int i = [balloonArray count] - 1; i >= 0; i--) { //Loop through an array containing all your balloons.


    UIImageView *aBalloon = [balloonArray objectAtIndex:i]; //Select balloon at the current loop index

    aBallon.center = CGPointMake(aBallon.center.x, aBalloon.center.y + 1); //Change the center of that balloon, in this case: make it go upwards.



}
}

以下是您需要在 .h 中声明的内容:

NSTimer *timer;
NSMutableArray *ballonArray;
//You should use properties on these also..

您必须创建一个生成方法,将气球添加到屏幕和数组中。
完成此操作后,实现触摸方法就很简单了,您只需循环气球并检查:

  //You need an excact copy of the loop from earlier around this if statement!
  If (CGRectContainsPoint(aBalloon.frame, touchlocation)) { //you can do your own tap detection here inthe if-statement
   //pop the balloon and remove it from the array!
    }

希望这看起来不太吓人,祝您好运。

You shouldnt animate game-objects (objects you interact with while they are moving) like that.
You should rather use a NSTimer to move the ballons.

-(void)viewDidLoad {

//Set up a timer to run the update-function
timer =  [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:NULL repeats:YES];

}


-(void) update {

for (int i = [balloonArray count] - 1; i >= 0; i--) { //Loop through an array containing all your balloons.


    UIImageView *aBalloon = [balloonArray objectAtIndex:i]; //Select balloon at the current loop index

    aBallon.center = CGPointMake(aBallon.center.x, aBalloon.center.y + 1); //Change the center of that balloon, in this case: make it go upwards.



}
}

Here is what you need to declare in .h:

NSTimer *timer;
NSMutableArray *ballonArray;
//You should use properties on these also..

You will have to create a spawn method which adds balloons to the screen AND to the array.
When you have done that, it's simple to implement the touch methods, You can just loop trough the balloons and check:

  //You need an excact copy of the loop from earlier around this if statement!
  If (CGRectContainsPoint(aBalloon.frame, touchlocation)) { //you can do your own tap detection here inthe if-statement
   //pop the balloon and remove it from the array!
    }

Hope that isnt too scary-looking, good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文