Search code examples
iphoneuiimageviewuibuttonuitouch

Touch Event on image , achieved by using UIButton but show up delayed compare to UIImageView


Sorry for the messy title, I just don't know how to describe the problem in a delicate way.

I'm writing a album-like app to display a bunch of image in my scrollview and do something when a image is touched.

I followed this question : how can i detect the touch event of an UIImageView and use button with background image to handle touch event.

My original method is using NSOperation to concurrently fetch image from internet and put it io a imageview and add to my scrollview, and the speed is quite ok because each imageview shows right after each NSOperation callback.

Then I change imageview to uibutton, the strange thing is that when a NSOperation callback, that button does not show in my view. They show up at once when all the NSOperation callback is done. That makes the user experience become unacceptable.

This is my NSOperation callback function, it will pass a button that contains the image fetched from internet

- (void)imageLoaded:(UIButton*)button;
{
    NSLog(@"Done");
    [button addTarget:self action:@selector(buttonPressed:) 
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

The buttons will only displa after the last "Done" appear instead of one by one, is that normal? or I messed up something?

==== update ========

I think I'm running the NSOperation on my viewcontroller. I have a imageLoadOperation class, I'll pass my viewcontroller to it

imageLoadOperation *ilo = [[imageLoadOperation alloc] initWithURLString:[NSString stringWithFormat:@"link for the image"];
[ilo setParent:self];
[queue addOperation:ilo];
[ilo release];

And in the main function of imageLoadOperation I'll do

[parentViewController performSelectorOnMainThread:@selector(imageLoaded:) withObject:button waitUntilDone:NO];

Do you mean I need to move these code to my AppDelegate instead of running in my viewcontrollor?


Solution

  • You can use a button of custom type over your image view instead of using button with background image, or you can use touch event on an UIImageView

    • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event