I need to flag the icon either favourite or unfavourite when the user return to this VC.
Can someone please give some pointers. Thank you.
Answer
1st Go to fa2png.io to create a 20px or 30px icon. Save it and import into assets.xcassets
.
Then the code
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIButton *heart = [UIButton buttonWithType:UIButtonTypeCustom];
BOOL favorite = YES;
if (favorite)
{
[heart setImage:[UIImage imageNamed:@"favorite.png"] forState:UIControlStateNormal];
}else{
[heart setImage:[UIImage imageNamed:@"notFavorite.png"] forState:UIControlStateNormal];
}
[heart addTarget:self
action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton =
[[UIBarButtonItem alloc] initWithCustomView:heart];
self.navigationItem.rightBarButtonItem = jobsButton;
}
-(void)buttonTapped:(UIButton*)heart
{
if( [[heart imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"notFavorite.png"]])
{
NSLog(@"favorite.png");
[heart setImage:[UIImage imageNamed:@"favorite.png"] forState:UIControlStateNormal];
[UIView animateKeyframesWithDuration:0.5
delay:0.0
options:0.0
animations:^{
// Heart expands
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.10 animations:^{
heart.transform = CGAffineTransformMakeScale(1.3, 1.3);
}];
// Heart contracts.
[UIView addKeyframeWithRelativeStartTime:0.15 relativeDuration:0.25 animations:^{
heart.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
} completion:nil];
}
else
{
NSLog(@"notFavorite.png");
[heart setImage:[UIImage imageNamed:@"notFavorite.png"] forState:UIControlStateNormal];
[UIView animateKeyframesWithDuration:0.5
delay:0.0
options:0.0
animations:^{
// Heart expands
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.10 animations:^{
heart.transform = CGAffineTransformMakeScale(1.3, 1.3);
}];
// Heart contracts.
[UIView addKeyframeWithRelativeStartTime:0.15 relativeDuration:0.25 animations:^{
heart.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
} completion:nil];
}
}
[![!\[Pic][2]][2]