0

I've applied this stackoverflow entry so that I can detect the touch as it moves from one view to the other.

The subviews really detect the touch. However, I'm doing something more. I need the view that detected the touch to be removed from it's parent view and add new views to the parent view. So, I have something like this:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.contentView];

    for (UIView *view in self.contentView.subviews)
    {
        if ([view isKindOfClass:[MyCustomView class]] &&
            CGRectContainsPoint(view.frame, touchLocation))
        {
            [view removeFromSuperview];
            UIView *aNewView = [[[UIView alloc] initWithFrame:someFrame] autorelease];
            [self.contentView addSubview:aNewView];
        }
    }
}

After passing through the if-statement, the app fails to respond to the touch move. What do you think is the problem? Was it caused by this: [view removeFromSuperview]?

Community
  • 1
  • 1
yoninja
  • 1,952
  • 2
  • 31
  • 39
  • u want to remove self.contentView ?? – iPatel Mar 19 '13 at 05:48
  • MyCustomView userintraction is enable? – Gaurav Patel Mar 19 '13 at 05:50
  • How many `MyCustomView` views are in your contentView? This method is called on the main thread so doing heavy computations will make the app seem unresponsive until the method terminates. – Nick C Mar 19 '13 at 06:05
  • I think you may remove the parentview. Because of that there are no self.contentView to add your new view. That may the cause for your error. If you don't want to remove this parent view use if-statement to pass it. – Hasintha Janka Mar 19 '13 at 06:06
  • `UITouch *touch = [[event allTouches] anyObject];` should be `UITouch *touch = [touches anyObject];` instead – Nick C Mar 19 '13 at 06:11
  • Is that your actual code? You said "The subviews really detect the touch... So, I have something like this:" – Nick C Mar 19 '13 at 06:16
  • @iPatel, I want to remove the subview. self.contentView is the parent view. – yoninja Mar 19 '13 at 06:34
  • @GauravPatel, userinteraction is enabled. – yoninja Mar 19 '13 at 06:35
  • @yoninja whene you touch view method is call??? – Gaurav Patel Mar 19 '13 at 06:38
  • @NickC, I don't think it's directly related to the performance or the heavy computation. At the moment, I only have 4 objects of MyCustomView. – yoninja Mar 19 '13 at 06:38
  • @Hasintha, the parent view is maintained. Only the subviews are removed. – yoninja Mar 19 '13 at 06:39
  • @NickC, yes, my actual code is somewhat similar to the code posted in the question. I thought the problem is caused by the code inside the if-statement. If I remove these codes and change it with, for example, 'NSLog(@"Test");', the touchesMoved responds perfectly. – yoninja Mar 19 '13 at 06:43
  • @NickC, I have already tried both `UITouch *touch = [[event allTouches] anyObject];` and `UITouch *touch = [touches anyObject];` bu the results are the same. – yoninja Mar 19 '13 at 06:49
  • If the code is in the subview class then if would make sense that the touch would stop because the view handling the touch was remove from the view hierarchy. How can the user continue to "touch" a view that is no longer visible? You should move your logic to the parent view because it will remain in the view hierarchy throughout the touch – Nick C Mar 19 '13 at 06:57
  • @NickC, the code is not in the subview but in the parent view. I followed the answer in the link I have provided above. – yoninja Mar 19 '13 at 07:28

1 Answers1

0

Try this:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.contentView];

    NSArray *subviews = [self.contentView.subviews copy];
    for (UIView *view in subviews)
    {
        if ([view isKindOfClass:[MyCustomView class]] &&
            CGRectContainsPoint(view.frame, touchLocation))
        {
            [view removeFromSuperview];
            UIView *aNewView = [[[UIView alloc] initWithFrame:someFrame] autorelease];
            [self.contentView addSubview:aNewView];
        }
    }
}

I think the problem is you are changing the array while iterating over it.

Nick C
  • 645
  • 3
  • 6