Pregunta

I'm newbie in iOS and i have a problem with setUserInteractionEnabled as: I have a object extend from UIImageView like:

@interface CustomImageView : UIImageView

@property (retain, nonatomic) NSString *sNameImage;
@property (retain, nonatomic) NSString *sID;
@property (assign, nonatomic) float nPointX;
@property (assign, nonatomic) float nPointY;

-(void)setPoint:(NSString *)sPoint;

Then when i add it to UIView like:

for(int i = 0; i < self.arrCustomImageView.count; i++)
{
    CustomImageView *cuiv = [self.arrCustomImageView objectAtIndex:i];
    NSData *data = [self readDataFromFile:cuiv.sNameImage];
    if(data != nil && data.length > 0)
    {
        UIImage *image = [[UIImage alloc] initWithData:data];
        CGRect r = CGRectMake(cuiv.nPointX, cuiv.nPointY, image.size.width, image.size.height);
        [cuiv setUserInteractionEnabled:FALSE]; /*or NO i tried all*/
        [cuiv setImage:image];
        cuiv.frame = r;
        [self addSubview:cuiv];
        [image release];
        image = nil;
    }
}

Then in touchesBegan, i still catch event when touch on it (i still receive log when touch it) :|

Then source touchBegan method:

UITouch * touch = [touches anyObject];
for (UIView *aView in [self subviews])
{
    if (CGRectContainsPoint([aView frame], [touch locationInView:self]))
    {
        self.imvChoice = (CustomImageView *) aView;
        originalPoint = aView.frame.origin;
        offsetPoint = [touch locationInView:aView];
        nIndexChoice = [self.arrCustomImageView indexOfObject:aView];
        NSLog(@"log: %@ >> %@: id: %d", NSStringFromClass([self class]),NSStringFromSelector(_cmd), nIndexChoice);
        [self bringSubviewToFront:aView];
    }
}

I don't think it problem because i have try add same UIImageView like as:

 for(int i = 0; i < [arrObject count]; i++)
{
    CustomObject *nsObj = [arrObject objectAtIndex:i];

    UIImageView *image1 = [self createUIImageView:[nsObj sName1] pointX:nsObj.pPoint1.x pointY:nsObj.pPoint1.y];
    image1.tag = i;
    [image1 setUserInteractionEnabled:FALSE];

    UIImageView *image2 = [self createUIImageView:[nsObj sName2] pointX:nsObj.pPoint2.x pointY:nsObj.pPoint2.y];
    image2.tag = i;
    [image2 setUserInteractionEnabled:TRUE];

    [self addSubview:image1];
    [self addSubview:image2];
    [image1 release];
    [image2 release];
}

it run normal :| i can't catch event when click on image1 and image2 is can.

So i have mistake? Please, explain for me! Thanks for all support!

¿Fue útil?

Solución

The touchesBegan receives touches for the entire viewcontroller, not just a single uiview. if you set userInteractionEnabled to false the view will not be receiving any view specific events like UITouchUpInside.

If you want to check in you touchesBegan method if the user clicked on a "disabled" object you have to get the coordinates where the user touched your ViewController for example like this

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];

and than check if your touchPoint is inside the rectangle of the view object you want to check

if (CGRectContainsPoint(cuiv, touchPoint){
    if (cuiv.userInteractionEnabled) {
     // your element is enabled, do something
    } else {
     // your element is disabled, do something else
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top