Обнаруживать отдельные прикосновения к множественным UIImageviews? - PullRequest
1 голос
/ 11 мая 2011

я добавил около 15-16 UIImageviews в мой вид, используя следующий код

- (void) setUpCellsUsingImage: (UIImage *) masterImage
{
  rows = 4;
 cols = 4;
 containerCellHeight=hight/4;
containerCellWidth=width/4; 


NSInteger row, col;
CGImageRef tempSubImage;
CGRect tempRect;
CGFloat yPos, xPos; 

UIImage * aUIImage;
UIImageView *label;

cellArray = [[NSMutableArray new] autorelease];
int i =0;

for (row=0; row < rows; row++) {
    yPos = row * containerCellHeight;
    for (col=0; col < cols; col++) {
        xPos = col * containerCellWidth;
        label = [[UIImageView alloc]init];
        tempRect = CGRectMake(xPos, yPos, containerCellWidth, containerCellHeight);     

        tempSubImage = CGImageCreateWithImageInRect(masterImage.CGImage, tempRect);

        aUIImage = [UIImage imageWithCGImage: tempSubImage];

        imgView = [[UIImageView alloc] initWithImage:aUIImage];

        imgView.tag =i;

        i++;

        NSLog(@"original tags = %d",label.tag);

        [cellArray addObject: aUIImage];        

        aUIImage = nil;
        CGImageRelease(tempSubImage);

    }
}
}

теперь я знаю, что могу определить, какое изображение было затронуто с помощью тега imageView, но я не знаю, как проверить теги в методе touchesBegan ... что я могу сделать, чтобы дифференцировать uiimageview на основе касания ??

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:imgView];    

NSLog(@"touch  %@",imgView.tag);

}

новый код:

- (void) setUpCellsUsingImage: (UIImage *) masterImage
{

  rows = 4;
 cols = 4;
 containerCellHeight=hight/4;
containerCellWidth=width/4; 

NSInteger row, col;
CGImageRef tempSubImage;
CGRect tempRect;
CGFloat yPos, xPos; 

UIImage * aUIImage;
UIImageView *label;

cellArray = [[NSMutableArray new] autorelease];
int i =0;

for (row=0; row < rows; row++) {
    yPos = row * containerCellHeight;
    for (col=0; col < cols; col++) {
        xPos = col * containerCellWidth;
        label = [[UIImageView alloc]init];
        tempRect = CGRectMake(xPos, yPos, containerCellWidth, containerCellHeight);     

        tempSubImage = CGImageCreateWithImageInRect(masterImage.CGImage, tempRect);

        aUIImage = [UIImage imageWithCGImage: tempSubImage];

        imgView = [[UIImageView alloc] initWithImage:aUIImage];

        [self.view addSubview:imgView]; // i add the uiimageview here

        imgView =CGRectMake(xPos, yPos, containerCellWidth-1, containerCellHeight-1);

        imgView.tag =i;

        i++;

        NSLog(@"original tags = %d",label.tag);

        [cellArray addObject: aUIImage];        

        aUIImage = nil;
        CGImageRelease(tempSubImage);

    }
}
}



- (void)viewDidLoad {

    UIImage *mImage = [UIImage imageNamed:@"menu.png"];
     hight = mImage.size.height;
     width = mImage.size.width;

    [self setUpCellsUsingImage:mImage];

[`super viewDidLoad];`

}

Ответы [ 2 ]

1 голос
/ 15 ноября 2011

Вы можете использовать UIGestureRecognizer начиная с iOS 3.2.Я бы порекомендовал использовать один UIImageView для вашего меню и добавить UITapGestureRecognizer для обнаружения одиночных нажатий.Затем используйте locationInView в действии, чтобы увидеть, где на изображении пользователь коснулся.

    ...
    UITapGestureRecognizer *tapRecognizer = 
        [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuSelect:)];
    [imageView addGestureRecognizer:tapRecognizer];
    [tapRecognizer release]; 
}

- (void)menuSelect:(UIGestureRecognizer *)gesture {
    // get location CGPoint for touch from recognizer
}

Это ссылка на очень полезное руководство Apple по распознавателям .

1 голос
/ 11 мая 2011

Пожалуйста, попробуйте это:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:imgView];    

    UIView *hitView = [self.view hitTest:location withEvent:event];
    NSLog(@"hitView %@",hitView);

    UIImageView *hitImageView = nil;

    if([hitView isKindOfClass:[UIImageView class]]) {
        hitImageView = (UIImageView *)hitImageView;
    } 

    NSLog(@"touched %@", hitImageView);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...