2012-11-17 14 views
2

当我的对象在屏幕上移动时,需要处理对象的触摸。当touchesBegan调用时,我需要隐藏我的对象。animateWithDuration和touchesBegan不能在对中工作

这是我的UIViewController代码:

- (void)win { 
    for (NSInteger i = 1; i <= 6; i++) { 
     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"rocket_%d.png", i]]]; 
     imageView.tag = 1000; 
     CGSize size = imageView.image.size; 
     [imageView setFrame:CGRectMake(200 + (i * 60), 500, size.width, size.height)]; 
     [self.view addSubview:imageView]; 
     [rockets addObject:imageView]; 
     [imageView setUserInteractionEnabled:YES]; 
    } 
    [self startRockets]; 
} 

- (void)startRockets { 

    CGFloat timeInterval = 1.0; 
    for (UIImageView *imageView in rockets) { 
     [UIView animateWithDuration:5.0 delay:timeInterval options:UIViewAnimationOptionAllowUserInteraction animations:^{ 
      [imageView setFrame:CGRectMake(imageView.frame.origin.x, 0, imageView.frame.size.width, imageView.frame.size.height)]; 
     } completion:nil]; 
     timeInterval += 1.0; 
    } 


} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 
    NSArray *views = [self.view subviews]; 
    for (UIView *v in views) { 
     if (v.tag == 1000) { 
      if (CGRectContainsPoint(v.frame, touchLocation) == YES) { 
       [v setHidden:YES]; 
      } 
     } 
    } 
} 

回答

9

也许你缺少AllowAnimatedContent。试着改变你的

UIViewAnimationOptionAllowUserInteraction

UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAllowAnimatedContent


...另一种可能是使用UITapGestureRecognizer

- (void)createViews { 
    rockets = [[NSMutableArray alloc] init]; 
    for (NSInteger i = 1; i <= 6; i++) { 
     UIView *view = [UIView new]; 
     view.backgroundColor = [UIColor redColor]; 
     view.frame = CGRectMake((i * 60), 100, 50, 50); 

     UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rocketTouched:)]; 
     [view addGestureRecognizer:tap]; 

     [self.view addSubview:view]; 
     [rockets addObject:view]; 
    } 
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(moveViews) userInfo:nil repeats:YES]; 
} 
- (void)moveViews { 
    for (UIView * view in rockets) { 
     [UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionAllowAnimatedContent animations:^{ 
      view.frame = CGRectMake(view.frame.origin.x -10, 0, view.frame.size.width, view.frame.size.height); 
     } completion:nil]; 
    } 
} 
- (void)rocketTouched:(UITapGestureRecognizer*)tap { 
    tap.view.hidden = YES; 
} 
+0

我已经添加了这个,但它不能帮助我。我认为问题在线程中。因为动画的方法抓住我的对象,我不能在主线程中处理它 –

+0

我实际上会推荐使用'UITapGestureRecognizer'来做这件事。 – nooitaf

0

你需要,如果你还检查了这需要检测与对象的交集。

我已编辑touchesBegan方法,它的工作原理。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 
    for (UIImageView *imageView in rockets) { 
     CALayer *bbl1ObjPresentationLayer = (CALayer*)[imageView.layer presentationLayer]; 
     if(CGRectIntersectsRect(bbl1ObjPresentationLayer.frame, CGRectMake(touchLocation.x, touchLocation.y, 1, 1))) { 
      NSLog(@"intersect"); 
       //They are intersecting 
      } 
} 
} 

如果你这样做使用CALayer insted UIImageView对象这工作正确。检查这个答案link

相关问题