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;
}];
Related
My goal is simple and I have achieved this previously which is why I'm resorting to asking my question here.
I am trying to animate a subview. Simple enough. The view ends up exactly where I want it to be after the animation is over. The only problem is:
whatever I do the animation duration is completely ignored. It happens instantly.
- (void)callToDismissView:(UIView *)view {
[self.view addSubview:view];
[self dismissViewControllerAnimated:NO completion:nil];
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
CGRect frame = self.view.frame;
frame.origin.x = frame.size.width;
view.frame = frame;
} completion:^(BOOL finished){
[view removeFromSuperview];
}];
}
Now I have tried different frames and durations to see if the view was animated at all and it always ends up where I need it to be. Only instantly...
EDIT:
The above code is being called by a delegate in the dismissed view controller like so:
- (void)dismissSelf {
if (self.delegate && [self.delegate respondsToSelector:#selector(callToDismissScan)]) {
[self.delegate callToDismissScan];
}
}
And that method itself is triggered by a notification.
I must admit that some things are happening during the transition between the two controllers that I don't fully understand. (And I would very much like to understand them...)
You can try
[self.view layoutIfNeeded];
like this
- (void)callToDismissView:(UIView *)view {
[self.view addSubview:view];
[self dismissViewControllerAnimated:NO completion:nil];
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
CGRect frame = self.view.frame;
frame.origin.x = frame.size.width;
view.frame = frame;
[self.view layoutIfNeeded];
} completion:^(BOOL finished){
[view removeFromSuperview];
}];
}
Maybe you need try like this.
- (void)callToDismissView:(UIView *)view {
[self.view addSubview:view];
CGRect frame = self.view.frame;
frame.origin.x = frame.size.width;
[UIView animateWithDuration:0.3 delay:0.3
options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
view.frame = frame;
}
completion:^(BOOL finished){
[view removeFromSuperview];
//[self dismissViewControllerAnimated:NO completion:nil];//uncomment if need.
}];
}
It seems there was no way in hell I could animate the transition in the presenting view controller and so the simplest way to resolve the issue was to animate the transition in the presented view controller before it is being dismissed.
The way I did this was by casting the delegate to a view controller. It was then easy to add it's view as a sub-view of my presented view controller and animate it like so:
- (void)dismissSelf {
UIView *dummyView = [ScreenCapper captureScreenIn: self];
UIViewController *presentingViewController = (UIViewController *)self.delegate;
[self.view.window addSubview: presentingViewController.view];
[self.view.window addSubview: dummyView];
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
dummyView.frame = CGRectMake(dummyView.frame.size.width, dummyView.frame.origin.y, dummyView.frame.size.width, dummyView.frame.size.height);
} completion:^(BOOL finished){
if (self.delegate && [self.delegate respondsToSelector:#selector(callToDismissView)]) {
[self.delegate callToDismissView];
}
}];
}
And in the presenting view controller all that was left to do was to dismiss the presented view controller without animations.
- (void)callToDismissView {
[self dismissViewControllerAnimated:NO completion:nil];
}
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 am trying to go from one view controller to another with the use of page curl up animation; but when my second controller loads, it hides the collection view from it. This is my code:
OffersVC *Offers = [[OffersVC alloc]init];
Offers = [self.storyboard instantiateViewControllerWithIdentifier:#"Offers"];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self.view addSubview:Offers.view];
[UIView commitAnimations];
Try to add animation as below and check
UIView *mainview = self.view.window;
OffersVC *Offers = [[OffersVC alloc]init];
Offers = [self.storyboard instantiateViewControllerWithIdentifier:#"Offers"];
[UIView transitionWithView:mainview
duration:kAnimationViewTransition
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[self.navigationController.view removeFromSuperview];
[mainview addSubview: offers.view];
}
completion:^(BOOL finished) {
[self presentModalViewController:offers animated:NO];
}
];
I'm trying to flip animate a view into another view, which corresponds of a small section of the screen. Both views have the same dimensions and center.
I keep getting as a result the animation of the full screen. From the code below, can somebody please let me know what the heck i'm doing wrong?
Thank you,
-j
+ (void) flipView:(UIView*)viewA toView:(UIView*)viewB wait:(Boolean)wait
{
// get parent view
UIView *parent = [viewA superview];
CGRect r = viewA.frame;
// create container view with the same dimensions as ViewA
UIView *containerView = [[UIView alloc] initWithFrame:viewA.bounds];
containerView.center = viewA.center;
// attache both views to intermidiate view
[containerView addSubview:viewA];
[containerView addSubview:viewB];
[viewA removeFromSuperview];
[parent addSubview:containerView];
viewB.alpha = 0;
viewA.alpha = 1;
// Perform the annimation
__block BOOL done = NO;
[UIView transitionWithView:viewA
duration:2.0
options: (UIViewAnimationOptionTransitionFlipFromTop)
animations:^{
viewA.alpha = 0;
viewB.alpha = 1; }
completion:^(BOOL finished) {
done = YES;
// detach all views
[viewA removeFromSuperview];
[viewB removeFromSuperview];
[containerView removeFromSuperview];
}
];
if(wait) {
while (done == NO)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
}
First get rid of the old animation code (or at least commit it), it should look like this:
+ (void) flipView:(UIView*)viewA toView:(UIView*)viewB wait:(BOOL)wait
{
viewB.alpha = 0;
viewA.alpha = 1;
__block BOOL done = NO;
[UIView transitionWithView:viewA
duration:2.0
options: (UIViewAnimationOptionTransitionFlipFromTop)
animations:^{
viewA.alpha = 0;
viewB.alpha = 1; }
completion:^(BOOL finished) {
done = YES;
}
];
if(wait) {
while (!done)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
}
Second, the superview of the subview that you want to transition is actually going to get the transition. So this means that you'll have to add viewA and viewB to a subview, add this subview to another view and then do the animation.
Here is a solution without any hacks with runloop. Just use the first message to run a transition to another view, and the second message to return to original state:
- (void) forwardTransitionFromView: (UIView *) viewA toView: (UIView *) viewB
{
NSAssert(viewA.superview, #"The 'from' view should be added to a view hierarchy before transition");
NSAssert(!viewB.superview, #"The 'to' view should NOT be added to a view hierarchy before transition");
CGSize viewASize = viewA.bounds.size;
viewB.frame = CGRectMake(0.0, 0.0, viewASize.width, viewASize.height);
[UIView transitionWithView:viewA
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[viewA addSubview:viewB];
}
completion:NULL];
}
- (void) backwardTransitionFromView: (UIView *) viewB toView: (UIView *) viewA
{
NSAssert([viewB.superview isEqual:viewA], #"The 'from' view should be a subview of 'to' view");
[UIView transitionWithView:viewA
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[viewB removeFromSuperview];
}
completion:NULL];
}
Why dont you flip both views in same direction?
-(void) FlipView:(bool) fromRight {
if(fromRight) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view1 cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view2 cache:YES];
[UIView commitAnimations];
}
else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view1 cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view2 cache:YES];
[UIView commitAnimations];
}
}
and call the method like below:
[view1 setHidden:NO];
[self FlipView:YES];
[view2 setHidden:YES];
to reverse the animation:
[view2 setHidden:NO];
[self FlipView:NO];
[view1 setHidden:YES];
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