我正在制作一个iPhone应用程序,其中涉及气球(UIImageViews)从屏幕底部产生并向上移动到屏幕顶部。
其目的是设置UIImageViews,以便气球上的轻拍手势会导致气球从屏幕上移除。
我已经设置了GestureRecognizer,并且taps正在被识别。
我遇到的问题是,我使用的动画方法没有提供有关在特定时间点绘制气球的信息,并将其视为已经到达了所提到的最后一点。
这会导致仅在屏幕顶部(气球的最终目的地)识别点击的问题。
我已经在这里粘贴了我的动画代码:
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
发布于 2011-04-09 06:12:10
你不应该那样动画游戏对象(当他们移动时你与之交互的对象)。您应该使用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..您将不得不创建一个将气球添加到屏幕和数组中的spawn方法。当你这样做的时候,实现触摸方法就很简单了,你只需要在气球中循环并检查:
//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!
}希望这不是太可怕的样子,祝你好运。
https://stackoverflow.com/questions/5069912
复制相似问题