Hitting buttons while UIView is in transition, flipping in or out - iphone

My son just discovered that he could hit my view's "Done" button, which flips to the previous view, once to start the flip and one or more times as the view is in transition (flipping out). The second and next hits would trigger the same action over again, creating some interesting results, such as ending up with no views visible, but my underlying UIWindow.
I am thinking that I should call:
[coming.view setUserInteractionEnabled: NO];
[going.view setUserInteractionEnabled: NO];
on both the views involved in the flip transition, and then
[coming.view setUserInteractionEnabled: YES];
on the final view after animation has ended.
I was thinking that maybe better than this would be some way to globally disable taps while any view was in transition. What do you think?
Here is the entire view switching code:
- (void)switchTwoViews:(UIViewController *)view1 otherView:(UIViewController *)view2
{
/*
This method is called to switch views.
It flips the displayed view from the main view to the flipside view and vice-versa.
*/
UIViewController *coming = nil;
UIViewController *going = nil;
UIViewAnimationTransition transition;
[view1.view setUserInteractionEnabled: NO];
[view2.view setUserInteractionEnabled: NO];
if (view1.view.superview == nil) {
coming = view1;
going = view2;
transition = UIViewAnimationTransitionFlipFromLeft;
}
else {
coming = view2;
going = view1;
transition = UIViewAnimationTransitionFlipFromRight;
}
// coming.view.frame = [UIScreen mainScreen].applicationFrame;
// going.view.alpha = 1.0; //uncomment these lines if we want fading of views
// coming.view.alpha = 0.0;
NSArray *viewArray = [[NSArray alloc] initWithObjects:coming, going, nil];
[coming viewWillAppear:YES];
[going viewWillDisappear:YES];
[UIView beginAnimations:#"View Flip" context:viewArray]; {
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidEnd:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// coming.view.alpha = 1.0; //uncomment these lines if we want fading of views
// going.view.alpha = 0.0;
[UIView setAnimationTransition:transition forView:self.view cache:YES];
[self.view addSubview: coming.view];
}
[UIView commitAnimations];
}
- (void) animationDidEnd:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
NSArray *viewArray = context;
[((UIViewController *)[viewArray objectAtIndex:1]).view removeFromSuperview];
[[viewArray objectAtIndex:1] viewDidDisappear:YES];
[[viewArray objectAtIndex:0] viewDidAppear:YES];
[[[viewArray objectAtIndex:0] view] setUserInteractionEnabled: YES];
[viewArray release];
}

setUserInteractionEnabled: seems to be the way to go. Any objections?

Related

How to change view on button click in iPhone without navigation controller?

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];
}

How to hide tabbar and navigationbar when tapped on the view

