我正在创建一个通过计时器的UIImageView,它在touchesended中调用并移动它。完成动画后,我将删除它的对象。如果我连续触摸屏幕6到7次,则其中一个图像(从定时器代码块创建)不会移动,因此不会取消分配..
这是我的代码:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
flakeImage = [UIImage imageNamed:@"bubble.png"];
TimerView1=[NSTimer scheduledTimerWithTimeInterval:(0.1) target:self selector:@selector(onTimer:) userInfo:[NSValue valueWithCGPoint:location] repeats:NO];
}
- (void)onTimer:(id) sender {
NSValue *value=[TimerView1 userInfo];
CGPoint location = [value CGPointValue];
int time= [secLabel.text intValue];
int tick = 0;
tick = time+1;
secLabel.text = [NSString stringWithFormat:@"%.2i", tick];
double scale = 1 / round(random() % 100) + 1.0;
double speed = 1 / round(random() % 100) + 1.0;
UIImageView* flakeView1 = [[UIImageView alloc]initWithImage:flakeImage];
flakeView1.tag=100;
int endX = round(random() % 320);
int endY = round(random() % 480);
flakeView1.frame = CGRectMake(location.x, location.y, 35.0 * scale, 35.0 * scale);
[self.view addSubview:flakeView1];
[UIView beginAnimations:nil context:flakeView1];
[UIView setAnimationDuration:6 * speed];
flakeView1.frame = CGRectMake(endX,endY, 5.0 * scale, 5.0 * scale);
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
if(tick == 1)
{
[TimerView1 invalidate];
secLabel.text=[NSString stringWithFormat:@"0"];
}
}
- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
UIImageView *flakeView1 = context;
[flakeView1 removeFromSuperview];
[flakeView1 release];
flakeView1=nil;
}
我不知道为什么会发生这种情况。有没有办法检测图像是否在移动? 我看过很多帖子,他们建议使用animationKeys.How用它来检测图像是否是动画?