iphone - do an action while a button is pressed - iphone

Say for example i have two volume buttons ( + and - )
How can I implement something such as when holding the + button, it will raise up the volume incrementally at an interval? (I'm only interested in doing an action at an interval while the button is being pressed)

You can use a timer for this. Start the timer when the touch starts. If the timer expires, increase or decrease the volume and restart the timer. When the touch ends, cancel the timer.

Assuming you have two button one for - and other for + ,
you could store the interval information in the tag field of your button
Installing the interval value in your button tag property.
myPulseButton.tag = 10;
myMinusButton.tag = 10;
Adding action with your button.
[myPulseButton addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[myMinusButton addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
Implement the buttonEvent method like below.
-(void) buttonEvent:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton == myPulseButton)
{
[self increaseVolume:myPulseButton.tag];
}
else if(myButton == myMinusButton)
{
[self decreaseVolume:myMinusButton.tag];
}
}

First add the button...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(increase)forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
then write the method for buttton press.
-(void)increase
{
//increase the volume here
}
if it is not the solution you want means add the comment...

Related

How to make button bright when clicking (like clicking shift key in iphone)

I want to do a shift key. When I click it, the key will change to other letters. How can I make it bright when clicking this shift button like in iphone ? Can you give some example codes ?
If you'd like a button's image to change when tapped, and then back, you can use the -[setImage:forState:] method. For this example, imagine yourself having 2 images: image_one.png and image_two.png for the unpressed and pressed images respectively.
Suppose you have a button like so:
UIButton *button = [[UIButton alloc] init];
[button setFrame:CGRectMake(0, 0, 100, 44)];
[button setImage:[UIImage imageNamed:#"image_one.png"] forState:UIControlStateNormal];
[[self view] addSubview:button];
You can add a target and action that will change the image back and forth. To see which image it is currently, I'd use the tag property.
[button setTag:0]; // initial, non pressed button tag, for this example
[button addTarget:self action:#selector(swapImage:) forControlEvents:UIControlEventTouchUpInside];
And have something like the following in the setImage method:
-(void)swapImage:(UIButton *)sender {
if ([sender tag] == 0) {
[sender setTag:1]; // pressed tag, for example
[sender setImage:[UIImage imageNamed:#"image_two.png"] forState:UIControlStateNormal];
} else {
[sender setTag:0]; // non-pressed tag, for example
[sender setImage:[UIImage imageNamed:#"image_one.png"] forState:UIControlStateNormal];
}
}
When you first tap the button, it selects. Tapping it another time will unselect it - just like the keyboard's shift button.

How to identify which button is clicked in Objective-C?

I am a new iPad developer.
I have created UIButton programmatically, in which I want to identify user has clicked on which button and according to that I want to do some action.
How should I identify this?
Here is my code snippet:
for(int i=0;i<[_array count];i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=count;
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
button.backgroundColor=[UIColor redColor];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(xpos, 270.0, 200.0, 150.0);
[self.view addSubview:button];
}
This is what I'm doing now: I thought I will assign count to each button and I will pass this count to button click method. Is this a good idea? Is there any other way possible?
On button click I'm calling aMethod:
-(void)aMethod:(id)sender{
NSLog(#"btn clicked");
}
Any help will be appreciated!
-(void)aMethod:(UIButton*)sender{
NSLog(#"btn clicked %d", sender.tag);
}

How can i maintain the selected state in button in iPhone

I have created scroll view and sets the buttons are in the scroll view. The Buttons are scrolling horizontally and it works fine. If i clicked the button, i set background image as "Selected State" in button. My problem is how can i changed the selected state in different button, when clicking it and how can i deselected the "selected state" button when clicking the another button.
I have three buttons in the scroll view,
-(IBAction) Button1 : (id) sender
{
// btn1.selected = YES;
[btn1 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateSelected];
}
-(IBAction) Button2 : (id) sender
{
// btn2.selected = YES;
[btn2 setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateSelected];
}
-(IBAction) Button3 : (id) sender
{
// btn3.selected = YES;
[btn3 setImage:[UIImage imageNamed:#"three.png"] forState:UIControlStateSelected];
}
see the below image,(Health, Entertainment and Money Watch are the three buttons)
Image http://www.freeimagehosting.net/uploads/6b3daab12f.png
and
Img http://www.freeimagehosting.net/uploads/b6e0f234dc.png
Note:(Like, Tabbar and Segmented control)
On clicking first button and sets background image in selected state and clicking the second button, then first buttons are to be deselected. So how can i maintain the selected state, till another button is clicked.
Thanks in Advance.
I solved this task in the following way:
init method:
Create number of buttons with defined images for normal and selected state.
Assign tag for each button (for example, for i'th button tag is 1000+i).
Assign IBAction for each button.
action method:
Remove selection from previously selected button (search it by it's tag with [view viewWithTag:] method)
Select sender.
Save sender's tag.
Here's the code:
- (void)init {
....INITIALIZE SCROLLVIEW HERE.....
for ( int i = 0; i < 10; i++ ) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:.....];
[btn setImage:_IMAGE_ forState:UIControlStateNormal];
[btn setImage:_IMAGE2_ forState:UIControlStateSelected];
[btn setTag:i + 1000];
[btn addTarget:self action:#selector(setSelectedButton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
}
- (IBAction)setSelectedButton:(id)sender {
[self setSelectedButtonByIndex:((UIButton *)sender).tag - 1000];
}
- (void)setSelectedButtonByIndex:(NSInteger)index {
if ( selectedElemId >= 0 ) {
UIButton *btn = (UIButton *)[self viewWithTag:selectedElemId + 1000];
[btn setSelected:NO];
}
UIButton *btn = (UIButton *)[self viewWithTag:index + 1000];
[btn setSelected:YES];
selectedElemId = btn.tag - 1000;
}

UIButton : managing dynamic creation and touch events

I'm currently dynamically creating UIButton objects in my view.
I have an NSMutableArray containing information about them (id - label).
I then create my view objects by doing a for iteration on my MutableArray.
I'm trying to use this code on my buttons to catch touche events :
[myButton addTarget:self action:#selector(selectedButton:) forControlEvents:UIControlEventTouchUpInside];
My selectedButton method is called with success, but I don't have any idea on knowing with button were touched.
I tried to do this :
-(void)selectedButton:(id)sender {...}
But don't know what to do with the sender object.
Thanks in advance for your help !
At the top of your .m file, put something like this:
enum {
kButtonOne,
kButtonTwo
};
When you're creating your buttons, do this
myButton.tag = kButtonOne;
Then in your selected button method, do this:
-(void)selectedButton:(id)sender {
switch (sender.tag) {
case kButtonOne:
// do something here
break;
case kButtonTwo:
// do something else here
break;
}
}
Set mybutton.tag to something, and then check for that tag in selectedButton:sender.
-(void)viewDidLoad{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 210.0, 40.0, 30.0);
button.tag=1;
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"raaz" forState:UIControlStateNormal];
[self.view addSubview:button];
}
-(IBAction)aMethod:(id)sender{
UIButton *btn=(UIButton *)sender;
NSLog(#"I have currently Pressed button=%d",btn.tag);
}

Add a multiple buttons to a view programmatically, call the same method, determine which button it was

I want to programmatically add multiple UIButtons to a view - the number of buttons is unknown at compile time.
I can make one or more UIButton's like so (in a loop, but shorted for simplicity):
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button x" forState:UIControlStateNormal];
button.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);
[view addSubview:button];
Copied/Edited from this link:
How do I create a basic UIButton programmatically?
But how do I determine in buttonClicked: which button was clicked? I'd like to pass tag data if possible to identify the button.
You could either keep a reference to the actual button object somewhere that mattered (like an array) or set the button's tag to something useful (like an offset in some other data array). For example (using the tag, since this is generally must useful):
for( int i = 0; i < 5; i++ ) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:i];
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aView addSubview:aButton];
}
// then ...
- (void)buttonClicked:(UIButton*)button
{
NSLog(#"Button %ld clicked.", (long int)[button tag]);
}
You can assign a tag to the button.
button.tag = i;
Then in -buttonClicked:, check the tag of the sender:
-(void)buttonClicked:(UIButton*)sender {
int tag = sender.tag;
...
}
I think this would be the best answer:-
http://conecode.com/news/2012/05/ios-how-to-create-a-grid-of-uibuttons/
For that give different tag to each button & use code like this:
[btn1 addTarget:self action:#selector(pressbtn:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:#selector(pressbtn:) forControlEvents:UIControlEventTouchUpInside];
& in method
-(void)pressbtn:(id)sender {
UIButton *button=sender;
if (button.tag==1)
{
NSLog(#"Press button 1");
}
if (button.tag==2)
{
NSLog(#"Press button 2");
}
and so on to check which button is called
If you want to add buttons at run time then there will be 10 20 50 or more than that. Then you should to use ui scroll view in this condition.
When the buttons will be generate then your scroll view size should be increased accordingly.
And you can write the code like this
scrollview = [[UIScrollView alloc] init];
scrollview.contentSize = CGSizeMake(INVOICE_ADDITEM_SCROLLVIEW_CONTENT_WIDTH, INVOICE_ADDITEM_SCROLLVIEW_CONTENT_HEIGHT);
scrollview.frame = CGRectMake(0,50, SCROLLVIEW_WIDTH, SCROLLVIEW_HEIGHT);
// scrollview.backgroundColor = [UIColor whiteColor];
scrollview.scrollsToTop = NO;
scrollview.delegate = self;
[self.view addSubview:scrollview];
for (int pos = 0; pos < 2; pos++) {
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[but setImage:[UIImage imageNamed:#"checkbox_active.png"] forState:UIControlStateSelected];
[but setFrame:CGRectMake(TXT_FLD_X_CORD+90, 295, 20, 20)];
// [but setCenter:CGPointMake( 50, i*40+20 )];
but.tag = pos;
/*if(pos==0)
{
[but setImage:[UIImage imageNamed:#"checkbox_active.png"] forState:UIControlStateNormal];
// [but setImage:[UIImage imageNamed:#"checkbox_active.png"] forState:UIControlStateSelected];
}*/
[but setCenter:CGPointMake(pos*90+125 ,310)];
[but addTarget:self action:#selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:but];
}
UIButton has a tag property. Use that and in your buttonClicked method, you can check the button that was clicked based on it's tag. Might want to keep constants around for what button is what.
For each of your buttons set an appropriate tag, and then refer to the tag in your action. i.e.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
...
button.tag = 1
[view addSubview:button];
You can easily set the tag based on the index of your iteration, if you're creating buttons in a loop. And then in your action:
- (void)aButtonWasTapped:(UIButton *)source {
if( source.tag == 1 ) {
// etc
}
}
Somebody might have this problem:
Jason Coco's answer worked fine for me, until I wanted to use the tag to access properties from an NSArray that was defined as a property.
Turns out the property had to be defined as "retain" instead of "weak".
The Swift version (with Labels):
for index in 1...5 {
var label = UILabel()
label.tag = index
labelsDictionary["Label\(index)"] = label
self.addSubview(label)
}
Call using using self.viewWithTag(i) as UILabel:
(cell.viewWithTag(5) as UILabel).text