开发者

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

开发者 https://www.devze.com 2023-02-21 18:52 出处:网络
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 i

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 N开发者_开发技巧SOperation 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?


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


What if instead of converting to a button you just allowed your UIImageView to handle the touch event?

To add a touch event to a UIImageView use the following format in your .m

UITapGestureRecognizer *newTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(newTapMethod)];

[_imageButtonName setUserInteractionEnabled:YES];

[_imageButtonName addGestureRecognizer:newTap];

then create the newTapMethod (or what ever you name it)

-(void)newTapMethod{
//...
}

hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消