I have view hierarchy as follows
MyViewController --> ScrollView(added via IB) --> ImageView (+ zooming there) ---> button added on image View(added via code)
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"map.png"]];
[scroll addSubview:imageView];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(660, 120, 40, 40);
[myButton setTitle:#"10" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(asiaPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButton setEnabled:YES];
[imageView addSubview:myButton];
later on in view controller, i defined selector as
-(void) asiaPressed:(id)sender{
NSLog(#"asiaPressed clicked");
}
But it is never going inside selector..
Pls help...i guess it is very basic error..
Thanks in advance
UIImageView has userInteractionEnabled property set to NO by default so it (and its subviews) does not receive touch events. You need to set that property to YES manually:
...
imageView.userInteractionEnabled = YES;
...
P.S. You also mention gesture recognizer in question title, however have not posted any code with it - so if it present it may also cause the problems, but the problem is most likely is what I described above
Related
I have a UIButton on which I attached an UIControlEventTouchUpInside action. This button is inserted in a UIScrollView.
My problem is: touch events won't work until I hold on the button for a certain time. If I tap on it directly, or just click on it in the simulator, it won't work.
The thing is: when I just click on it, pointInside:withEvent: is actually triggered and returns YES. What could possibly prevent the event from being fired?
Try setting the delaysContentTouches property of your scrollView to NO.
It should work so you can check the following eg.:
1) make sure that you button is properly added to scrollview. For example like this:
...
myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
myScrollView.bounces = NO;
[myScrollView setScrollEnabled:YES];
myScrollView.showsHorizontalScrollIndicator = NO;
myScrollView.delegate = self;
myScrollView.pagingEnabled = YES;
[self.view addSubview:myScrollView];
myScrollView.contentSize = CGSizeMake(320*5, 480);
...
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
mybutton.frame = CGRectMake(0,0,100,100);
mybutton.tag = 1
[mybutton addTarget:self action:#selector(pressedmybutton:)
forControlEvents:UIControlEventTouchUpInside];
[myScrollView addSubview:bu];
2) make sure that button is top of the view stack. For testing purpose add at the end of the view init code [mybutton bringSubviewToFront];
3) test it on device and not just in Simulator. I experienced different scrolling/button pressing behavior in Simulator than on real device (which is the better for testing this out).
In Cocos2d, I have read that
[[[[CCDirector sharedDirector] openGLView] window] addSubview:someButton];
will add a UIView to the main window: http://www.cocos2d-iphone.org/forum/topic/3588
Yet in my code I have this:
- (void)onEnterTransitionDidFinish {
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[[[[CCDirector sharedDirector] openGLView] window] addSubview:button];
}
But no button is visible. This method is part of a CCLayerColor which is the first scene presented in the app, if that matters. What am I doing wrong here? Thanks!
Edit: I can confirm that the buttons are being added to the windows subviews, because NSLogging
[[[[[CCDirector sharedDirector] openGLView] window] subviews] count]
shows a difference when I comment/uncomment the addSubview line. So why isn't the button showing up?
Edit 2:
Thanks to #Marine, I have found that my problem is the way I am declaring my button; using buttonWithType: solved the problem, because this code works:
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(100, 100, 100, 100)];
[[[[CCDirector sharedDirector] openGLView] window] addSubview:button];
Can someone explain to me why using this class method works and using initWithFrame: doesn't? Because I would rather not create lots of autoreleased objects, and also because I have some UIButton subclasses that do NOT show up when I use the buttonWithType: method.
Edit 3 (Solution):
The reason why using anything but buttonWithType: doesn't work is that if you do not use that method to create the button, the background color of the button is not set, and by default it is clearColor. Using [button setBackgroundColor:[UIColor redColor]] or any other color fixes the problem. Also, if using a custom button class like this one, you might need to manually call [button awakeFromNib] if the button isn't connected to a nib.
Try the below code it would help you
-(id) init
{
if((self = [super init]))
{
UIView* glView = (UIView*) [[CCDirector sharedDirector] openGLView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(100, 100, 250, 250)];
[btn addTarget:self action:#selector(actionper:) forControlEvents:UIControlEventTouchUpInside];
[glView addSubview:btn];
}
return self;
}
-(void) actionper : (id) sender
{
printf("\n Stuff of the Code on button Press");
}
I have posted the solution to the problem as an edit to the question.
Try using the bringsubviewtofront method of the OpenGL window passing the recently added button as the argument. Also make sure that the frame of your button is such that it is in the visible part of the screen. Try playing around with it's position.
I am using PhotoScroller project and want to create buttons over imageViews programmatically. ImageView is a subView of ScrollerView. My code snippet is shown below:
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
//imageView.tag = i; // tag our images for later use when we place them in serial form
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[self addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:#selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[self addSubview:btn]; //Buttons are clickable but shows button on all images**
//[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images
I have two choices rite now. Whether i make my button a subview of ImageView or ScrollView which is parent of ImageView. If i make button subview of Scrollview like [self addSubView:btn]; it displays at right position and is clickable but problem is that it is created on all images in the queue because i am making it a subview of parent view i.e scrollview. Else if i make it subview of child view i.e ImageView which is unique for every image, but still its shown on all images and its not clickable :/
Can any1 guide me on how to make it clickable and keeping the linkage between dynamic button and the image unique so that i have different buttons at various positions on each image in the queue.
Thanks in advance.
Regards,
wahib
You have to create an extra UIView for containing both UIImageView and UIButton
UIView* imageContainer = [[UIView alloc] initWithFrame:CGRectMake(0,0,1000,1000)] autorelease];
UIImageView* imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[imageContainer addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:#selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[imageContainer addSubview:btn];
[self addSubview:imageContainer];
Caution with memory leak. You have lot of unused retain in your code.
So the code I have is as follows:
// Create a containing view to position the button
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 23.5, 21.5)] autorelease];
// Create a custom button with the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Settings.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(-19, -3, 21.5, 21.5)];
[containingView addSubview:button];
// Create a container bar button
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
self.navigationItem.rightBarButtonItem = containingBarButton;
The issue is that my Settings.png original image size is 21.5, 21.5 and there fore the button is super hard to click. I have to tap really hard in order to trigger the UITouchUpInside to be triggered. This will clearly be rejected if I put it in the app store, as it is not in compliance with apple's HIG. Is there a way around this? I still need to use UIView as the container for the UIButton as I'd like to position it correctly on the UINavigationBar
Try to set a bigger fram to the button like 40,40 and set the content mode to be the center so the image will apear to be in the center.
button.contentMode = UIViewContentModeCenter;
Tell me if that helped
I'm trying to use dynamic buttons created via code (no IB) in my project and they appear where and how I want them but they don't fire their actions.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(475, 302, 49, 58);
[button1 setTitle:#"1"
forState:(UIControlState)UIControlStateNormal];
[button1 addTarget:self
action:#selector(goToFirstTrailer)
forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[myImageView addSubview:button1];
-(void)goToFirstTrailer {
[self startAnimator:#"OutsideToTrailer1_" forNumFrames:60];
}
The imageView this is placed on has buttons and User Interaction Enabled On.
Any light you can shed would be much appreciated.
I think you have the wrong signature of the action method change that line to
-(void) goToFirstTrailer:(id)sender {
and where you set the action to
[button1 addTarget:self action:#selector(goToFirstTrailer:) forControlEvents:....
Important is the colon after the message name because I changed the action method signature to include the sender.
Edit I wrote a small sample project with just an imageView in the MainWindow.xib and created a button programmatically as follows
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(0.f, 0.f, 50.f, 50.f);
[button1 setTitle:#"1"
forState:(UIControlState)UIControlStateNormal];
[button1 addTarget:self
action:#selector(goToFirstTrailer:)
forControlEvents:(UIControlEvents)UIControlEventTouchDown];
imageView.userInteractionEnabled = YES; // <--- this has to be set to YES
[imageView addSubview:button1];
[self.window makeKeyAndVisible];
return YES;
}
It is quick and dirty and yes, I am misusing the application delegate as the view controller. I know it is bad.
Here is the action method
- (void) goToFirstTrailer:(id)sender {
imageView.backgroundColor = [UIColor redColor];
}
Setting the userInteractionEnabled property on the parent imageView makes the difference. With it set to NO which is the default, no events are routed to the button.
If myImageView is a UIImageView object, adding a subview to it (like UIButton) only displays the image that object is represented by. If you add that UIButton as a subview of a UIView, it should work perfectly. Try adding an auxiliary UIView to your UIImageView and then add your button to a subview of the new auxiliary UIView, that should solve your problem.
This works fine for me. Note that I changed the event to UIControlEventTouchUpInside to allow the button press down state to be visible first.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(20,400,88,35); //The position and size of the button (x,y,width,height)
[btn setTitle:#"Quit" forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(showAbout)
forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
[self.view addSubview:btn];