I have a UILabel and I would like to make it react to a touch. I tried putting a button on top of the label, thanks to this I could interact with the button. However, the button cannot be fully transparent, right? I could set an alpha of the button to 0,02, but it is still visible on by background. How to solve this? Maybe I could set the properties in some other way to make them fully invisible? Or is there some other solution?
First, why not just use a button and set the button title to the label's contents?
If you can't/don't want to do that, you can also set userInteractionEnabled = YES on the label and then add a gesture recognizer to the label.
In Swift:
label.userInteractionEnabled = true
let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed"))
label.addGestureRecognizer(gestureRecognizer)
Get your click in Action :
func labelPressed(){
print("Label pressed")
//Your awesome code.
}
I usually do this:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(pushAction)];
[myLabel addGestureRecognizer:tap];
I don't know if it works with labels, but then i just make a transparent UIView with the same rect and put it on top.
Okay, i checked, it only works in UIView, but then, do this:
UIView *tapView = [[UIView alloc] initWithFrame:myButton.frame];
And put "tapView" in the addGestureRecognizer-method.
Related
I am developing an application in which I have a horizontal scroller.
The scroller area comprise of different colors [which are nothing but UIImageView of colors].
Above the scroller area is a canvas [which is also a UIImageView].
Now, what I want is that when I select any color in the scroller area, it should set on the canvas.
It is pretty much like a color picker but I am already providing solid color options to be selected by the user.
How should I go about it ? Any sample Code to get me started or my mind kicking would be awesome ?
Thanx a ton
To implement it easily.
Use UIView not UIImageView
Set UIView backgroundColor and place it on the scroll
Set UITapGestureRecognizer on views,Refer here
when a purticular view is touched you have its instance of view which
is touched
get the background color of the view and set it in the scroller
Can you take UIButtons instead of UIImageViews inside your scrollView to show colours to be picked.
This will make the implementation easier. User will select any colour button and you will receive a callback in buttons action selector method. Then based on button tag or any property you can set the colour to your canvas.
In current implementation it will be tricky as you need to know the exact content offset where the tap is done to make out which UIImageView was pressed.
coding steps:
In your scroll view add UIButtons instead of UIImageView like:
UIButton* button = [[UIButton alloc] initWithFrame:someRect];
//put some tag to button
button.tag = someInt;
//add selector to each button to get callback
[view addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
In the btnSelected method put this code:
-(IBAction)btnSelected:(id)sender;
{
UIButton* button = (UIButton*) sender;
if (button.tag == someInt) {
//do something like
canvas.backgroundColor = button.backgroundColor;
}
}
Instead of using UIImageViews or UIViews as suggested, I would put custom UIButtons for picking colors.
UIButton * colorButton;
//You will probably do these inside a for loop
colorButton = [UIButton buttonWithType:UIButtonTypeCustom];
[colorButton setFrame:CGRectMake(X, Y, buttonSize, buttonSize)];
[colorButton setTitle:#"" forState:UIControlStateNormal];
[colorButton setBackgroundColor:SOME_COLOR];
//Border visualization would be good if you are putting buttons side-by-side
[colorButton.layer setBorderWidth:1.0];
[colorButton.layer setBorderColor:[[UIColor darkGrayColor] CGColor]];
//You can use this tag to identify different buttons later
[colorButton setTag:INDEX+SOME_OFFSET]; //use loop index with some offset
[colorButton addTarget:self action:#selector(colorButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(void) colorButtonPressed:(UIButton *) sender
{
UIColor *selectedColor = sender.backgroundColor; //Or identify with sender.tag
[yourCanvas doSmtOnCanvas:selectedColor];
}
Initialize UIImageView then add gesture recognizer to all imageViews by calling this method.
- (void) setupImageView : (UIImageView *) imageView{
[imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(changeUIImageViewBackgroundColor:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tapGestureRecognizer];
}
Now change color of canvasView within the selector by following way:
- (void) changeUIImageViewBackgroundColor : (UITapGestureRecognizer *) recognizer{
UIImageView *imageView = (UIImageView *)[recognizer view];
[canvasView setBackgroundColor:[imageView backgroundColor]];
}
You can put a tap gesture in your scrollview and whenever a tap is made in the scroll check whether the tap is on the imageview. (You will get whether the tap point in on imageview or not) and according you can change the image.
Second alternative is to take a custom button and handle on its click.
Hope it helps
I want to create hyperlink like in html text. Is it possible to hyperlink a label in Xcode?
I suggest using the Round Rect Button then setting the background image to be an image that is the same color as your view's background.
Just add a tapGestureRecognizer to that label
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTapped:)];
tapGesture.numberOfTapsRequired = 1;
self.theLabel.userInteractionEnabled = YES;//Since by default, UILabel is not interaction enabled
[self.theLabel addGestureRecognizer:tapGesture];
//Some where in the code
-(void)labelTapped:(UIGestureRecognizer *)sender
{
//Do some stuff
}
I've got a view containing subviews of various types on it. At one point, I would like to disable all interactions with the view and subviews, and register instead taps from a gesture recognizer I place on the whole view:
tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:revealController action:#selectorrdoSomething:)];
tapGestureRecognizer.cancelsTouchesInView = YES;
While it sort of works, the view below still interacts to all the touches. I then tried adding:
tapGestureRecognizer.delaysTouchesBegan = YES;
tapGestureRecognizer.delaysTouchesEnded = YES;
It now works EXCEPT - when I tap over a UITextField, this receives the touch INSTEAD of the gesture recognizer. Why is this, how can I stop it? Any help is much appreciated :)
Sounds like what you want to do is set the view you don't want receiving the touches .userInteractionEnabled property to FALSE. Or I am misunderstanding something.
yourView.userInteractionEnabled = FALSE;
I am trying to add a UIView as an accessoryView to a UITextField, but the alpha property doesnt seem to be respected.
Here is my current code.
self.keyboardAccView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
[self.keyboardAccView setBackgroundColor:[UIColor lightGrayColor]];
[self.keyboardAccView setOpaque:NO];
[self.keyboardAccView setAlpha:0.0];
UITapGestureRecognizer *hideKeyboardTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard:)];
[self.keyboardAccView addGestureRecognizer:hideKeyboardTap];
[hideKeyboardTap release], hideKeyboardTap=nil;
The alpha value doesnt seem to matter. No matter what I set it to, the accessoryView is always set to no transparency.
What I'm trying to accomplish is show a transparent view above the keyboard that will dismiss the keyboard anytime the user taps away from the keyboard. If there is a better/proper way to do this that I am completely missing, I'm all ears as well.
EDIT *
I know I could just use [UIColor clearColor] as the backgroundColor but I more want to know why the alpha setting isnt honored, in case I truly did want to have a semi-transparent accessoryView
yes - there is a better way to do it! :)
I personally do it like this - I change my ViewController.xib class (UIView to UIControl) and then I create a simple IBAction which resigns first responder whenever is the UIControl tapped.
The method should look like this:
- (IBAction)hideKeyboard {
[textField resignFirstResponder];
}
That's it :)
I was wondering if it is possible to add gesture recognizers to objects like buttons, images etc.
I have used this code to create a gesture recognizer:
UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(foundTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
and than when I try to add the gesture I use this code:
[button addGestureRecognizer:tapRecognizer];
Problem is that it doesn't work.
Any ideas?
Thanks in advance :)
Your code works great for me on a UIButton, and overrides the normal action of the button.
Can you be more specific about "doesn't work"? You mean it gives an error? Or you see no callback?
If the latter, how did you declare the method 'foundTap:' ?