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...
Related
I have a view controller....when the user presses a button I want a black screen to fade in over the top and a uiactivityindicator to appear on top of the faded screen.
How can I get this animated fade in effect?
-(IBAction) loginToApp:(id)sender{
//fade in view
}
Add another view over the top of your view, colored solid black. You can do it in IB or programatically, just make sure it has a higher z-order (is on top). Make its alpha = 0. Then:
-(IBAction) loginToApp:(id)sender{
[UIView animateWithDuration:1.5 animations:^{ myview.alpha = 1.0; }]
}
...obviously you need an outlet or variable myview connected to that view. 1.5 = seconds. Change for your needs.
Sure you can. You can create the overlay view however you wish, but this will work (assuming you create appropriate instance variables somewhere). My code was in my view controller, so [self view] returns the view I want to shade over.
shadeView = [[UIView alloc] initWithFrame:[[self view] bounds]];
shadeView.backgroundColor = [UIColor blackColor];
shadeView.alpha = 0.0f;
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorStyleWhiteLarge];
spinner.center = CGPointMake(CGRectGetMidX([shadeView bounds]),
CGRectGetMidY([shadeView bounds]));
[shadeView addSubview:spinner];
[[self view] addSubview:shadeView];
To present:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
[spinner startAnimating];
shadeView.alpha = 0.7f;
[UIView commitAnimations];
And to hide:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[spinner stopAnimating];
shadeView.alpha = 0.0f;
[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];
}
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.
please consider the code below, and tell me what I'm doing wrong.
I want to flip between two UIViews.
Somehow, when I flip away from the initial view, I just get the flipped view, without animation. When I flip back, the animation shows just fine.
The flips are triggered from buttons on the views themselves.
- (IBAction)showMoreInfo:(id)sender
{
UIView *moreInfo = self.flipView;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationBeginsFromCurrentState:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
UIView *parent = self.view.superview;
[self.view removeFromSuperview];
[parent addSubview:moreInfo];
[UIView commitAnimations];
}
- (IBAction)showLessInfo:(id)sender
{
UIView *lessInfo = self.view;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationBeginsFromCurrentState:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.flipView cache:YES];
UIView *parent = self.flipView.superview;
[self.flipView removeFromSuperview];
[parent addSubview:lessInfo];
[UIView commitAnimations];
}
It's probably because you aren't using a container view as the transition view. Refer to the documentation on setAnimationTransition:forView:cache:
If you want to change the appearance of a view during a transition—for example, flip from one view to another—then use a container view, an instance of UIView, as follows:
Begin an animation block.
Set the transition on the container view.
Remove the subview from the container view.
Add the new subview to the container view.
Commit the animation block.
Try using self.view.superview in the animation transition view of the showMoreInfo:
The reason the showLessInfo: method works is you are using a container view.
Can you use your MainWindow (UIWindow) as the container view as UIWindow inherence from UIView?
Also iPhone 3.0 introduced the flip transaction via the presentModalViewController method:
CustomViewController *vc = [[CustomViewController alloc]
initWithNibName:#"CustomViewController" bundle:nil];
vc.delegate = self;
// The magic statement. This will flip from right to left.
// present the modal view controller then when you dismissModalViewController
// it will transition flip from left to right. Simple and elegant.
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:vc animated:YES];
[vc release];
After iOS 4.0, you can flip between views with this:
[UIView transitionFromView:sourceView toView:destinationView duration:0.35f options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
NSLog(#"I just flipped!");
}];
As Jason mentioned, you'll need this to occur within a container view.
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.