I was building an articles based application,i am having tab bar and navigation bar in my application.What i want to do is while my article is displaying when i tap on that view,the tabbar and navigationbar should hide and viceversa.
Please help me in this
Thanks for the responses,Finally i have used gesture method
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleDoubleTap)];
doubleTap.numberOfTapsRequired =1;
[scrollView addGestureRecognizer:doubleTap];
[doubleTap release]; - (void)handleDoubleTap {
NSLog(#"Single tap");
if(firstTime)
{
[UIView beginAnimations: #"moveField"context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[self.navigationController setNavigationBarHidden:YES animated:YES];
scrollView.frame = CGRectMake(0,0,320,460.0f);
tabBar.hidden=YES;
[self.view bringSubviewToFront:scrollView];
[UIView commitAnimations];
firstTime=NO;
}
else {
[UIView beginAnimations: #"moveField"context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
//[self.view bringSubviewToFront:scrollView];
[self.navigationController setNavigationBarHidden:NO animated:YES];
scrollView.frame = CGRectMake(0.0, 0.0, 320, 372);
tabBar.hidden=NO;
[UIView commitAnimations];
firstTime=YES;
}
Now iam getting this when i tap on the screen...Is it possible to hide the tabbar and navigationbar wheni scroll the view and vice versa....Please help me in this
Use one BOOL variable i called it firstTime in .h file
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if(firstTime)
{
[self.navigationController hidesBottomBarWhenPushed];
firstTime=FALSE;
self.navigationController.navigationBar.hidden=TRUE;
}
else
{
[self.navigationController hidesBottomBarWhenPushed];
firstTime=TRUE;
self.navigationController.navigationBar.hidden=FALSE;
}
}
- (BOOL)hidesBottomBarWhenPushed
{
if(firstTime)
{
return TRUE;
}
else
{
return FALSE;
}
}
this will hide your Tabbar and NavigationBar on single tap and viceversa
int DEF_BAR_HEIGHT = 40;
-(void)hideBars:(bool)value
{
// hide navigation bar
[self.navigationController setNavigationBarHidden:value animated:YES];
//hide tabBar
CGRect viewFrame = view.frame;
viewFrame.size.height += value ? DEF_BAR_HEIGHT : -DEF_BAR_HEIGHT; // Change this to the height of the tab bar
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
view.frame = viewFrame;
[UIView commitAnimations];
}
I'm not sure about DEF_BAR_HEIGHT value. Try 40 or 40 plus navigationBar height.

iPhone view appearing behind animation

I have set up a multiview application with two views. The views can be switched using a button in each view (there are two separate actions). I am using the animation UIViewAnimationTransitionFlipFromRight, and when I go from the first to the second view, the view I am going to appears behind the flipping animation. I would like it just to be white. Any help is appreciated.
alt text http://img35.imageshack.us/img35/9982/picture11wu.png
This is the action switching the views:
- (IBAction)switchViewsOne:(id)sender
{
if (self.uLViewController == nil)
{
ULViewController *uLController =
[[ULViewController alloc]
initWithNibName:#"ULView"
bundle:nil];
self.uLViewController = uLController;
[uLController release];
}
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
UIViewController *comming = nil;
UIViewController *going = nil;
UIViewAnimationTransition transition;
if (uLViewController.view.superview == nil)
{
comming = uLViewController;
going = mainViewController;
transition = UIViewAnimationTransitionFlipFromLeft;
}
[UIView setAnimationTransition: transition forView:self.view
cache:YES];
[comming viewWillAppear:YES];
[going viewWillDisappear:YES];
[going.view removeFromSuperview];
[self.view insertSubview: comming.view atIndex:10];
[going viewDidDisappear:YES];
[comming viewDidAppear:YES];
[UIView commitAnimations];
}
You could try using addSubView which just adds it to the top of the stack.
This kind of bug can be if comming.view is nil
[self.view insertSubview: comming.view atIndex:10];
Check, maybe your nib isn't loaded properly
ULViewController *uLController = [[ULViewController alloc] initWithNibName:#"ULView" bundle:nil];
NSLog(#"Loaded controller %#:", uLController);

Why no animation in PageControl project?

I'm using the PageControl project, which is on Apple's dev site. I've added a flip view to the project and an info icon to the top right of each view/page. For some reason, only the first page is able to animate the flipping. Page 2 on still shows the flipped page but does not animate. To make sure it isn't anything special about page 1, I switch page 1 and 2 and that worked fine. Page 2 in position 1 animated while Page 1 in position 2 did not. Any ideas why this would happen or how I can trouble shoot it?
I did take a look at this thread, which seems to be the same issue: Flip View Iphone. However, My flipview is a UIViewController and so is the class with the info icon on it. In the other thread, they are using UIViews.
I did implement the showInfo code from the above thread. When on Page 2, I see no flip. Then I scroll over to Page 1 and see it has flipped. Not sure why it isn't staying with Page 2. When on Page 1, it doesn't animate the flip. The flipview just suddenly appears.
Do you have a containerView? Something that can be there so you can add and remove subviews from it? Animation can break if you have two viewControllers, one coming and one going, without a containerView. I use a rootViewController and animate all my pages to and from each other with the rootViewcontroller in the back. Here is my code for flipping, you'll probably have to do a little editing to make it work for you:
(keep in mind that self is the rootViewcontroller, a viewcontroller with a blank view (color it so it matches your views))
- (void)switchTwoViews:(UIViewController *)view1 otherView:(UIViewController *)view2
{
/*
This method is called to switch views.
It flips the displayed view from the main view to the flipside view and vice-versa.
*/
UIViewController *coming = nil;
UIViewController *going = nil;
UIViewAnimationTransition transition;
[view1.view setUserInteractionEnabled: NO];
[view2.view setUserInteractionEnabled: NO];
if (view1.view.superview == nil) {
coming = view1;
going = view2;
transition = UIViewAnimationTransitionFlipFromLeft;
}
else {
coming = view2;
going = view1;
transition = UIViewAnimationTransitionFlipFromRight;
}
// in some cases the following is needed to size the view
// coming.view.frame = [UIScreen mainScreen].applicationFrame;
// going.view.alpha = 1.0; //uncomment these lines if we want fading of views
// coming.view.alpha = 0.0;
NSArray *viewArray = [[NSArray alloc] initWithObjects:coming, going, nil];
[coming viewWillAppear:YES];
[going viewWillDisappear:YES];
[UIView beginAnimations:#"View Flip" context:viewArray]; {
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidEnd:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// coming.view.alpha = 1.0; //uncomment these lines if we want fading of views
// going.view.alpha = 0.0;
[UIView setAnimationTransition:transition forView:self.view cache:YES];
[self.view addSubview: coming.view];
}
[UIView commitAnimations];
}
- (void) animationDidEnd:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
NSArray *viewArray = context;
[((UIViewController *)[viewArray objectAtIndex:1]).view removeFromSuperview];
[[viewArray objectAtIndex:1] viewDidDisappear:YES];
[[viewArray objectAtIndex:0] viewDidAppear:YES];
[[[viewArray objectAtIndex:0] view] setUserInteractionEnabled: YES];
[viewArray release];
}

For iphone development, how to flip a UIImageView?

I want to flip an imageView (left/right), but I can not find a UIView (or UIImageView) method to do that? any idea?
thanks!
I think the only thing you're looking for is:
UIView's
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;
and
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
These animation transitions can only be used within an animation block. The transition is set on the container view and then the old view is swapped out for the new view, then the animation is committed.
Like:
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:yourContainerView cache:YES];
[yourContainerView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];
Put your UIIMageView into a UIView and use this code (from the utility app project sample)
- (void)loadFlipsideViewController {
FlipsideViewController *viewController = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
self.flipsideViewController = viewController;
[viewController release];
// Set up the navigation bar
UINavigationBar *aNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
aNavigationBar.barStyle = UIBarStyleBlackOpaque;
self.flipsideNavigationBar = aNavigationBar;
[aNavigationBar release];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(toggleView)];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:#"Test123"];
navigationItem.rightBarButtonItem = buttonItem;
[flipsideNavigationBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];
[buttonItem release];
}
- (IBAction)toggleView {
/*
This method is called when the info or Done button is pressed.
It flips the displayed view from the main view to the flipside view and vice-versa.
*/
if (flipsideViewController == nil) {
[self loadFlipsideViewController];
}
UIView *mainView = mainViewController.view;
UIView *flipsideView = flipsideViewController.view;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionFlipFromRight : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];
if ([mainView superview] != nil) {
[flipsideViewController viewWillAppear:YES];
[mainViewController viewWillDisappear:YES];
[mainView removeFromSuperview];
[infoButton removeFromSuperview];
[self.view addSubview:flipsideView];
[self.view insertSubview:flipsideNavigationBar aboveSubview:flipsideView];
[mainViewController viewDidDisappear:YES];
[flipsideViewController viewDidAppear:YES];
} else {
[mainViewController viewWillAppear:YES];
[flipsideViewController viewWillDisappear:YES];
[flipsideView removeFromSuperview];
[flipsideNavigationBar removeFromSuperview];
[self.view addSubview:mainView];
[self.view insertSubview:infoButton aboveSubview:mainViewController.view];
[flipsideViewController viewDidDisappear:YES];
[mainViewController viewDidAppear:YES];
}
[UIView commitAnimations];
}
mainView and flipToView are objects of imageViews and containerView is an object of UIView.
Given below are two sample code to curl the imageView and to flip the ImageView from left to Right
- (void)curlAction:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown)forView:containerView cache:YES];
if ([flipToView superview])
{
[flipToView removeFromSuperview];
[containerView addSubview:mainView];
}
else
{
[mainView removeFromSuperview];
[containerView addSubview:flipToView];
}
[UIView commitAnimations];
}
and to bring the image from left to right here's the code
- (void)flipAction:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:([mainView superview] ?
UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
forView:containerView cache:YES];
if ([flipToView superview])
{
[flipToView removeFromSuperview];
[containerView addSubview:mainView];
}
else
{
[mainView removeFromSuperview];
[containerView addSubview:flipToView];
}
[UIView commitAnimations];
}
Thank &Regards
Another solution is available at my repo but it does'nt involve animation yet! It only manipulates image orientations to provide for some image effects namely flip vertically/horizontally and rotate 90 degrees left/right.