检测选定的UIImageView

时间:2013-05-24 08:59:48

标签: ios xcode uiimageview uitouch

如何识别视图中当前触及的UIImageViewUIImageViews是动态创建的,基于标记。我还将使用带有我的对象和数组索引的标签作为参考。我已经读过我需要在这种情况下使用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;

1 个答案:

答案 0 :(得分:1)

试试这个

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

        }

(OR)

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

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
    }

}