I'm new to cocoa and iphone programming and I want to implement an animation in UIButton. For example, I create a custom UIButton with a square image. Then when I press that UIButton, the square image will flip. Note that the square image is the image of the UIButton.
[UIButton setImage:[UIImage imageNamed:#"square.png"]];
I had the same problem and I solved it in a very similar way. I just subclassed the UIButton and implemented a method like that:
- (void) flipBackgroundImage:(UIImage*) image
{
[UIView beginAnimations:#"flipbutton" context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];
[self setBackgroundImage:image forState:UIControlStateNormal];
[UIView commitAnimations];
}
Works like a charm.
Cheers,
anka
Below is the complete code for flipping a button (with background image) when it will pressed.
Basically you need two button and a container view.
///// .H file code....
//Container views used for flipping the bars to show whose turn it is
UIButton* btn1;
UIButton* btn2;
UIView *BarContainerView;
///// .M file code....
- (void)viewWillAppear:(BOOL)animated
{
BarContainerView = [[UIView alloc] initWithFrame:CGRectMake(20, 30, 103, 150)];
btn1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain];
[btn1 addTarget:self action:#selector(btn1_click) forControlEvents:UIControlEventTouchUpInside];
[btn1 setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
btn1.frame = CGRectMake(0, 0, 103, 150);
btn2 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain];
[btn2 addTarget:self action:#selector(btn2_click) forControlEvents:UIControlEventTouchUpInside];
[btn2 setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateNormal];
btn2.frame = CGRectMake(0, 0, 103, 150);
[BarContainerView addSubview:btn1];
[self.view addSubview:BarContainerView];
[self.view bringSubviewToFront:BarContainerView];
}
- (void) btn1_click
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:BarContainerView cache:YES];
[btn1 removeFromSuperview];
[BarContainerView addSubview:btn2];
[UIView commitAnimations];
}
- (void) btn2_click
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:BarContainerView cache:YES];
[btn2 removeFromSuperview];
[BarContainerView addSubview:btn1];
[UIView commitAnimations];
}
Related
I have an AlertView with an imageView and I have to show the image in flip animation (UIViewAnimationTransitionFlipFromLeft/Right) as a subview of alert view
I am using the following method
- (void) setAnimationFlip:(UIAlertView*)alertView {
UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(20, 40, 150, 150)];
UIImageView *mainView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"scene1.jpg"]];
UIImageView *flipToView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"scene2.jpg"]];
mainView.frame = CGRectMake(20, 40, 200, 210);
flipToView.frame = CGRectMake(20, 40, 200, 210);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
[UIView setAnimationTransition:([mainView superview] ?
UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
forView:containerView cache:YES];
if ([flipToView superview])
{
[flipToView removeFromSuperview];
[containerView addSubview:mainView];
}
else
{
[mainView removeFromSuperview];
[containerView addSubview:flipToView];
}
[self.alrtViewPreview addSubview:containerView];
[self.alrtViewPreview show];
[UIView commitAnimations];
//[containerView release];
//[mainView release];
//[flipToView release];
}
and calling like...
switch (iEffect) {
case 1:
[self setAnimationFlip:alrtViewPreview];
break;
}
but it is not working for me, can any one help me?
-(IBAction)btnClick:(UIButton*)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:sender cache:YES];
[UIView setAnimationDelegate:self];
[sender setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",self.tileImage]] forState:UIControlStateNormal];
[sender setTitle:self.tileText forState:UIControlStateNormal];
[UIView commitAnimations];
}
I have a white image button. when I click, it should change into yellow button image by animating from Bottom to top and if I click yellow button image again it must animate top to bottom and change as white image button image.
This is what I tried:
float originalY = btn.frame.origin.y;
float originalH = btn.bounds.size.height;
[UIView animateWithDuration:3.0f delay:1.0f options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
btn.frame = CGRectMake(btn.frame.origin.x, (originalY + originalH), btn.bounds.size.width, 0);
[btn setBackgroundImage:[UIImage imageNamed:#"yellowimg.png"] forState:UIControlStateNormal];
[btn setTitleColor:[[UIColor alloc]initWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0] forState:UIControlStateNormal];
}
completion:^(BOOL finished) {
}];
btn.frame = CGRectMake(btn.frame.origin.x, (originalY + originalH), btn.bounds.size.width, 0);
This method will make your button with height = 0.
If you want to curl (flip) your button and to have 'new' button which appeared from the first as I know you should use something like that
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:firstView cache:YES];
[firstView addSubview:secondView];
[UIView commitAnimations];
I am working in an app in which i have used the GMGridview. I have added the buttons in cellforItems method. -
(GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
NSLog(#"Creating view indx %d", index);
CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
GMGridViewCell *cell = [gridView dequeueReusableCell];
if (!cell)
{
cell = [[GMGridViewCell alloc] init];
cell.deleteButtonIcon = [UIImage imageNamed:#"close_x.png"];
cell.deleteButtonOffset = CGPointMake(-15, -15);
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:index];
[btn setFrame:CGRectMake(0, 0,size.width, size.height)];
NSLog(#"button frame is %#",btn);
[[btn layer] setBorderColor:[UIColor redColor].CGColor];
[[btn layer]setBorderWidth:2.0f];
[btn addTarget:self action:#selector(SegmentChange:) forControlEvents:UIControlEventTouchUpInside];
cell.contentView = btn;
//int btntag=btn.tag;
return cell;
then I have used the method to Flip all the buttons,but its not working , What i want is when i click on flip button all the buttons in the gridview should flip.how can i do this ,the code for flip i use is here,
for(ll=0;ll<[Image_array count];ll++)
{
// [UIView setAnimationDelay:2.0];
[UIView beginAnimations:#"BackButton" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:12.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:btn cache:YES];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
[btn setImage:[UIImage imageNamed:nil] forState:UIControlStateNormal];
[arr_check replaceObjectAtIndex:ll withObject:#"uncheck"];
front = NO;
}
[_gmGridView reloadData];
As the FOR loop is very much faster comparing to UIViewAnimation. So even if it is animating your view, you will not able to see the animation effect.
You can use NSTimer Or UIViewAnimation delegate method to determine if the one view flip event is completed or not. Then from the delegate method(of UIViewAnimation) you can start another views animation.
i have removed the code from for loop and used the same code in the cellForItemAtIndexpath and use the cell to flip than the button
here is the code :
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:cell cache:YES];
[UIView setAnimationDuration:0.7];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
I'm newbie at iOS development and this is my first question on SO.
I want to create "animation".
When I tap UIButton I want to slowely (about 0.5 seconds) move it down for 10px and when I raise my finger I want to open new viewController.
I made something but I don't know how to make "animation".
UIImage *normalImage = [UIImage imageNamed:#"FirstImage"];
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
firstButton.frame = CGRectMake(31, 194, normalImage.size.width, normalImage.size.height);
[firstButton setImage:normalImage forState:UIControlStateNormal];
[firstButton addTarget:self action:#selector(showSecondViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:firstButton];
- (void)showSecondViewController; {
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:secondViewController animated:YES];
[progressViewController release];
}
[UIView animateWithDuration:(NSTimeInterval)duration animations:^(void) {
//put your animation result here
//then it will do animation for you
} completion:^(BOOL finished){
//do what you need to do after animation complete
}];
for example:
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//add firstButton to some view~
//....
firstButton.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:0.5 animations:^(void) {
firstButton.transform = CGAffineTransformMakeTranslation(0,10);
} completion:^(BOOL finished){
show your view controller~
}];
[UIView animateWithDuration:0.5 animations: ^ {
button.frame = CGRectMake:(button.frame.origin.x, button.frame.origin.y + 10.0f, button.frame.size.width, button.frame.size.height);
}];
You can put transformations(repositioning), scaling and rotatations between beginAnimations and commitAnimations blocks where you can specify the AnimationRepeatCount,AnimationCurve etc.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:0.5];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:2];
..........................//etc.
firstButton.frame = CGRectMake(31, 194, normalImage.size.width, normalImage.size.height);
[UIView commitAnimations];
But in iOS4 and later animations are encouraged to be performed by
animateWithDuration:delay:options:animations:compeletion and similar methods that invlove blocks which are very powerful. For more information on block animations please refer to apple documentation
I am trying to put together an app that has a set of text that can be randomized when the user presses the button to flip the page. Basically, pressing the button does two things; flip the page and changes the text to a randomly included phrase.
I am trying to implement this by having the page curl into the same XIB file and just have the text change. The page curl works but it is not changing the text. Not sure what I am doing wrong here
Here is my code:
#implementation InfoViewController
#synthesize isViewPushed;
// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
if(isViewPushed == NO) {
{ UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(200, 300, 100, 50);
[btn setTitle:#"Next" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(cancel_Clicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
{ UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(20, 300, 100, 50);
[btn setTitle:#"Previous" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(next_clicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
int text = rand() % 5;
switch (text) {
case 0:
textview.text = #"Hello 1" ;
break;
case 1:
textview.text = #"Hello 2" ;
break;
case 2:
textview.text = #"Hello 3" ;
break;
case 3:
textview.text = #"Hello 4" ;
break;
case 4:
textview.text = #"Hello 5" ;
break;
}
}
}
-(void) cancel_Clicked:(id)sender {
[self.navigationController dismissModalViewControllerAnimated:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[UIView commitAnimations];
}
-(void) next_clicked:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView commitAnimations];
}
Any help would be so great.
I don't see where you are setting the text value here:
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
...set or update text value here....
[UIView commitAnimations];
Or after
[UIView commitAnimations];
...set or update text value here....