要添加到其他答案,请按照以下方式对其进行动画处理:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Touches began!");
UITouch *touch= [[event allTouches] anyObject];
CGPoint point= [touch locationInView:touch.view];
UIImage *image = [UIImage imageNamed:@"BluePin.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame: CGRectMake(point.x-(imageView.bounds.size.width/2), point.y-(imageView.bounds.size.width/2), imageView.bounds.size.width, imageView.bounds.size.height)];
[self addSubview: imageView];
//self.currentPins += 1;
[UIView animateWithDuration:2.0 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
[imageView setAlpha:0.0];
} completion:^(BOOL finished) {
[imageView removeFromSuperview];
//self.currentPins -= 1;
}];
// for(;self.currentPins > 10; currentPins -= 1){
// [[[self subviews] objectAtIndex:0] removeFromSuperview];
// }
}
注释掉的代码是我写的一些额外的代码,用于将屏幕上的引脚数量一次限制为 10 个,假设您有一个名为currentPins的@property 。经过测试,它可以工作,假设我在复制、粘贴和注释掉几行后没有搞砸任何事情。
编辑:忽略注释掉的代码。我实际上混合了两个版本(一个没有动画,一个有)所以它坏了。