繁体   English   中英

当我在旋转uiview中触摸图像时如何检测图像子视图的标签

[英]how to detect the tag ofimage subviews when i touched the image in the rotatory uiview

我想要创建一个转盘,并且想要在单击拨盘中的图像时获取图像的标签。我尝试了很多,但没有得到。请参见下面的代码

- (void) drawWheel {

container = [[UIView alloc] initWithFrame:self.frame];

CGFloat angleSize = 2*M_PI/numberOfSections;

for (int i = 0; i < numberOfSections; i++) {

    UIImageView *im = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"segment.png"]];

    im.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
    im.layer.position = CGPointMake(container.bounds.size.width/2.0-container.frame.origin.x, 
                                    container.bounds.size.height/2.0-container.frame.origin.y); 
    im.transform = CGAffineTransformMakeRotation(angleSize*i);
    im.alpha = minAlphavalue;
    im.tag = i;

    if (i == 0) {
        im.alpha = maxAlphavalue;
    }

    cloveImage = [[UIImageView alloc] initWithFrame:CGRectMake(12, 15, 40, 40)];
    cloveImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon%i.png", i]];
    cloveImage.tag=i;
   [im addSubview:cloveImage];

}

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

//我想在单击imageview时获取cloveImage.tag

}

所以我的问题是,当我触摸旋转轮时,如何检测旋转轮中的图像标签
特殊的图像?

我建议您改为在UIImageView上添加按钮,然后将背景图像添加到uibuttons。就获得标签而言,您应在其目标方法中获得相应的被单击按钮的标签

for (UIImageView *img in self.view.subviews)
    {
        if ([img isKindOfClass:[UIImageView class]])
        {
            if (img.tag==index)
            {
                Your code....
            }
        }

    }

您可以使用上述代码通过使用for循环来获取Perticular图像的标签。

尝试这个:

... ...
cloveImage = [[UIImageView alloc] initWithFrame:CGRectMake(12, 15, 40, 40)];
cloveImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon%i.png", i]];
cloveImage.tag=i;
//   **********add begin**********
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showCloveImageTagOnImageView:)];
[cloveImage addGestureRecognizer:tapGR];
//   **********add end**********
[im addSubview:cloveImage];
... ...



//  copy the selector below and log the cloveImageTag
- (void)showCloveImageTagOnImageView: (UIImageView *)tappedImageView
{
    NSLog(@"tappedImageView.tag: %d", tappedImageView.tag);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM