How to add an animation in adding a view? - iphone

I have an application in which I had a button on the navigationbar.
When clicking on that I need to add a subview to the self.view.
DemoViewController *demoViewController1 = [[DemoViewController alloc] initWithNibName:#"DemoViewController" bundle:nil];
CGRect frame=demoViewController1.view.frame;
frame.origin.y = -300;
[self.view addSubview:demoViewController1.view];
[UIView animateWithDuration:0.5
animations:^{
CGRect frame1 = frame;
frame1.origin.y = 0;
demoViewController1.view.frame = frame1;
}];
I was tried by setting the frame origin y of the adding view(it is of another view controller).to some negetive value and by clicking bring it to 0.but it doesnt have that drop down effect.
I need to add the view with an animation like drop down view. and when pressed again on the button need to remove also like this.
Can anybody help me on this?

You add the view as subview with a frame that is not on the screen (e.g. negative y coordinate) and modify the frame animated:
MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(50, -200, 200, 200)];
[self.view addSubview:myView];
[UIView animateWithDuration:0.3
animations:^{
CGRect frame = myView.frame;
frame.origin.y = 0;
myView.frame = frame;
}];
And the code for removal:
[UIView animateWithDuration:0.3
animations:^{
CGRect frame = myView.frame;
frame.origin.y = -1 * frame.size.height;
myView.frame = frame;
}
completion:^(BOOL finished) {
[myView removeFromSuperview];
}];
Edit:
Replace your code with this:
DemoViewController *demoViewController1 = [[DemoViewController alloc] initWithNibName:#"DemoViewController" bundle:nil];
CGRect frame=demoViewController1.view.frame;
frame.origin.y = -300;
demoViewController1.view.frame = frame; // You forgot this line
[self.view addSubview:demoViewController1.view];
[UIView animateWithDuration:0.5
animations:^{
CGRect frame1 = demoViewController1.view.frame;
frame1.origin.y = 0;
demoViewController1.view.frame = frame1;
}];

First keep ur view at bottom.
when it's clicked first time
do this
[UIView animateWithDuration:0.3 animations:^{
//keep points that's in a screen
self.view.center = CGPointMake(x,y,width,height);
}];
when it's clicked second time
give points below the visible screen
[UIView animateWithDuration:0.3 animations:^{
//keep points that's in a screen
self.view.center = CGPointMake(x,y,width,height);
}];
Let me know if you find a better way than this.

[self.yourView setFrame:setYourInitialFrame];
[UIView animateWithDuration: 1.00 animations:^{
[self.yourView setFrame:setYourFinalFrame];
}];

Related

Set animation for a dynamically created UIView

I figured out most of the thing how to add UIView dynamically, but now I'm stuck trying to create a alpha animation when a uibutton is clicked and the uiview is shown up. Not sure what I am missing here:
- (void)buttonPressedAction:(id)sender
{
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size = self.scrIcon.frame.size;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
UIView* view = [[UIView alloc] initWithFrame:frame];
view.alpha = 1.0;
[UIView commitAnimations];
view.backgroundColor = [UIColor redColor];
[scrIcon addSubview:view];
/*
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
CategoryViewController* theView = [sb instantiateViewControllerWithIdentifier:#"categoryIdentifier"];
theView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:theView animated:YES];*/
}
scrIcon is a UIScrollView
thanks
You can try this method: animateWithDuration:animations:
UIView *view = [[UIView alloc] initWithFrame:self.scrIcon.frame];
view.backgroundColor = [UIColor redColor];
view.alpha = 0.0;
[scrIcon addSubview:view];
[UIView animateWithDuration:1.0 animations:^{
view.alpha = 1.0;
}];
Modified your code add the view 1st to scrIcon. Then perform the animation.
- (void)buttonPressedAction:(id)sender
{
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size = self.scrIcon.frame.size;
UIView* view = [[UIView alloc] initWithFrame:frame];
view.alpha = 0.0f;
view.backgroundColor = [UIColor redColor];
[scrIcon addSubview:view];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
view.alpha = 1.0;
[UIView commitAnimations];
}
In ViewDidLoad: just init Your View…..
YourAnimateView = [[UIView alloc]initWithFrame:CGRectMake(-320, 0, 320, 320)];
[self.view addSubview:YourAnimateView];
In Your Button Action Method : just put this
[UIView animateWithDuration:0.5 delay:0.1
options:UIViewAnimationOptionTransitionCurlDown
animations:^{
//animation code
YourAnimateView.frame = CGRectMake(0, 0, 320, 345);
} completion:^(BOOL finished) {
//completion code
}];

UIAnimation - Show UIView from button.frame.origin.y

I have a UIButton somewhere on my view. On touch event of button I making a UIView appear. The UIAnimation I have used make the view appear from top of window. But I want it to appear from button.frame.origin.y . Before touching the button the view is not visible. On touching the button view should start appearing from above position.
Here is the code :
-(IBAction) ShowInfowView:(id)sender{
CGRect rect = viewInfo.frame;
rect.origin.y = rect.size.height - rect.size.height+58.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.70];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
viewInfo.frame = rect;
[UIView commitAnimations];
}
This is how I am hiding the view again :
-(IBAction) HideInfoView:(id)sender{
CGRect rect = viewInfo.frame;
rect.origin.y = -rect.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.70];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
viewInfo.frame = rect;
[UIView commitAnimations];
}
In viewDidLoad I am doing the following :
CGRect rect = viewInfo.frame;
rect.origin.y = -rect.size.height;
viewInfo.frame = rect;
UPDATE:
Please see this example. Here view is appearing from top of screen. I want it to appear from button y axis. For that matter please consider button y position a bit upwards.
So you want a slide-in effect, but not from the top of the screen, just some arbitrary value?
One way to do it:
1) You should create a view that has the dimensions and position of your desired view AFTER animation finishes, we'll call it baseview.
2) Set this baseview property clipsToBounds to YES. This will make all subviews that are outside of the baseview's frame invisible.
3) Add your animating view as a subview of the baseview, but set the frame so it is invisible (by plcacing it above the baseview):
frame = CGRectMake(0, -AnimViewHeight, AnimViewWidth, AnimViewHeight);
4) Animate the animview frame:
//put this inside of an animation block
AnimView.frame = CGRectMake(0, 0, AnimViewWidth, AnimViewHeight);
EDIT:
Example:
//define the tags so we can easily access the views later
#define BASEVIEW_TAG 100
#define INFOVIEW_TAG 101
- (void) showInfo
{
//replace the following lines with preparing your real info view
UIView * infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[infoView setBackgroundColor: [UIColor yellowColor]];
[infoView setTag: INFOVIEW_TAG];
//this is the frame of the info view AFTER the animation finishes (again, replace with real values)
CGRect targetframe = CGRectMake(100, 100, 100, 100);
//create an invisible baseview
UIView * baseview = [[UIView alloc] initWithFrame:targetframe];
[baseview setBackgroundColor: [UIColor clearColor]];
[baseview setClipsToBounds: YES]; //so it cuts everything outside of its bounds
[baseview setTag: BASEVIEW_TAG];
//add the nfoview to the baseview, and set it above the bounds frame
[baseview addSubview: infoView];
[infoView setFrame:CGRectMake(0, -infoView.bounds.size.height,
infoView.bounds.size.width, infoView.bounds.size.height)];
//add the baseview to the main view
[self.view addSubview: baseview];
//slide the view in
[UIView animateWithDuration: 1.0 animations:^{
[infoView setFrame: baseview.bounds];
}];
//if not using ARC, release the views
[infoview release];
[baseview release];
}
- (void) hideinfo
{
//get the views
UIView * baseView = [self.view viewWithTag: BASEVIEW_TAG];
UIView * infoView = [baseView viewWithTag: INFOVIEW_TAG];
//target frame for infoview - slide up
CGRect out_frame = CGRectMake(0, -infoView.bounds.size.height,
infoView.bounds.size.width, infoView.bounds.size.height);
//animate slide out
[UIView animateWithDuration:1.0
animations:^{
[infoView setFrame: out_frame];
} completion:^(BOOL finished) {
[baseView removeFromSuperview];
}];
}
I have done exactly what you're trying in my recent project.
The following steps should make this work:
1. In your viewDidLoad: you init your viewToFadeIn like this:
viewToFadeIn = [[UIView alloc] initWithFrame:CGRectMake(20,self.view.frame.size.height+10, self.view.frame.size.widh-40, 200)];
//its important to initalize it outside your view
//your customization of this view
[self.view addSubview:viewToFadeIn];
2.your ShowInfowView: Method should look like this:
` -(IBAction) ShowInfowView:(id)sender{
[UIView beginAnimations:#"fadeIn" context:NULL];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
viewToFadeIn.transform = CGAffineTransformMakeTranslation(0, -290);
//290 is in my case, try a little for your correct value
[UIView commitAnimations];
}
3.yourHideInfoView:` Method looks like this
-(IBAction) HideInfoView:(id)sender{
[UIView beginAnimations:#"fadeOut" context:NULL];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
viewToFadeIn.transform = CGAffineTransformMakeTranslation(0, 0);
[UIView commitAnimations];
}
EDIT:
4. animationFinishedMethod:
- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
if ([animationID isEqual:#"fadeIn"]) {
//Do stuff
}
if ([animationID isEqual:#"fadeOut"]) {
//Do stuff
}
}
SEE THIS
This should do the trick. Good luck
Your problem is not clear (for me).
What is this?
rect.origin.y = rect.size.height - rect.size.height+58.0;
Is 58 origin of your UIButton?
You should use sender.frame etc
Use blocks to animate like this
[UIView animateWithDuration:0.5f animations:^{
// Animation here
} completion:^(BOOL finished) {
// onComplete
}];
- (void) slideIn{
//This assumes the view and the button are in the same superview, if they're not, you'll need to convert the rects
//I'm calling the animated view: view, and the button: button...easy enough
view.clipToBounds = YES;
CGRect buttonRect = button.frame;
CGRect viewRect = CGRectMake(buttonRect.origin.x, buttonRect.origin.y + buttonRect.size.height, view.bounds.size.width, 0);
CGFloat intendedViewHeight = view.height;
view.frame = viewRect;
viewRect.size.height = intendedViewHeight;
[UIView animateWithDuration:.75 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
view.frame = viewRect;
}completion:nil];
}
This will cause the the view to appear to slide out from under the button. To slide the view back in, you just reverse the animations. If you wish to slide it up from the button, you'll need to make sure your starting 'y' is the 'y' value of the button (rather than the 'y' + 'height') and animate both the height and y values of the view that needs animating.

animate and resize UIImageView

How do I animate and resize a UIImageView? I tried doing this and it didn't work out for me:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
imgView.frame = CGRectMake(0, 0, 320, 480);
[UIView commitAnimations];
Try using a block animation:
[UIView animateWithDuration:1.0f // This can be changed.
animations:^{
CGRect frame = imageView.frame;
frame.size.width += 100.0f; // This is just for the width, but you can change the origin and the height as well.
imageView.frame = frame;
}
completion:^(BOOL finished){
}];
Hope that Helps!
Try this code. This works fine for me,
CGPoint newLogoutCenter = CGPointMake(160, 364);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
twitterBarVIew.center = newLogoutCenter;
[UIView commitAnimations];
If we assume that you have correct reference to the imgView, then it seems that starting frame of the imgView is the same before and after animation.
Try to set imgView.frame to:
imgView.frame = CGRectMake(100,100,100,100);
//Set up the UIImageView. the Frame determines the position and size of the image.
UIImageView *i = [[UIImageView alloc] initWithFrame:[CGRectMake(50.0,50.0,50.0,50.0)]];
//Add images to UIImageView
i.images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"], nil];
//Set the amount of time for the animation to run
i.animationDuration = 1.0f;
//start the animations
[i startAnimating];
You can resize the image by changing the frame of the image
i.frame = CGRectMake(x, y, width, height);

How to show/hide a UIView with animation in iOS?

The main UIView contains two subviews - UIView_1 and UIView_2.
In the UIView_2, there is a button to show or hide the UIView_1.
For example, when a user touches the button to show the UIView_1, then UIView_1 will slide down and UIView_2 will push downwards with transition.
I have very little knowledge in animation. Can someone show me some sample code for reference?
Should I use CGAffineTransformMakeTranslation?
Thanks.
You don't need anything so complex. Just change the view's frame size.
NSTimeInterval animationDuration = /* determine length of animation */;
CGRect newFrameSize = /* determine what the frame size should be */;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
theViewToChange.frame = newFrameSize;
[UIView commitAnimations];
Simply Hide/show with fadein/out effect
`
/To Show/
sliderView.hidden = NO;
sliderView.alpha = 0.1;
[UIView animateWithDuration:0.25 animations:^{
sliderView.alpha = 1.0f;
} completion:^(BOOL finished) {
// do some
}];
/To hide/
[UIView animateWithDuration:0.25 animations:^{
sliderView.frame = CGRectMake(130, 30, 0, 0);
[sliderView setAlpha:0.1f];
} completion:^(BOOL finished) {
sliderView.hidden = YES;
}];
`
It depends on what you want to do with UIView_2.
Place UIView_1 below UIView_2 in Interface Builder.
Size UIView_2 to take up all the space below the UINavigationBar.
Use the following code to either resize (using uiview2_resized_rect) the frame for UIView_2, or translate/move the frame for UIView_2 (using uiview2_translated_rect ):
CGRect uiview1_original_rect = UIView_1.frame;
CGRect uiview2_original_rect = UIView_2.frame;
CGRect uiview2_translated_rect = CGRectMake(uiview2_original_rect.origin.x,
uiview2_original_rect.origin.y+uiview1_original_rect.size.height,
uiview2_original_rect.size.width,
uiview2_original_rect.size.height);
CGRect uiview2_resized_rect = CGRectMake(uiview2_original_rect.origin.x,
uiview2_original_rect.origin.y+uiview1_original_rect.size.height,
uiview2_original_rect.size.width,
uiview2_original_rect.size.height-uiview1_original_rect.size.height);
[UIView animateWithDuration:0.300 delay:0.0
options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState
animations:^{
//uncomment this and comment out the other if you want to move UIView_2 down to show UIView_1
//UIView_2.frame = uiview2_translated_rect;
UIView_2.frame = uiview2_resized_rect;
} completion:^(BOOL finished) {
}];

iphone: expand and collapse a UIView programmatically

I am very new to iPhone/iPad development.
can you please help me create this programmatically. I want to expand/collapse a UIView programmatically.
This expandable/collapsable view will have some text field and lables which should appear and disappear with that view
Suppose you have a UIView instance in the UIViewController class like this:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, w1, h1)];
[self.view addSubview:view];
based on the requirement you set the view visibility as I did here .. I am not displaying the view when the controller is loading it's view ..
[view setHidden:YES];
Maintain a flag to check the visibility of the view instance .. lets say isViewVisible is my flag to check the visibility of the view .. I set it to NO in the begning ..
isHelpViewVisible = NO;
and I wrote an action method (viewClicked) here to expand and to collapse the view object , give this action method to a button instance and it will work.
- (void)viewClicked:(id)sender {
if (!isViewVisible) {
isViewVisible = YES;
[view setHidden:NO];
[UIView beginAnimations:#"animationOff" context:NULL];
[UIView setAnimationDuration:1.3f];
[view setFrame:CGRectMake(x, y, w1, h1)];
[UIView commitAnimations];
} else {
isViewVisible = NO;
[view setHidden:NO];
[UIView beginAnimations:#"animationOff" context:NULL];
[UIView setAnimationDuration:1.3f];
[view setFrame:CGRectMake(x, y, width, hight)];
[UIView commitAnimations];
}
}
and add the textfields and labels objects to the view object as subviews and set the animation to those objects as well .. it will work.
Replaces the following from above:
[UIView beginAnimations:#"animationOff" context:NULL];
[UIView setAnimationDuration:1.3f];
[view setFrame:CGRectMake(x, y, w1, h1)];
[UIView commitAnimations];
With the New way of doing animations:
[UIView animateWithDuration:1.3f animations:^{
self.frame = CGRectMake( x1, x2, w1, h1);
} completion:^(BOOL finished) {
// this gives us a nice callback when it finishes the animation :)
}];
To change the size / position of a UIView within its parent just change its frame property:
CGRect newFrame = myView.frame;
newFrame.origin.x = newX;
newFrame.origin.y = newY;
newFrame.size.width = newWidth;
newFrame.size.height = newHeight;
myView.frame = newFrame;
You can try this
CGRect frame = [currentView frame];
frame.size.width = 999;//Some value
frame.size.height = 444;//some value
[currentView setFrame:frame];
You can follow this whenever you want to increase/decrease view size