检测多个 UIImageview 上的单独触摸?
我使用以下代码在我的视图上添加了大约 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 标签确定哪个 imageview 已被触摸,但我不知道如何检查 TouchBegan 方法中的标签..可以做什么我可能会根据触摸来区分 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];`
}
i have added around 15 to 16 UIImageviews on my View using the following code
- (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);
}
}
}
now i know i can determine which imageview has been touched with the help of tag of imageView but i dont know how to check for the tags in touchesBegan method.. what can i possibly do to differentiate uiimageview on basis of touch??
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:imgView];
NSLog(@"touch %@",imgView.tag);
}
new code:
- (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];`
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试这个:
Please try this:
从 iOS 3.2 开始,您可以使用 UIGestureRecognizers。我建议为您拥有的菜单使用一个 UIImageView 并添加 UITapGestureRecognizer 来检测单击。然后在操作中使用 locationInView 来查看用户触摸图像中的位置。
这是非常有用的 Apple 识别器指南。
You can use UIGestureRecognizers as of iOS 3.2. I would recommend using one UIImageView for the menu you have and adding the UITapGestureRecognizer to detect single taps. Then use locationInView in the action to see where in the image the user touched.
This is a link to the very helpful Apple guide to recognizers.