首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >应该能识别点击的动画UIImageView (在UIImageView所在的每个位置)

问应该能识别点击的动画UIImageView (在UIImageView所在的每个位置)
EN

Stack Overflow用户
提问于 2011-02-22 02:44:48
回答 1查看 692关注 0票数 1

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

其目的是设置UIImageViews,以便气球上的轻拍手势会导致气球从屏幕上移除。

我已经设置了GestureRecognizer,并且taps正在被识别。

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

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

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

代码语言:javascript
复制
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,如果是,我就销毁气球。

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

此外,UIViewAnimation的多线程特性是否会影响tap系统的准确性?

谢谢,

Vik

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-09 06:12:10

你不应该那样动画游戏对象(当他们移动时你与之交互的对象)。您应该使用NSTimer来移动气球。

代码语言:javascript
复制
-(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中声明的内容:

代码语言:javascript
复制
NSTimer *timer;
NSMutableArray *ballonArray;
//You should use properties on these also..

您将不得不创建一个将气球添加到屏幕和数组中的spawn方法。当你这样做的时候,实现触摸方法就很简单了,你只需要在气球中循环并检查:

代码语言:javascript
复制
  //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!
    }

希望这不是太可怕的样子,祝你好运。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5069912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档