I'm trying to build a subview that sits above my main view that is approximately 3/4 the size of the main view. When a button is pressed, the subview should flip to another subview. I'm close, but am having TWO problems.
The first problem is that the first time the flip button is pressed, the second subview starts at full screen and shrinks to the specified frame size rather than any flipping animations being rendered.
Once the second view is rendered, I can then flip between the first and second views (I added a second button to flip back for testing purposes).
My second issue is that when the view flips, the new view is loaded before the flip, and then the flipping occurs. I've built flipping views before that have worked correctly, but only when the entire screen was the view.
Here's my code that is in my main view controller:
- (void)viewDidLoad {
Page1Controller *myPage1=[[Page1Controller alloc] initWithNibName:#"Page1Controller" bundle:nil];
Page2Controller *myPage2=[[Page2Controller alloc] initWithNibName:#"Page2Controller" bundle:nil];
self.page1 = myPage1;
self.page2 = myPage2;
[myPage1 release];
[myPage2 release];
[[page1 view] setFrame:CGRectMake(45, 40, 230, 280)];
[[self view] addSubview:[page1 view]];
[super viewDidLoad];
}
And here is the code for the two buttons:
- (void)flipPressed:(id)sender {
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[page2 view] cache:YES];
[page1.view removeFromSuperview];
[[page2 view] setFrame:CGRectMake(45, 40, 230, 280)];
[[self view] addSubview:[page2 view]];
[UIView commitAnimations];
}
- (void)backFlipPressed:(id)sender {
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[page1 view] cache:YES];
[page2.view removeFromSuperview];
[[self view] addSubview:[page1 view]];
[[page1 view] setFrame:CGRectMake(45, 40, 230, 280)];
[UIView commitAnimations];
}
Any help on either of my issues is greatly appreciated!
First problem: Set the frames of your views before beginAnimations.
Related
I am new with animations and I am using following code for Flip animation.
//MyMapView is UIView
MyMapView *aMapView = [[MyMapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300) AndResArray:self.nearMeArray];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[[self view] addSubview:nearMeMap];
[UIView commitAnimations];
Its working fine. But whole view (with super view) Flips.
I only want to Flip subview that is MyMapView. The super view should not flip.
How to do this ???
EDITED
screen :
Below is main screen. When click on Map button I want to Flip only Brown part of the screen. Whole screen should not Flip.
Thanks...
I have figure out what's the problem. Please see the below code.
MapView.m
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
[self performSelector:#selector(addMapView) withObject:nil afterDelay:2.0];
}
return self;
}
- (void) addMapView {
//MyMapView is UIView
MKMapView *aMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 321, 300)];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];
[self addSubview:aMapView];
[UIView commitAnimations];
}
And after that simply add object of MapView class where you want. like below.
//MyMapView is UIView
mapView *aMapView = [[mapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300)];
[[self view] addSubview:aMapView];
vote up if this comment help you.
Thanks,
MinuMaster.
Matteo's answer is partially correct, but you need to set the animation on the view before you add it, or it won't work correctly. Like this:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:aMapView
cache:YES];
[self.view addSubview:aMapView];
[UIView commitAnimations];
Alternatively, you can use the block-based API introduced in iOS 4. Here's an example.
[UIView transitionWithView:aMapView
duration:1.0
options:UIViewAnimationTransitionFlipFromRight
animations:^{ [self.view addSubview:aMapView]; }
completion:^(BOOL f){ }
];
You need to add your view to self.view and then flip only aMapView:
MyMapView *aMapView = [[MyMapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300) AndResArray:self.nearMeArray];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[self.view addSubView:aMapView];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:aMapView cache:YES];
[UIView commitAnimations];
Animated transitions like this animate the contents of a view. The system takes a snapshot of the contents at the beginning of the animation, another snapshot when you commit, and then animates between them. As you've seen with your original code, the entire view gets animated.
The simplest solution is probably just to introduce an intermediate view that's the size and position of where the map will go. Then animate that view as you add the map to it.
More info in the View Programming Guide's section on Creating Animated Transitions Between Views. Though the methods you're using have, in iOS 4, been superceded by new block-based versions.
-(void)viewDidLoad
{
[super viewDidLoad];
OptionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
OptionView.backgroundColor=[UIColor blueColor];
OptionIsShow=false;
MoveView=[[UIView alloc] initWithFrame:CGRectMake(40, 40, 70, 70)];
[MoveView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:MoveView];
}
-(void)ViewOrHideOptionView
{
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.MoveView cache:NO];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0];
if(!OptionIsShow)
{
[self.MoveView addSubview:OptionView];
OptionIsShow=true;
}
else
{
[self.OptionView removeFromSuperview];
OptionIsShow=false;
}
[UIView commitAnimations];
}
I want to know how to change view on button click in iPhone without navigation controller?
If you're doing this with a UIViewController, you would probably do this like following:
- (IBAction)change {
UIViewController* viewController = [[UIViewController alloc] init];
[self.view addSubView];
// Save the UIViewController somewhere if necessary, otherwise release it
}
Not sure why you don't want to use a UINavigationController though. If it's the navigation bar at the top you don't want to see, there's a property for that so you can hide that bar. Not sure about it's name, probably navigationBarHidden or something like that. You can look that up in the API.
There are many different ways you can do that, and you should possibly provide more information about your app.
A generic way to do it is the following, with animation:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[vc1 viewWillDisappear:YES];
[vc2 viewWillAppear:YES];
vc1.view.hidden = YES;
vc2.view.hidden = NO;
[vc1 viewDidDisappear:YES];
[vc2 viewDidAppear:YES];
[UIView commitAnimations];
In this case, both views are there, you only hide/show them as you need. Alternatively, you could add/remove the view from their superview:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[vc1 viewWillDisappear:YES];
[vc2 viewWillAppear:YES];
[vc1 removeFromSuperview];
[masterController.view addSubview:vc2.view;
[vc1 viewDidDisappear:YES];
[vc2 viewDidAppear:YES];
[UIView commitAnimations];
Place this in Button IBAction method
[mCurrentView removeFromSuperview];
[self.view addSubView:mNewView];
Assume you have two view myView1 and myView2 and one button myUIButton in memory
myView1.tag = 1;
myView2.tag = 2;
myUIButton.tag = 1; //Current visible view is myView1
-(void) ButtonCLicked:(id) sender
{
UIButton* myButton = (UIButton*)sender;
if(sender.tag == 1)
{
[myView1 removeFromSuperview];
[self addSubview:myView2]
myButton.tag = myView2.tag;
}
else
{
[myView2 removeFromSuperview];
[self addSubview:myView1]
myButton.tag = myView1.tag;
}
}
You can place two UIView variables in the header file and hide/show one of them or you can make another layer over the existing view like this:
-(IBAction)changeView {
UIView *view = [[UIView alloc] initWithNibName:#"letMeSeeThis" bundle:nil];
[self.view addSubview:view];
}
-(void)CallingView2{
SettingsViewController *aSettingsView = [[SettingsViewController alloc] initWithNibName:#"Settings" bundle:nil];
[self setSettingsViewController:aSettingsView];
[aSettingsView release];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
//setting the animation
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
[self.window addSubview:[settingsViewController view]];
**[[settingsViewController view] setBounds:CGRectMake(0, -30, 320, 480)];**
[UIView commitAnimations];}
I have put the code between the stars in the code where I commit my animation and it works, it moves the view as it should but now the problem is that when i rotate to the view i can see when the view is moving down.
Is it possible to set bounds for the view before it is shown so the user cant see when it is moving 30px down every time he go to settings
Your problem is that you're setting the view bounds within an animation block. The view origin will animate from (0,0) to (0,-30) during the time it takes to perform the flip transition.
Setting your view geometry in the view controller's viewDidLoad would be a better approach.
I have a UITableViewController subclass that has a container UIView which has 1 of 2 subviews: the tableview, or a UIScroll depending on whether or not the view is "flipped".
my viewDidLoad() method adds the tableview to the container:
[kContainerView addSubview: self.tableView];
self.view = kContainerView;
and initializes the UIScrollview with an 900x600 image and sets itself as the delegate:
kSBCanvasScrollView = [[UIScrollView alloc] initWithFrame:bounds];
[kSBCanvasScrollView setDelegate: self];
kSBImage = [[UIImageView alloc] initWithImage:img];
[kSBCanvasScrollView addSubview:kSBImage];
[kSBCanvasScrollView setContentSize:[img size]];
[kSBCanvasScrollView setMinimumZoomScale:0.5];
The the front "Table side" of the view works fine. I then flip the view when the user selects a button on the toolbar by doing the following:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationDelegate:self];
if (kSBCanvasIsVisible==NO) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:kContainerView cache:YES];
[self.tableView removeFromSuperview];
[kContainerView addSubview:kSBCanvasScrollView];
kSBCanvasIsVisible = YES;
} else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:kContainerView cache:YES];
[kSBCanvasScrollView removeFromSuperview];
[kContainerView addSubview:self.tableView];
kSBCanvasIsVisible = NO;
}
[UIView commitAnimations];
It flips just like I want, but the kSBCanvasScrollView does not scroll. The pinch gesture works. Why won't the view scroll? Perhaps it relates to the fact this is a TableViewController?
Is your scrollview 320 x 460? I had to make the scroll view the same , or smaller than the iphone screen for it to work(In IB but not necessarily in the
[kSBCanvasScrollView setContentSize:[img size];).
Did you write:
[kSBCanvasScrollView setScrollEnabled:YES];
PS: I did try this. It worked for me...
I'm having some trouble getting a view to flip. I have the following code in my View Controller:
- (void)loadFlipsideViewController {
ProblemViewFlipController *viewController = [[ProblemViewFlipController alloc] initWithNibName:#"ProblemViewFlip" bundle:nil];
self.problemViewFlipController = viewController;
[viewController release];
}
- (void) flipView {
if (problemViewFlipController == nil) {
[self loadFlipsideViewController];
}
UIView *mainView = self.view;
UIView *flipView = problemViewFlipController.view;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) forView:self.view cache:YES];
if ([flipView superview])
{
[flipView removeFromSuperview];
[self.view addSubview:mainView];
}
else
{
[mainView removeFromSuperview];
[self.view addSubview:flipView];
}
[UIView commitAnimations];
}
The problem is, is that when I call flipView, the view is replaced with a blank view (i.e. nothing in the view I'm flipping to is displayed).
Is there something obvious I'm missing here? (I suspect there is!)
Not positive, but I think you need to use a 'controller' to flip the views. Looks like you're using one of the flipped views as the controller. Just add a root controller to flip your views.
Code like this should work from the root controller:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; // self.view is the root controller's view
[mainViewController viewWillAppear:YES];
[flipViewController viewWillDisappear:YES];
[flipViewController.view removeFromSuperview];
[self.view addSubview:mainViewController.view];
[flipViewController viewDidDisappear:YES];
[mainViewController viewDidAppear:YES];
[UIView commitAnimations];
Based on the variable names, it looks like this is adapted from the default "Utility" application template you get from XCode. If the template project works, and yours doesn't, then you've obviously changed something you shouldn't have :-)
Chances are, one of the outlets in your controller that should be pointing at a view, isn't. Double-check your Nibs, and check the values of the outlets in the debugger. If all else fails, start over again from the template, and see at what point it stops working.