Reputation: 13
Im trying to create a flashcard app, I tried this code but its displays only the first image.
Any one can help me?
@implementation CardsViewController
- (IBAction)Flip:(UIButton*)sender
{
if ([sender.currentImage isEqual:[UIImage imageNamed:@"a"]])
{
[sender setBackgroundImage:[UIImage imageNamed:@"b"]
forState:UIControlStateNormal];
}
else
{
[sender setBackgroundImage:
[UIImage imageNamed:@"a"] forState:UIControlStateNormal];
}
}
Upvotes: 1
Views: 598
Reputation: 8402
hear is the simple way
hope this helps u ..
@interface ViewController ()
{
BOOL ButtonClicked;//to trake change images ..
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
ButtonClicked = NO;
// Do any additional setup after loading the view, typically from a nib.
[self.button setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal]; //change the type of button to custom in the xib
}
- (IBAction)whenClicked:(id)sender
{
if(ButtonClicked)
{
ButtonClicked = NO;
[UIView animateWithDuration:0.8 animations:^{
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.button cache:YES];
[self.button setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
}];
}
else
{
ButtonClicked = YES;
[UIView animateWithDuration:0.8 animations:^{
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.button cache:YES];
[self.button setImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateNormal];
}];
}
}
Upvotes: 0
Reputation: 2072
Instead of using background image, one way you can achieve the flip effect is by using the UIButton states and, set one of your images as default image for the 'Normal' state and the other image as default image for the 'Selected' state; you could even set those images at design time and then only change the state of your button through your method Flip which will automatically change the image shown to the user.
Please note that for each button state you can set two images, a default image and a background image. You don't even need/have to set a background image or keep changing it every time the button is selected.
@implementation CardsViewController
- (IBAction)Flip:(UIButton*)sender
{
if ([sender isSelected])
{
[sender setSelected:NO];
}
else
{
[sender setSelected:YES];
}
}
If you want or need to set your images through code, add these lines in a method that fires one time only, typically ViewDidLoad:
[yourButtonInstance setImage:[UIImage imageNamed:@"a"] forState:UIControlStateNormal];
[yourButtonInstance setImage:[UIImage imageNamed:@"b"] forState:UIControlStateSelected];
Upvotes: 0
Reputation: 3543
I think you should create a View contain two subviews, then just transition them ,[UIView transitionFromView:toView:duration:options:completion:]
code snippet:
UIButton *containerView = [UIButton buttonWithType:UIButtonTypeRoundedRect];
containerView.frame = CGRectMake(100.0, 100.0, 120.0, 120.0);
[containerView addTarget:self action:@selector(transition:) forControlEvents:UIControlEventTouchUpInside];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:containerView.bounds];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:containerView.bounds];
imageView1.image = [UIImage imageNamed:@"Sample1"];
imageView2.image = [UIImage imageNamed:@"Sample2"];
imageView1.tag = 1;
imageView2.tag = 2;
[containerView addSubview:imageView1];
[containerView addSubview:imageView2];
[self.view addSubview:containerView];
- (void)transition:(UIButton *)sender {
UIImageView *imageView1 = (UIImageView *)[sender viewWithTag:1];
UIImageView *imageView2 = (UIImageView *)[sender viewWithTag:2];
static NSInteger index = 0;
index++;
if (index % 2 == 0) {
[UIView transitionFromView:imageView1 toView:imageView2 duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews completion:NULL];
} else {
[UIView transitionFromView:imageView2 toView:imageView1 duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews completion:NULL];
}
}
Upvotes: 0
Reputation: 37290
You're setting the background image, but using the "currentImage," i.e. foreground image as the comparison.
Try replacing:
if ([sender.currentImage isEqual:[UIImage imageNamed:@"a"]])
with
if ([sender.currentBackgroundImage isEqual:[UIImage imageNamed:@"a"]])
Upvotes: 1