moving button image from first button pressed to second button pressed - iphone

I have 5X5 grids of buttons. If I press the button at position1 and then press the button at position2, I want the button image of position1 to be moved to the button at position2. Like as shown in the example below below, where A1 button is pressed first and then B2 button is pressed. Example:
Fig: A1 button pressed
Fig: B2 button pressed
So, the total process has two steps:
1. Prepare to move image when first button pressed
2. Move the image from first button to second button when the second button is pressed
I have set button tags for those 25 buttons shown in the 5X5 grid. Now I am having trouble to find out the logic to have the desired action.
What would be the logic in the these methods:
-(void)a1ButtonPressed:(id)sender
{
}
-(void)b2ButtonPressed:(id)sender
{
}
While the buttons have these tags:
A1.tag = 1;
B2.tag = 7;
Can anyone help? Thanks in advance.... :)

There are many ways to obtain your goal...
You can use this method to change your button image:
[aButton setImage:[UIImage imageNamed:#"anImage"] forState:UIControlStateNormal];
And you can manage your button image using a variable.
Then:
-(void)a1ButtonPressed:(id)sender
{
if (firstButton) {
// Get image to move from A1
imageToMove = [A1 imageForState:UIControlStateNormal];
// Remove image from A1
[A1 setImage:nil forState:UIControlStateNormal];
firstButton = NO;
} else {
// Put new image to A1
[A1 setImage:imageToMove forState:UIControlStateNormal];
firstButton = YES;
}
}
-(void)b2ButtonPressed:(id)sender
{
if (firstButton) {
// Get image to move from B2
imageToMove = [A1 imageForState:UIControlStateNormal];
// Remove image from B2
[B2 setImage:nil forState:UIControlStateNormal];
firstButton = NO;
} else {
// Put new image to B2
[B2 setImage:imageToMove forState:UIControlStateNormal];
firstButton = YES;
}
}
And so on...
fistButton is a BOOL that control if the button pressed is first or second.
imageToMove is a UIImage declared elsewhere that manage the image to move.
Anyway, you can use just one method for all of yours 25 buttons:
-(void)buttonPressed:(UIButton *)button
{
if (firstButton) {
// Get image to move from button
imageToMove = [button imageForState:UIControlStateNormal];
// Remove image from button
[button setImage:nil forState:UIControlStateNormal];
firstButton = NO;
} else {
// Put new image to button
[button setImage:imageToMove forState:UIControlStateNormal];
firstButton = YES;
}
}

If you want to give animation need to do UIView animation that is core animation
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
//set animation value means the next postion where u want to move object
[UIView commitAnimations];
please like it

Just use one method for all the buttons, simple logic something like this
-(void)buttonPressed:(UIButton *)btn {
if (buttonPressed) {//second press
[initialBtn setImage:blankImage forControlState:UIControlStateNormal];
[btn setImage:buttonImage forControlState:UIControlStateNormal];
buttonPressed=NO;
} else {//first press
buttonImage=[btn imageForState:UIControlStateNormal];
buttonPressed=YES;
initialBtn=btn;
}
In your header you would have
BOOL buttonPressed;
UIImage *buttonImage;
UIButton *initialBtn;
This would also work if the user pressed the same button twice, cancelling their selection.
Hope this helps you!

An alternative to accomplish this, add all the buttons in an array when creating the buttons and set each button a tag.Then store the tag of both pressed button.Use the previous pressed tag to read the button using [buttonarray objectAtIndex:previous.tag] and set to nil and to current tag set background image of previous button.

Related

UIButton tags in iPhone

I have 2 button objects (UIButton *obtn,*ebtn), I have created 12 buttons(6 buttons with 6 tag values for obtn, 6 buttons with 6 tag values for ebtn)... I have a single btnClick: method for all those 12 buttons... when i click on a button of tag value 1... Iam getting the properties of selected button to another button by using sender... I want to get the properties of unselected button (Example obtn object with tag value 3) into another button How can i get it...?
My Direct Question is: How to assign the properties of a button with a tag value to another button when it is in unselected state...
It sounds like you want to be able to get a pointer to a button with a certain tag, and at the moment all you can do is get the button that has sent you the action.
The method you are looking for is viewWithTag:. This will get you a button (or any other view) with the tag you want. You pass this method to the superview of the button. So, in your action method (assuming all buttons are in the same superview):
UIView *superview = [sender superview];
UIButton *otherButton = [superview viewWithTag:3];
You then have sender, which is the button that was tapped, and otherButton, which is the button with tag 3. You can modify each one as you wish.
// Create textfield 5
btn_select_Country = [UIButton buttonWithType:UIButtonTypeCustom];
//btn_select_Client = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn_select_Country.frame = CGRectMake(BTN_X_CORD, 228, BTN_WIDTH, BTN_HEIGHT);
btn_select_Country.tag = 1;
// [btn_select_Client setTitle:#"Button One" forState:UIControlStateNormal];
[btn_select_Country addTarget:self action:#selector(countryListBtnTap:) forControlEvents:UIControlEventTouchUpInside];
btn_Image_Select_Country= [UIImage imageNamed:#"drop-down.png"];
[btn_select_Country setImage:btn_Image_Select_Country forState:UIControlStateNormal];
//btn_select_Client.tag=1205;
[scrollview addSubview:btn_select_Country];
- (IBAction)currencyListBtnTap:(id)sender;
{
button_Number = [sender tag];
[self allTextFeildResignFirstResponder];
}

Small UIButton on top of larger UIButton

I have a smaller UIButton which is on top of a larger UIButton.
The problem right now is that if I tap the smaller UIButton, it also will trigger the larger UIButton. The code I'm using to determine if the buttons were tapped is:
if(CGRectContainsPoint(button1.frame, location)) {
}
Is there a property of the buttons or some automated way to make the smaller button not effect the larger button?
I know I could alter the code above to say if its within button1's frame and not within button2, but is there another way to do it?
UIControl (which is the superclass of UIButton) passes itself as the only parameter to its target using the action selector. Make use it, it's there for exactly these cases!
[smallButton addTarget:self action:#selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
[bigButton addTarget:self action:#selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
// ...
- (void) doStuff:(UIButton *)btn
{
if (btn == smallButton)
{
// smaller button was clicked
}
else
{
// bigger button was clicked
}
}

adding extra image when the UIButton remain pressed

I want to add an extra image when my button remains pressed.
Is it possible in iphone?
I guess you mean when the user makes a long press on the cell (tap down and keep finger down)?
You can add a UILongPressGestureRecognizer element to your button and specify its target and action like this:
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[self.button addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];
This will call the -longPress: method when a user long presses on the button. The callback function could look like this:
- (void)longPress:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan){
// do something like add an image
}
}
Hope this helps!
Of course Yes,
First take a custom button and set any image to it.
Then bind the following method wih Touch Down event of the button.
-(IBAction)buttonTouchedImage:(id)sender
{
[yourButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateHighlighted];
}
Set button highlighted image and make it highlighted = YES when button pressed. This should work I guess.

how can I get image tag from different image views on tapping on it

I want to used image view from nos. of image views in scroll view on single tapping any particular image view.
If you are insistent on using images instead of buttons you can use Gesture Recognizer. Create an imageView and enable its userInteraction
UIImageView *testImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"someImage"]];
testImageView.frame = CGRectMake(30.0,30.0,60.0,40.0);
testImageView.tag = 30;
testImageView.userInteractionEnabled = TRUE;
[tempPlotView addSubview: testImageView];
[testImageView release];
Now allocate a gesture Recognizer object and add it to your imageView...
UITapGestureRecognizer *testGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singletap:)];
[testGesture setNumberOfTapsRequired:1];
[testImageView addGestureRecognizer: testGesture];
[testGesture release];
Now in the selector "singleTap" you can do whatever your action is..
-(void)singleTap:(UIImageView*)sender{
if(sender.tag == 30){
//do your stuff here...
}
}
Hope this help...cheers....
Create a button and set image as background.while creating button you can set tag like
button.tag=yourtag;//your tag integer value
[button addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchDown];
[button setBackgroundImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
and implement this function in your clss
- (void)buttonTouched:(UIButton *)sender
{
NSLog(#"touched %i",[sender tag]);
}
while tapping the particular button this function will get called.
Rather than use image views, you could use UIButtons with image views as their content.
That way, you'll get a callback when a given image is tapped with a reference to the button. From there you should be able to get the tag of that which has been tapped!
I hope this helps,
You can do according to what Nick has suggested.
Or else, you can create a subclass of the imageview. Set frame and tag to it.
In touchesEnded method of this custom class, you can find which imageview it is based on its tag.

To change the state of button highlighted from normal and keeping it hold

I am creating an iphone app where I have used 25 buttons and each button has been shown a background image in its normal mode.I have also set another image on the highlighted mode of the button.
Initially button will be shown in its normal mode
now I want that when a button is pressed then button should change to its highlighted state so as the other image will be displayed there.
I have done so by doing:
button.highlighted = YES;
Its working but its sets the highlighted image for a fraction and then again normal state of the button come back. I use the following code to create the buttons.
for (int i=0; i<25; i++) {
if (i > 0) {
if (i%5 == 0) {
xaxis = 28;
yaxis = yaxis+42;
}
}
iconButton[i] = [UIButton buttonWithType:UIButtonTypeCustom];
iconButton[i].frame = CGRectMake(xaxis, yaxis, 50, 40);
[iconButton[i] setBackgroundImage:[iconArray objectAtIndex:random] forState:UIControlStateNormal];
[iconButton[i] setBackgroundImage:[tapedIconArray objectAtIndex:random] forState:UIControlStateHighlighted];
[iconButton[i] addTarget:self action:#selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:iconButton[i]];
xaxis = xaxis+53;
}
How can i hold the state of the button, I even tried with timer but app get crashed then.
Please help me
Many Thanks in advance
I am not sure of this but try it by setting image for button's UIControlStateSelected
i.e.
[btn setImage:[UIImage imageNamed:#"selectedImage.png"] forState:UIControlStateSelected];