0

如何确定UIImageView当前在视图中触摸了哪些内容?它们是动态创建的UIImageViews,并且基于标签。我还将使用带有我的对象和数组索引的标签以供参考。我已经读过我需要在UIButton这种情况下使用,但问题UIButton是我不能将它拖到它自己的范围之外。

我需要知道如何通过标签获取特定的动态创建的按钮,以便检测UIImageView要在TouchesMoved.

UIImageViews这是从我的对象创建的代码:

for (ObjectClass *obj in [self myDelegate].theObj)
{
    CGRect rect = CGRectMake(obj._sX + (obj._sW / 2),obj._sY + (obj._sH / 2),10,10);
    handlerImg = [[UIImageView alloc] initWithFrame:rect];
    handlerImg.backgroundColor = [UIColor yellowColor];
    [[self view] addSubview:handlerImg];
    handlerImg.tag = z;
    [handlerImg setUserInteractionEnabled:YES];
    z++;
}

然后我的 touchesMoved 事件代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (drag == YES)
    {
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInView:self.view];
        ObjectClass *obj = [[self myDelegate].theObj objectAtIndex:currHandler]; //Get object based on selected UIImageView

        if ([touch view] == handlerImg) //Identify which UIImageView is selected
        {
            someImage.center=location;
4

1 回答 1

1

试试这样

if ([[touch view] isKindOfClass:[UIImageView class]]) //Identify which UIImageView is selected
        {
            someImage.center=location;
            //[touch view].tag here you'l get tag value.

        }

(或者)

使用手势,在 .h 文件中添加手势委托并将其放在 ein for 循环中。

UITapGestureRecognizer *tapRecognizer;
    tapRecognizer=[[UITapGestureRecognizer alloc] 
                   initWithTarget:self
                   action:@selector(handleTap:)];
    tapRecognizer.numberOfTapsRequired=1;
    tapRecognizer.numberOfTouchesRequired=1;
    [imageview addGestureRecognizer:tapRecognizer];
    tapRecognizer.delegate = self;
    [tapRecognizer release];

并把这个

- (void)handleTap:(UITapGestureRecognizer *)recognizer {

    NSLog(@"KidsViewController ==> handleTap.");

    switch (((UIGestureRecognizer *)recognizer).view.tag)      
    {
         //do whatever you want here
    }

}
于 2013-05-24T09:08:36.950 回答