I am currently working through a beginners guide to programming for ios6. It's been fine until just now when I tried to animate switching back and forth between two views. The final goal of the exercise was to have it seem as if each view was on the back of the other (like the sides of a coin/piece of paper).
However, when I use the code given in the book, only one of the animations activates but the book says that the code should work for both.
I have been over my code multiple times to make sure that I have done it right and I have not been able to distinguish a difference between the code that I have and the code that is in the book. I know that it's something simple that I am doing (or more likely not doing) but I just don't have the experience to find it.
Any help would be very much appreciated.
Code:
- (IBAction)switchViews:(id)sender
{
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if (self.yellowViewController.view.superview == nil) {
if (self.yellowViewController == nil) {
self.yellowViewController = [[BIDYellowViewController alloc] initWithNibName:#"YellowView" bundle:nil];
}
// This one doesn't work
[UIView setAnimationTransition:UIViewAnimationOptionTransitionFlipFromRight forView:self.view cache:YES];
[self.blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
}
else
{
if (self.blueViewController == nil) {
self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:#"BlueView" bundle:nil];
}
// This one works
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[self.yellowViewController.view removeFromSuperview];
[self.view insertSubview:self.blueViewController.view atIndex:0];
}
[UIView commitAnimations];
}
Its because you use UIViewAnimationOptionTransitionFlipFromRight instead of UIViewAnimationTransitionFlipFromRight
Related
Update, SOLVED
Sorry for my english, but i will do my best.
I have a problem with a button flip animation.
I want the UIButtons to fill the UIView. But how do i do that?
Here is my problem.
I have two UIButtons in a UIView.
When i press the first button it will flip correctly but when i press the second button that it have flipped to the UIView is still the same size but the UIButton image is changing size to the original size of the image and it also move the image to the upper left corner off the view controller. The same thing will repeat every time it flip.
Here is the flip animation code.
- (IBAction) buttonPressedFlip {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
if (flipState == 0) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:buttonContainer cache:YES];
[self.btn1 removeFromSuperview];
[buttonContainer addSubview:btn2];
flipState = 1;
}
else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:buttonContainer cache:YES];
[self.btn2 removeFromSuperview];
[buttonContainer addSubview:btn1];
flipState = 0;
}
[UIView commitAnimations];
}
Have a nice day and thanks for any help!
I solved the problem like this
- (IBAction) buttonPressedFlip {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
if (flipState == 0) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:buttonContainer cache:YES];
[self.buttonContainer bringSubviewToFront:btn2];
flipState = 1;
}
else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:buttonContainer cache:YES];
[self.buttonContainer bringSubviewToFront:btn1];
flipState = 0;
}
[UIView commitAnimations];
}
I solved the problem
See my update code.
I'm building an iPad app and want to switch one of my subviews for another by pressing a button. I've been reading the documentation and this guide:
View Controllers: How to switch between views programmatically?
But I keep getting errors and the animations doesn't really happen, the views just switch immediately.
I'll paste some code below for context. I have a main view which has in it a sub uiview. In the sub uiview, there is another view. I want to swap the other view with another.
self.settingsViewController = [[GHPSettingsViewController alloc] initWithNibName: #"GHPSettingsViewController" bundle: nil andParent: self];
UIView *outerView = [self.containingView viewWithTag:0];
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration: 100.25f];
[UIView setAnimationDuration: UIViewAnimationCurveEaseInOut];
if (self.settingsViewController.view.superview == nil) {
// Flip Out Old View
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.containingView cache:YES];
[self.settingsViewController viewWillAppear: YES];
[outerView removeFromSuperview];
[self.containingView insertSubview: self.settingsViewController.view atIndex: 0];
[self.settingsViewController viewDidAppear: YES];
}
[UIView commitAnimations];
I am new to programming, I have been searching all night long and tyring to figure this out for a long time. Cant.... I dont want to use a navigation Controller but what I am trying to do is this.
So I have two classes in my project. Class1 and Class2. Now there is a button in Class1. If this button is pressed, I want the screen to go to Class2. The closest I got was creating a parent class but when I do that, the button remains on both classes.
- (IBAction)addScheduleB:(id)sender {
[UIView beginAnimations:#"View Curl" context:nil];
[UIView setAnimationDuration:1.00];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if (self.firstScreen.view.superview ==nil){
if(self.firstScreen==nil) {
self.firstScreen = [[FirstScreen alloc] initWithNibName:#"FirstScreen" bundle:nil];
}
[self.config1.view removeFromSuperview];
[self.view insertSubview:self.firstScreen.view atIndex:0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[self.config1.view removeFromSuperview];
[self.view insertSubview:self.firstScreen.view atIndex:0];
[sender setTitle:#"Switch To DVR"];
}
else{
if(self.config1 == nil){
self.config1 = [[Config1 alloc] initWithNibName:#"Config1" bundle:nil];
}
[self.firstScreen.view removeFromSuperview];
[self.view insertSubview:self.config1.view atIndex:0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self.firstScreen.view removeFromSuperview];
[self.view insertSubview:self.config1.view atIndex:0];
[sender setTitle:#"Switch To TV"];
}
[UIView commitAnimations];
}
I want to remove the parent class and have just the two classes.
Is it possible to have a button inside class1 when pushed will take me to class2???
There is a 7 minute tutorial on YouTube Here. That shows how to move from one view to another using a button without storybord so you will be able to lern the code to move from one class to another.
I hope this helps. As for being a beginner if you look around youtube you will find lots of tutorials to help you learn. :)
EDIT: this does not use a navigation controller
i hope This solution helps You
just make object from Class2 and when Button1 Pressed add Class2's object.View as subview to Class1.view
Good Luck
Try something like :
- (IBAction)flipViews {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.6f];
if ([saisieCompleteView superview])
{
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[saisieCompleteView removeFromSuperview];
[self.view addSubview:saisieRapideView];
[self.view sendSubviewToBack:saisieCompleteView];
}
else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[saisieRapideView removeFromSuperview];
[self.view addSubview:saisieCompleteView];
[self.view sendSubviewToBack:saisieRapideView];
}
[UIView commitAnimations];
}
The both saisieCompleteView and saisieRapideView must be subview of your principal view.
you may pop the another view with modal flip transition:
[self presentViewController:Class2 animated:YES completion:^{
// Some code on completeion
}];
to dismiss the Class2 view:
[self dismissViewControllerAnimated:YES completion:^{
//code
}];
if you don't want to go out of current view controller, you may flip it this way:
BOOL toShowSecond = YES;
...
[UIView transitionWithView:self.view
duration:1.0
options:(toShowSecond?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight)
animations:^{
if(toShowSecond) {
firstView.hidden = YES;
secondView.hidden = NO;
} else {
firstView.hidden = NO;
secondView.hidden = YES;
}
}
completion:^(BOOL finished){
toShowSecond = !toShowSecond;
}];
I have a UIView and I am animating it.
The thing here is that I need to release theView once it is animated out.
originalRECT = [[UIScreen mainScreen] bounds];
if(theView)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
theView setFrame = originalRECT;
[UIView commitAnimations];
[theView autorelease];
theView = nil;
}
So I know the code is setting theView to nil, but the animation does finish ok (No SIGABRT or something like that)
Or is there a callback function I can use to know that the view disappeared? How can I use a function like that in this case?
Thanks!
Few things -
First of all if you want to be sure you can use:
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^ {
//your animation
theView setFrame = originalRECT;
}
completion:^(BOOL finished) {
//Animation finished you can release
[theView release];
}];
second - why do you use autoRelease and not release?
[theView release];
Third - Just for w meeter you don't know. USE ARC it will make your life a bit easier. You can read more here ARC tutorial
I am creating an ePub reader, which reads ePub files. I want to implement a page slide effect similar to a physical book. For example.
What animation is this? This doesn't work:
- (void)loadPage{
[UIView beginAnimations:nil context:nil];
//change to set the time
[UIView setAnimationDuration:1];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
// do your view swapping here
[UIView commitAnimations];
}
on swipe:
- (void)swipeRightAction:(id)ignored
{
NSLog(#"Swipe Right");
if (_pageNumber>0) {
_pageNumber--;
[self loadPage];
}
}
- (void)swipeLeftAction:(id)ignored
{
NSLog(#"Swipe Left");
if ([self._ePubContent._spine count]-1>_pageNumber) {
_pageNumber++;
[self loadPage];
}
}
I have done the same with third party api. You can take refrence from below URL
https://github.com/jemmons/PageCurl
https://github.com/xissburg/XBPageCurl
https://github.com/brow/leaves