Correct way to implement TouchesMoved?

I have a custom UIView that generates a set of subzones and displays them in rows and columns, such as tiles. What I'm trying to achieve is to let the user touch the screen, and when the finger moves, the tiles under it disappear.

Below is the UIView code that contains the fragments:

- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        int i, j;
        int maxCol = floor(self.frame.size.width/TILE_SPACING);
        int maxRow = floor(self.frame.size.height/TILE_SPACING);

        CGRect frame = CGRectMake(0, 0, TILE_WIDTH, TILE_HEIGHT);
        UIView *tile;       

        for (i = 0; i<maxCol; i++) {
            for (j = 0; j < maxRow; j++) {
                frame.origin.x =  i * (TILE_SPACING) + TILE_PADDING;
                frame.origin.y =  j * (TILE_SPACING) + TILE_PADDING;
                tile = [[UIView alloc] initWithFrame:frame];

                [self addSubview:tile];
                [tile release]; 
            }
        }

    }
    return self;
}


- (void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event {
    UIView *tile = [self hitTest:[[touches anyObject] locationInView:self] withEvent:nil];

    if (tile != self)
        [tile setHidden:YES];
}



- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
    UIView *tile = [self hitTest:[[touches anyObject] locationInView:self] withEvent:nil];

    if (tile != self)
        [tile setHidden:YES];
}

This approach works, but if the tiles become denser (i.e. smaller tiles and more tiles on the screen). The iPhone is less sensitive as it moves with a finger. It could be hitTest, charging for the processor, as it struggles to keep up, but would like some opinions.

My questions:

  • Is this an efficient way / right way to implement touchphonesMoved?

  • If not, what would be the recommended approach?

  • Tile ( UIView), . subview Tile TouchesBegan, TouchesBegan , . subview Tile, TouchesBegan/TouchesMoved ?

+5
3
  • .
  • , , , :

 //In your init method, make sure each tile doesn't respond to clicks on its own
 ...
 tile.userInteractionEnabled = NO;
 ...


 - (void) touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
      CGPoint tappedPt = [[touches anyObject] locationInView: self];
      int     xPos = tappedPt.x / (TILE_SPACING + TILE_PADDING);
      int     yPos = tappedPt.y / (TILE_SPACING + TILE_PADDING);
      int     tilesAcross = (self.bounds.size.width / (TILE_SPACING + TILE_PADDING));
      int     index = xPos + yPos * tilesAcross;

      if (index < self.subviews.count) {
          UIView  *tappedTile = [self.subviews objectAtIndex: index];
          tappedTile.hidden = YES;
      }
  }

>

  1. , , . , , touch-, . , , , ( , ). , , . , ( ), . 15x15 , , , .

( , 1 ...)

+11

, , , CGRect, . , .

for (UIView* aSubview in self.subviews) {

    if([aSubview pointInside: [self convertPoint:touchPoint toView:aSubview] withEvent:event]){

       //Do stuff
    }
}
+5

Just an idea that might help ... I have not tested this, but after detecting a click on a specific view image, disable userInteractionEnabled from all other images ...

I suppose this will help to increase the speed a bit, since the iPhone will not use resources trying to find which image is being used during drag and drop. The same is true for multitouch.

+1
source

All Articles