UIButton show selected state on tap - objective-c

How do I show the selected state for the UIButton on tap?
I want it so that when people tap on the uibutton it shows the selected state with the hover that I have?
Right now they have to hold the button to see the hover.
[t1 setBackgroundImage: [UIImage imageNamed: [NSString stringWithFormat: #"hover.png"]] forState:UIControlStateSelected];
I want to show the hover image on tap also.

You need to set the selected state for the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setImage:[UIImage imageNamed:#"hover.png"] forState:UIControlStateSelected];
To show a hoover image while the button is pressed you need to set the state property to UIControlStateHighlighted

I had the same problem - my button was highlighted when pressed (tap+hold) and wasn't - when simply tapped. Here is my code:
btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image =[UIImage imageNamed:#"btn"];
UIImage *imageSelected =[UIImage imageNamed:#"btn_tap"];
[btn setBackgroundImage:transImage forState:UIControlStateNormal];
[btn setBackgroundImage:imageSelected forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
-(void)btnPressedWithDelay{
//your code for btn action
}
- (void) action:(UIButton *)sender{
[self performSelector:#selector(btnPressedWithDelay) withObject:nil afterDelay:0.1];
}
Did it work for you?

Related

Change button image on click event

I want to change button image. As when i click on the button it will change it's image and after the click the button will return in its normal form.I want to know how to implement this? Please help me.
UIImage *buttonImage = [UIImage imageNamed:#"image.png"];
[sender setImage:buttonImage forState:UIControlStateHighlighted];
UIImage *buttonImage1 = [UIImage imageNamed:#"image.png"];
[sender setImage:buttonImage1 forState:UIControlStateNormal];
You can customize the "highlighted" state of your button right in Interface Builder, or use this code:
[self.button setImage:[UIImage imageNamed:#"normal"] forState:UIControlStateNormal];
[self.button setImage:[UIImage imageNamed:#"pressed"] forState:UIControlStateHighlighted];
first fix the Backgroudimage is #"image1.jpeg" in xib, then
- (IBAction)mybutton:(id)sender
{
[_buttonrefrence setBackgroundImage:[UIImage imageNamed:#"download.jpeg"] forState:UIControlStateHighlighted];
}

Event on an Image in iOS

I am pretty new to iOS development, so this may seem to be a stupid question.. In my xib file, i have taken a UIImageView that will show an image when the page is loaded. But when a user taps on an image, I want the next view to be loaded.. But I am not able to do that because we cannot have a delegate not a target-action mechanism for a UIImage view.. Many places, I found a workaround.. using a button and then placing an image on it, and then using it.. but I want the first way to work.. Please help..
use UITapGestureRecognizer ,and Don't forget to set imageView.userInteractionEnabled = YES; because by default image not having UserInteraction.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:singleTap];
-(void)handleSingleTap:(id)sender {
// push you view here
}
(OR) If you want
take button with BackGround image.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
Instead of Using UIImageView, you can use UIButton.
UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[myBtn setBackgroundImage:[UIImage imageNamed:#"your_image_Name"] forState:UIControlStateNormal];
[myBtn setBackgroundImage:[UIImage #"your_image_Name"] forState:UIControlStateHighlighted];
[myBtn addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
also write button action:
-(void)buttonAction:(id)sender {
// Do whatever you want
}
In Case of XIB, Just use UIButton instead of UIImageView and set Background Image for both state Normal & Highlighted as you want. Rest you can manage button's action from XIB.
You can also add a fully transparent UIButton on top of your UIImageView... It should be preferred as each element has a clean semantics.

Why are UIButton actions not being called when UIButton is inside a UIScrollView

I have created a springboard like panel containing buttons as follows:
-(void)configurePanel{
self.criteriaPanel.scrollEnabled=YES;
self.criteriaPanel=[[UIScrollView alloc] initWithFrame:CGRectMake(
0.0f,
0.0f,
[UIScreen mainScreen].bounds.size.width,
(BUTTON_HEIGHT+(3*BUTTON_Y_OFFSET))
)];
UIView *scrollViewContent=[[UIView alloc]init];
NSArray *criteriaTypeButtonTitles=[NSArray arrayWithObjects:
#"Option1",
#"Option2",
#"Option3",
#"Option4",
#"Option5",
#"Option6",
nil
];
for (int i=0; i<[criteriaTypeButtonTitles count]; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=i;
button.frame = CGRectMake(
(((BUTTON_X_OFFSET*i)+(BUTTON_WIDTH*i))+BUTTON_X_OFFSET)
, BUTTON_Y_OFFSET
, BUTTON_WIDTH
, BUTTON_HEIGHT
);
[button setTitle:[criteriaTypeButtonTitles objectAtIndex:i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
button.titleLabel.font=[UIFont systemFontOfSize:13.0f];
[button setBackgroundColor:[UIColor grayColor]];
[button.layer setCornerRadius:10.0f];
[button.layer setBorderWidth:1.0f];
[button addTarget:self action:#selector(sortByButtonTap:) forControlEvents:UIControlEventTouchDown];
[scrollViewContent addSubview:button];
}
//based upon # of buttons
self.criteriaPanel.contentSize=CGSizeMake(
(([criteriaTypeButtonTitles count]*BUTTON_WIDTH)+([criteriaTypeButtonTitles count]*BUTTON_X_OFFSET)),
(BUTTON_HEIGHT+(2*BUTTON_Y_OFFSET)));
[self.criteriaPanel addSubview:scrollViewContent];
[self.view addSubview:self.criteriaPanel];
}
The panel displays correctly and scrolls, but the tap events (sortByButtonTap:) for the buttons are never called. I suspect this is related to the buttons being contained in a view which is contained in the scrollview. After reading a number of other questions and the docs I still can't figure out what the solution should be.
EDIT:
I experimented w/ adding the buttons to the UIScrollView (self.criteriaPanel) directly and the button taps call sortByButtonTap: so it is something to do w/ the buttons being in the scrollViewContent.
You might need to set userInteractionEnabled on the containing view scrollViewContent for the button to receive touch events:
[scrollViewContent setUserInteractionEnabled:YES];
What if you try:
[button setExclusiveTouch:YES];
Buttons tend to work abnormally when they are inside a view that also handles touch events.
Follow this code for your query,
Add UIButton in Scrollview
scrollview.pagingEnabled = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake();
[button setTitle:#"X" forState:UIControlStateNormal];
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnCloseClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:#selector(btnCloseDraged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(btnCloseDragedEnd:withEvent:) forControlEvents:UIControlEventTouchDragExit];
[scrollview addSubview:button];
scrollview.userInteractionEnabled = YES;
you can put this three methods of button manually.
Let me know if you still have query.
instead of:
[button addTarget:self
action:#selector(sortByButtonTap:)
forControlEvents:UIControlEventTouchDown];
try:
[button addTarget:self
action:#selector(sortByButtonTap:)
forControlEvents:UIControlEventTouchUpInside];
I think your problem is just that you set the wrong UIControlEvent to the UIButton. Change from UIControlEventTouchDown to UIControlEventTouchUpInside. If you connect a UIButton inside the Interface Builder it will always set it to this UIControlEvent:
Another reason:
Otherwise your UIScrollView controls the interactions because it needs to know if you want to scroll it. If you place UIButtons inside your UIScrollView they will be accessible but always with a little delay. This is why you should maybe change the settings for the UIScrollView:
You can disable delays content touches and UIButtons will be accessible immediately

UIImage Buttons (Rounded Rect Button)

I want to be able to change the image of any of the buttons upon the user pressing the button and then when they release, the image that was there before is put back.
So basically when they press their finger on the button, the image changes and when they release, the original image I specified in IB is put back.
How can I do this? Here is the code I have so far.
- (IBAction)btnOne:(id)sender {
result.text = [self appendresult:#"1"];
//Changes the button image upon touch
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1.png"] forState:UIControlStateNormal];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateSelected];
btnOne.showsTouchWhenHighlighted = YES;
}
Put your code...
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1.png"] forState:UIControlStateNormal];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateSelected];
btnOne.showsTouchWhenHighlighted = YES;
in viewDidLoad instead of in the IBAction, then just make sure that your button in Interface Builder is properly linked to the IBOutlet you created for it.

Special tag for UIButton?

I have 3 different UIButtons for each image grouped. I have IDs for each image. Right now, I have a special id for each image, and I set the button with that tag.
I want to change the background image of the one selected when you tap it. The problem is, is that 3 buttons have the same tag so I cannot change the right button's background image.
Here's what I have:
UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[likeButton setBackgroundColor:[UIColor clearColor]];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button.png"] forState:UIControlStateNormal];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateSelected];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateHighlighted];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateDisabled];
[likeButton setFrame:CGRectMake(13, 52 + (285 * count), 51, 55)];
[likeButton addTarget:self action:#selector(likeDudle:) forControlEvents:UIControlEventTouchDown];
[likeButton setTag:theIdInt];
[likeButton setTitle:#"no_like" forState:UIControlStateNormal];
[scrollView addSubview:likeButton];
- (IBAction)likeDudle: (id)sender {
NSInteger tagId = ((UIControl*)sender).tag;
UIButton *tempButton = (UIButton*)[scrollView viewWithTag:tagId];
NSLog(#"likeDudle: %d // %#", tagId, tempButton.titleLabel.text);
if ([tempButton.titleLabel.text isEqualToString:#"no_like"]) {
[tempButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"like" forState:UIControlStateNormal];
} else if ([tempButton.titleLabel.text isEqualToString:#"like"]) {
[tempButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"no_like" forState:UIControlStateNormal];
}
Is there a better way in doing this?
Thanks,
Coulton
If you have fewer than 10 images, then give the ith button the tag i*10+image.tag. Then you can retrieve the image.tag by button.tag % 10 and the button tags will be unique. You can even retrieve the button only information by int b = button.tag/10.
Also, you can access the button's image's tag with button.backgroundImage.tag, so then the button could have its own separate tagging system, depending on your uses.