Changing UIImageView with button - iphone

I am wanting to press a button and alternate between two images with one UIImageView.
Right now when I run it, I press the button, the image changes but will not change back. What do I need to change in this action code to make it alternate?
- (IBAction)Change:(id)sender {
image.image = [UIImage imageNamed:#"car.png"];
}

Try following code :
- (IBAction)Change:(id)sender {
if ([sender isSelected])
{
imageView.image = [UIImage imageNamed:#"car.png"];
[sender setSelected:NO];
}
else
{
imageView.image = [UIImage imageNamed:#"bike.png"];
[sender setSelected:YES];
}
}
To Display sequence image on button click.
.h file :
int counter;
.m file :
in viewDidLoad initialize counter = 0
Then
- (IBAction)Change:(id)sender {
counter++;
imgView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png",counter]];
}
And give your image name like 1.png, 2.png, 3.png and so on...

You have to maintain a flag.
In Somewhere before tapping the button, assign the flag
BOOL isFirstImageShown=YES;
Then on your button action
- (IBAction)Change:(id)sender {
if(isFirstImageShown)
{
isFirstImageShown=NO;
yourImageView.image = [UIImage imageNamed:#"yourSecondImage.png"];
}
else
{
isFirstImageShown=YES;
yourImageView.image = [UIImage imageNamed:#"yourFirstImage.png"];
}
}

You want that on button click you need another image you have to first set the image when you load the view and then on button click.

Related

Any alternative to get Image name that is currently set to UIImageView?

I am using a UIImageView and about 5 images that is changing time to time with the help of timer.
Can we get currently set image name in UIImage.I google it and found a that by inserting image names to array and by setting tag to imageView we can get image name by its tag but in my case there is only one imageView and images are 5.My code is given below:--
[_imgView setImage:[UIImage imageNamed:#"tops.png"]];
imgArray=[[NSArray alloc] initWithObjects:#"tops.png",#"position_conv.png",#"stocks.png",#"home.png",#"report.png",nil];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(change) userInfo:nil repeats:YES];
-(void)change
{
int randNum = rand() % (4 - 0) + 0;
[_imgView setImage:[UIImage imageNamed:[imgArray objectAtIndex:randNum]]];
}
Thanks!
you can use Accessibility identifier property of UIImageView's image.Set image accessibility identifier in your method and get anywhere you want.
-(void)change
{
int randNum = rand() % (4 - 0) + 0;
[_imgView setImage:[UIImage imageNamed:[imgArray objectAtIndex:randNum]]];
[_imgView.image setAccessibilityIdentifier:[imgArray objectAtIndex:randNum]];
}
- (IBAction)currentImgName:(id)sender {
NSLog(#"image name is %#", [_imgView.image accessibilityIdentifier]);
}
You can use setAccessibilityIdentifier method for any subclass of UIView
UIImageView *image ;
[image setAccessibilityIdentifier:#"file name"] ;
NSString *file_name = [image accessibilityIdentifier] ;

how to change image in imageView on button click

I want to change the image in image view on button click but it does not changing. I am using JPG image to load in image View
- (IBAction) changeImage:(id)sender
{
UIButton *tappedButton = (UIButton*) sender;
NSInteger selectedIndex = [tappedButton tag];
if (selectedIndex==0)
{
UIImage *modelimage = [UIImage imageNamed:#"compositone.jpg"];
modelImagewView.image=modelimage;
}
else
{
UIImage *modelimage = [UIImage imageNamed:#"composittwo.jpg"];
modelImagewView.image=modelimage;
}
}
Try this format - modelImagewView.image = [UIImage imageNamed:#"composittwo.jpg"];
Also, make sure you have set tag numbers properly so that selectedIndex is not always 0. At the same time check if you have linked this IBAction to the appropriate button.
Try This Code
- (IBAction) changeImage:(id)sender
{
UIButton *tappedButton = (UIButton*) sender;
if (tappedButton.tag==0)
{
modelImageView.image=[UIImage imageNamed:#"compositone.jpg"];
}
else
{
modelImageView.image=[UIImage imageNamed:#"composittwo.jpg"];
}
}

Unable to keep UIButton in selected state after TouchUpInside Event

I have the need for an UIButton to maintain a pressed state. Basically, if a button is in a normal state, I want to touch the button, it highlight to its standard blue color and then stay blue after lifting my finger.
I made the following UIAction and connected the buttons Touch Up Inside event to it.
-(IBAction) emergencyButtonPress:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
But what happens, after I remove my finger, the button goes back to a white background. For a test I added a UISwitch and have it execute the same code:
-(IBAction) emergencySwitchClick:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
In this case the button toggles to a highlighted and non-highlighted state as I would expect.
Is there another event happening after the Touch Up Inside event that is resetting the state back to 'normal'? How do I maintain the highlighted state?
The highlighted state is applied and removed by iOS when you touch / release the button. So don't depend on it in your action method.
As one of the other answers says, set the highlighted image to be the same as the selected image, and then modify your action method:
-(IBAction) emergencyButtonPress:(id) sender
{
emergencyButton.selected = !emergencyButton.selected;
}
If you want something like a toggle button, customize your "selected" state to be your "pressed" state, and then write something like this:
- (IBAction)buttonTapped:(id)sender {
[(UIButton*)sender setSelected:![sender isSelected]];
}
if you want the above code to work, you should set an image for the state selected and one (even the same) for he state highlighted.
[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateHighlighted];
[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateSelected];
hope it helps.
I had this same problem, and this is what worked for me:
-(IBAction)buttonPressed:(id)sender {
if (isSelected) {
[_button setSelected:NO];
[_button setImage:[UIImage imageNamed:#"not_selected.png"] forState:UIControlStateSelected];
isSelected=NO;
}
else {
[_button setSelected:YES];
[_button setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateSelected];
isSelected=YES;
}
}
This is not possible unfortunately, as soon as you let go Apple changes the state again.
One option I've used before (but not pretty) is that you use a performSelector with delay 0.1 after the button is pressed to put it again in the selected state. This did work in my case.
EDIT: If you don't want to see the blinking effect, just set the delay to 0.0
This worked for me:
- (void)tapButton:(id)sender{
UIButton *button = sender;
if (!button.selected){
[self performSelector:#selector(highlight:) withObject:button afterDelay:0.0];
}else{
[self performSelector:#selector(removeHighlight:) withObject:button afterDelay:0.0];
}
}
- (void)highlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = YES;
}
- (void)removeHighlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = NO;
}
Try this simple answer....
- (void)mybutton:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = ![button isSelected]; // Important line
if (button.selected)
{
NSLog(#"Selected");
NSLog(#"%i",button.tag);
}
else
{
NSLog(#"Un Selected");
NSLog(#"%i",button.tag);
}
}

Iphone : Applying strechable images to a button disables it

I am sure I am doing something stupid here. I build a category on top of UIButton which I want it to take all of the background images assigned to it (different states) and convert them to stretchable versions and reapply them back to the button.
- (void)enableBackgroundImageStrechingWithLeftCapWidth:(float)leftCapWidth withTopCapHeight:(float)topCapHeight;
{
UIImage *backgroundimageNormal = [self backgroundImageForState:UIControlStateNormal];
if (backgroundimageNormal != nil)
{
UIImage *stretchImage = [backgroundimageNormal stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateNormal];
}
UIImage *backgroundimageSelected = [self backgroundImageForState:UIControlStateSelected];
if (backgroundimageSelected != nil)
{
UIImage *stretchImage = [backgroundimageSelected stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateSelected];
}
UIImage *backgroundimageHighlighted = [self backgroundImageForState:UIControlStateHighlighted];
if (backgroundimageHighlighted != nil)
{
UIImage *stretchImage = [backgroundimageHighlighted stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateHighlighted];
}
UIImage *backgroundimageDisabled = [self backgroundImageForState:UIControlStateDisabled];
if (backgroundimageDisabled != nil)
{
UIImage *stretchImage = [backgroundimageDisabled stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateDisabled];
}
}
Seems to work except the button is now not clickable
It seems that the highlighted state causes the issue!
I have removed the highlighted block and it works fine?
If you do not have custom images for highlighted/disabled states, but rely on UIButton to apply the highlight/disabled effect, then [self backgroundImageForState: UIControlStateHighlighted] will NOT return nil. It will instead return a pointer to the normal state image.
Using your code, you are then effectively setting identical images for all states. They look like custom images to the framework, though. This disables the built-in highlight/disabled effects.

iPhone: Button Level

i'm new to iPhone.Is there any way to set 3 images to button, and when I will click the button it will change button image in a circle ? Thanks...
In your IBAction method for the button click, you can keep a count of which image you are on, and also have all 3 UIImages ready to go (so there isn't the load everytime..unless the images are large (which they shouldn't be for a button) and check it and rotate it basically with something simple like (assumes you have 3 instance variables of UIImages ready that are already initialized with the images.:
-(IBAction) myButtonPress:(id)sender {
int imageCounter = 0;
if(imageCounter == 0) {
[myButtonImage setBackgroundImage:image1 forState:UIControlStateNormal];
imageCounter++;
}
else if(imageCounter == 1) {
[myButtonImage setBackgroundImage:image2 forState:UIControlStateNormal];
imageCounter++;
}
else if(imageCounter == 2) {
[myButtonImage setBackgroundImage:image3 forState:UIControlStateNormal];
imageCounter = 0;
}
//Do other button press stuff
}