I would like to be able to add a new UIImageView of a certain size which should have a few UIGestureRecognizers detecting touch on the UIImageView.
How does one subclass UIImageView or how would this be best achieved?
I want the user to be able to add UIImageViews as they want at runtime and then the gestureRecognizer will allow them to manipulate the views.
Thanks
I think you just forgot to enable userInteractions.
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"foo.png"]] autorelease];
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[self.view addSubview:imageView];
Related
For a Universal App, how do I set the UIImageView frame in the ViewController?
UIView *baseView = [[UIView alloc] init];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
[baseView addSubview:myImage];
When I run app it shows black screen. So i guess I have to set a frame for it.
You kinda answered your own question - yes you have to set a frame for it.
You may want to try:
UIView *baseView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
myImage.frame = baseView.bounds;
[baseView addSubview:myImage];
You could also set the autoresizingmask for baseView and myImage so that as self.view's frame changes, so do these two views.
Also, just in case you aren't using ARC - don't forget to autorelease these views before leaving this method.
I want to add an event to an image. but if i add this means it will gives the warning: UIImage maynot respond to addtarget action for control events. how can i correct this code.
this is my code
UIImageView *image;
UIImage *image1=[UIImage imageNamed:#"left_arrow.png"];
image=[[UIImageView alloc]initWithImage:image1];
image.frame=CGRectMake(100,410,30,30);
[image1 addTarget:self action:#selector(buttonPressed2:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:image];
If you don't want to use an extra button you can add a UITapGestureRecognizer to the imageView.
Like this:
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(buttonPressed2:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[imageView setUserInteractionEnabled:YES];
but this is more like a UIControlEventTouchDown event
You need to create a transparent custom UIButton on top of the UIImageView. The button will handle events. UIImageView can do that.
UIButton *btn = [[[UIButton alloc] initWithFrame:imageView.frame] autorelease];
btn.buttonType=UIButtonTypeCustom;
[btn addTarget:...];
I have a UIScrollView with multiple UIImageViews in it created like this.
frame = [[UIImageView alloc] initWithImage:bg];
frame.frame = CGRectMake(FRAME_SEPARATOR + numPage*1024 + numColumn*(FRAME_SEPARATOR+230), 10 +numRow*(FRAME_SEPARATOR+145), 230, 145);
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[frame addGestureRecognizer:tap];
[tap release];
[scroll addSubView:frame];
The problem is that imageTapped is not being called when tapping over an image.
If I add the gesture recognizer to the scrollview like this:
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[scroll addGestureRecognizer:tap];
[tap release];
imageTapped is called.
How can I detect the taps over the UIImageViews?
Thanks
Make sure userInteractionEnabled is set to YES on the UIImageView:
frame.userInteractionEnabled = YES;
I'd also recommend using a different name for the UIImageView variable (eg. imageView instead of frame). Otherwise, you can easily confuse it with the view's frame property.
Has anyone managed to get a UIGestureRecognizer to work on a UIView that is a subview of a UIScrollView? My callbacks never seems to get called.
As a simple example, I want to have a paging scrollview and on the third page listen for a tap with a UITapGestureRecognizer. However I can not get it to work.
Here's how I would do it:
self.scrollView = [[[UIScrollView alloc] initWithFrame:self.view.frame] autorelease];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(self.section1ScrollView.frame.size.width * 3, self.scrollView.frame.size.height); //3 pages
UIImageView *p0 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page0.png"]] autorelease];
[self.scrollView insertSubview:p0 atIndex:self.scrollView.subviews.count];
UIImageView *p1 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page1.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p1 atIndex:self.scrollView.subviews.count];
UIImageView *p2 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page2.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p2 atIndex:self.scrollView.subviews.count];
UITapGestureRecognizer *p2TapRegocnizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(p2Tapped:)] autorelease];
//p2TapRegocnizer.delegate = self;
[p2 addGestureRecognizer:p2TapRegocnizer];
UIImageView has in default userInteractionEnabled set to NO. I would try to change it to YES.
[webView setUserInteractionEnabled:YES]
[((UIImageView *)cell.backgroundView) removeFromSuperview];
it removes cell.backgroundView from the UITableviewCell, but how can I bring it back again ..?
(add again that view?)
[cell addSubview:myBackgroundView]
Where the myBackgroundView is a UIImageView. If you keep myBackgroundView as an instance variable, you could simply add it again. If you do not, you need to reinitialize the backgroundView;
UIImageView *myBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"your image"]] autorelease];
Something like that.
You will need to keep a reference to the superview, and use addSubview:
UIView *imageView = (UIView *)cell.backgroundView;
UIView *imageSuperview = imageView.superView; // I assume it's cell, but just in case
// Remove imageView
[imageView removeFromSuperview];
// Add it again
[imageSuperview addSubview:imageView];
If you store the imageView in fields and such, please ensure that you retain your references properly.