I want to animate a view inside ViewController, somewhat like the notification center as seen on the iPhone.
How does one extend the subview using the x,y and/or the height values to do this? Because I would like it to be compatible with both the 3.5 inch screens and the 4 inch screens, so it would be better not to use specific values.
I just tested this out in my own project and confirmed that it works on varying screen sizes.
UIView *slidingview = [[UIView alloc] initWithFrame:CGRectOffset(self.view.frame, 0, -self.view.frame.size.height)];
slidingview.backgroundColor = [UIColor whiteColor];
[self.view addSubview:slidingview];
[UIView animateWithDuration:1 animations:^{
slidingview.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
}];
To implement this, simply replace slidingview with the view you want to slide down. You would, however, have to declare your own view using the frame I provided.
How about something like this?
CGRect currentFrame = self.view.bounds;
UIView * slidingView = [UIView alloc] initWithFrame:(CGRect){currentFrame.origin, currentFrame.size.width, 0};
[self.view addSubview:slidingView];
[UIView animateWithDuration:0.5f animations:^{
slidingView.frame = currentFrame;
}];
You may need to use delay version of animateWithDuration... to make animation in effect.
Related
Here is what I currently have: http://cl.ly/PIFB
And the code behind it:
UIView *smallView = [[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:recognizer.view.tag inSection:0]] contentView];
CGRect initialSize = smallView.frame;
CGRect finalSize = CGRectMake(150.0, 100.0, 660.0, 450.0);
UIView *largeView = [[UIView alloc] initWithFrame:initialSize];
[largeView setBackgroundColor:[UIColor blackColor]];
[largeView setClipsToBounds:NO];
[UIView transitionFromView:smallView toView:largeView duration:2 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
[[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:recognizer.view.tag inSection:0]] contentView].frame = finalSize;
[largeView setFrame:finalSize];
[smallView removeFromSuperview];
[self addSubview:largeView];
[UIView animateWithDuration:2 animations:^{
smallView.frame = finalSize;
[largeView setFrame:finalSize];
}];
}];
Basically I have a UITableView of views. When one of those views is tapped I would like to scale and flip it at the same time so that it shows a new larger view in the center of the screen. Currently it flips the smaller view and eventually shows the larger view at the right size but it doesn't transition smoothly.
In the code above smallView is the currently tapped cells content view. largeView is the new view that will eventually be developed to show more data relevant to the cell that was tapped. Right now it is represented by the black view. I really would like the small cell to grow and flip at the same time so that it smoothly transitions into the larger view.
Your help would be greatly appreciated. My experience with objective c, cocoa touch, and performing animations is pretty limited.
Thanks so much.
I finally figured it out. See here: http://cl.ly/PQqP
It required me to do two separate UIView transitionWithView methods:
[_transitionView addSubview:smallView];
[smallView setFrame:CGRectMake(0, 0, _transitionView.frame.size.width, _transitionView.frame.size.height)];
[UIView transitionWithView:_transitionView duration:.5 options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionCurveEaseOut animations:^(){
[UIView transitionWithView:cellView duration:.25 options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionCurveEaseIn animations:^(){
[cellView setFrame:smallViewOrigin];
} completion:NULL];
_transitionView.frame = finalSize;
smallView.frame = smallViewOrigin;
[_transitionView insertSubview:metricModalViewController.view belowSubview:smallView];
[smallView removeFromSuperview];
metricModalViewController.view.frame = metricModalEndSize;
} completion:^(BOOL finished) {
[_transitionView setFinalView:metricModalViewController.view];;
}];
It looks like you should use transitionWithView:duration:options:animations:completion: instead. Like this:
[UIView transitionWithView:self
duration:2
options:UIViewAnimationOptionTransitionFlipFromRight|UIViewAnimationOptionAllowAnimatedContent
animations:^(){
smallView.frame = finalSize;
[smallView removeFromSuperview];
[self addSubview:largeView];
largeView.frame = finalSize;
}
completion:NULL];
That might flip the self view, which might not look exactly right. If that's a problem, then add an extra view in your hierarchy which does nothing but contain smallView or largeView at the same size as them. Set the frame of that view in addition to those of the smallView and largeView. (Or just set the container view transform, or just set the container view frame and configure appropriate autoresizing options for the smallView and largeView.)
Maybe try this. Set up a view containerView that is a child of self and initially contains smallView. Make the frame of containerView what you initially had for smallView. Make the size of smallView the same as the size of containerView but the origin (0,0) so that smallView fills containerView entirely.
[UIView transitionWithView:containerView
duration:2
options:UIViewAnimationOptionTransitionFlipFromRight|UIViewAnimationOptionAllowAnimatedContent
animations:^(){
containerView.frame = finalSize;
smallView.frame.size = finalSize.size;
[smallView removeFromSuperview];
[containerView addSubview:largeView];
largeView.frame.size = finalSize.size;
}
completion:NULL];
I'm just guessing, again.
I am try to create an animation in my app which would slide up a frame to my desired position. The frame would be having other buttons and text on it as well.
I was unable to use actionsheet for this, as it starts from the bottom most part of the window. What i want to create is shown in the picture below.
the frame I want to animate is the one with Capture mode: text.
Click on the button on the tabView I want this frame to slide up, starting from the top of the tabview. and again clicking the button should make the frame disappear.
So far my code looks like
-(IBAction)setting
{
NSLog(#"finalShare Button");
UIView *tempView;
CGRect tmpFrame;
tempView = [[[UIView alloc] initWithFrame:CGRectMake(0, 490, 320, 90)]
autorelease];
[tempView setBackgroundColor:[UIColor clearColor]];
[tempView setAlpha:.87];
tempView.hidden = YES;
[self.view addSubview:tempView];
tmpFrame = tempView.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
tmpFrame.origin.y=390;
self.view.frame=tmpFrame;
[UIView commitAnimations];
}
The problem I am facing here is that when I animate my frame to move, the main view moves along with it. Which is something I don't want it to do.
I am new to iPhone programming, please guide how can I fix this.
Or what would be a better way to do it.
Thanks
You don't need to make the view hidden, just add it as a subview after you set its frame to have y origin of 480. Also, don't use the old UIView animation methods; use blocks.
Here's an example of a view that starts with a y origin of 0. It gets positioned at negative y equal to its height, added as a subview then animated down to meet the keyboard coming up from the bottom of the screen.
// add to view while completely off-screen
CGRect unlockViewFrame = unlockView.frame;
unlockViewFrame.origin.y = -unlockViewFrame.size.height;
unlockView.frame = unlockViewFrame;
[self.view addSubview:unlockView];
unlockViewFrame.origin.y = 0.;
[UIView animateWithDuration:0.3 animations:^{
unlockView.frame = unlockViewFrame;
}];
Here's the same thing in reverse, using a completion block to remove the view.
unlockViewFrame.origin.y = -unlockViewFrame.size.height;
[UIView animateWithDuration:0.3 animations:^{
view.frame = unlockViewFrame;
} completion:^(BOOL completion) {
[view removeFromSuperview];
}];
Another thing I notice is that you are doing everything in code; consider doing it in Interface Builder. A smart man once told me that code you don't write can't have any bugs and he was right. It's easy to create a view in IB and instantiate it inyour code when you need it:
NSArray* nibViews;
nibViews = [[NSBundle mainBundle] loadNibNamed:#"UnlockView" owner:self options:nil];
UnlockView *unlockView = [nibViews objectAtIndex:0];
Also, consider using ARC. No more autorelease!
Your resetting frame of main view and not tempView
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
tmpFrame.origin.y=390;
tempView.frame=tmpFrame; //it should be tempView not self.view ie main view
[UIView commitAnimations];
When Mac kernel panic, there's animation, it's not like slide animation exactly, how to implement this animation in iPhone, is there a exist API to do this?
There is no API for that but it is fairly simple animation:
Simply create a UIView and animate its frame, changing from 0 height to full height:
// Create your view
UIView *kPview = [[UIView alloc] init];
// Add it to your view
[self.view addSubView:kPview]
// Set its frame
kPview.frame = CGRectMake(0,0,320,0);
//Set the animation
[UIView animateWithDuration:5
animations:^{
kPview.frame = CGRectMake(0,0,320,460);
}
completion:^(BOOL finished){
//add a UILabel with the warning.
}];
i'm developping an application for kids (memory game): turning two same tiles(images) and you can remove them. the problem is how to turn the uiimages with animation and only the uiimages without turning the view that contain them..
do i have to put every uiimage in a small view?
any ideas?
PS :i'm not using OpenGL
Sorry, misread the first time. Use UIImageViews for each tile and then animate as below.
a UIView is just a view, so you can animate it the way you animate any view. Its fairly straightforward.
UIImageView *tileToFlip = self.[tiles objectAtIndex:3];
CGRect frameOfTileToFlip = tileToFlip.frame;
UIImageView *newImageToShow = [[UIImageView alloc] initWithFrame:frameOfTileToFlip];
// add the image to newImageToShow
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:newImageToShow cache:NO];
[self.View addSubview:newImageToShow]; // I'm not sure if this is necessary
[UIView commitAnimations];
[tileToFlip removeFromSuperView]; // remove it so you can add it back later
Alternatively, you can use CATransition which gives you a little more control and different transitions.
You could use UIImageView for each tile.
I am using the flip animation to animate between two views in my viewcontroller. The problem is that the background shows a white blank background while the animation is taking place. I would like to show a black background.
I tried setting the background color of the main view to black both in IB and code. But the background is still white.
Can someone please help me.
Thanks.
Adding the code
[self setContentView:[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]];
contentView.backgroundColor = [UIColor blackColor];
[contentView addSubview:toolbar];
[self setView:contentView];
[contentView release];
frontView = [[FrontView alloc] initWithFrame:viewFrame];
[frontView setViewController:self];
[self.view insertSubview:frontView belowSubview:toolbar];
//Initializing the back view here too
//on button click, executing normal flip code
Even after this I get a white background
I think your issue might be that the UIWindow is shown during the animation. To fix this issue, set the background color of your main window. You can do this in code or in IB.
You begin by creating a fake main view, and set its background to black:
// Create the main view
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blackColor];
self.view = contentView;
[contentView release];
Then, you create your front and back views, and add them to your main view:
// create front and back views
UIView *frontView = ...
UIView *backView = ...
If you are using IB, skip the previous step and add directly your views
// add the views
[self.view addSubview:backView];
[self.view addSubview:frontView];
Now do the flip animation as usual.
EDIT: Probably it does not work because in your code you are adding the frontView below the toolbar. Add first the backView, then the frontView and finally the toolbar using the addSubview: method. Then, use the following code to animate the flip:
- (IBAction) flipView{
// Start Animation Block
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
// Animations
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// Commit Animation Block
[UIView commitAnimations];
}
Since the code performs [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; the order in which you add the subviews is relevant.