я добавил около 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];`
}