How to expand on clicking view(orange color) and collapse on clicking the same view or other view
like in this image :
You should be able to move a view with animation to cover the other view. Something like this:
[UIView animateWithDuration:0.5f animations:^{
view1.frame = CGRectOffset(view1.frame, 0, 250);
}];
You will have to change the frame size/position to reach the position of the other view.
Related
I'm trying to animate a image view inside a container view.
Container view is square and aligned center of the screen. i do have a image view inside my container. position of my image view is shown in below screenshot.(super view is my container view)
i want to animate the image view inside the container like from top 0 to bottom 0 so i tried animating from top 0 to top maximum (that is height of the container view) but it is not working as expected. My expected output is something like the image view should animate inside container view continuously from top to bottom and reverse.
animation method which i tried is
int maxTop = self.containerView.frame.size.height/2;
self.topConstraint.constant = maxTop;
[self.imageView setNeedsUpdateConstraints];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
[self.imageView layoutIfNeeded];
} completion:nil];
EDIT:
Here is the current output GIF. It is not moving to full height
The animation should be started after viewDidAppear, so the parent view could align itself properly.
Try
[UIView beginAnimations:nil context:nil;
[UIView setAnimationDuration:0.5];
[self.topConstraint.animator setConstant:maxTop];
[UIView commitAnimations];
As in your original code you didn't set the constraint's constant inside the animation
I am trying to make a UITableView appear when a user taps on a button and disappear when the button is re-tapped.
I implemented the following code but nothing seems to appear when I tap the button.
- (IBAction)dropDown:(UIButton *)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
self.markersTableView.transform = transfrom;
self.markersTableView.alpha = self.markersTableView.alpha * (-1) + 1;
[UIView commitAnimations];
}
What may potentially be the issue?
EDIT:
I was able to make the UITableView appear and disappear by adding self.markersTableView.hidden = YES; in viewDidLoad() and self.markersTableView.hidden = NO; in the IBAction method.
However, the table view disappears when I initially tap on the button as shown in the screenshot:
The fading of the rows is an indication it is moving down the screen, and then it disappears.
It only reappears when I re-tap on the UIButton the 2nd time.
Any clues?
Don't use a transform on your table view. That will make it LOOK like it's in a different position, but it won't respond to taps correctly.
Instead, use the newer UIView block-based animation methods and change the view's center.
The code might look like this (if you're NOT using auto-layout)
[UIView AnimateWithDuration: .2
animations: ^
{
CGPoint center = self.markersTableView.center;
center.y += 200;
self.markersTableView.center = center;
}
];
If you're using auto-layout, though, that won't work and you will need to animate the view's position using constraints.
From memory, here's an outline of how to do auto-layout based animations: You would create a vertical position constraint on your table view and connect the constraint to an IBOutlet in your view controller. In your animation code you change the constant of the constraint (the vertical position) and then in the animation block, send the view a needsLayout message.
I have a view with
UIScrollView -> image view
UILabel
I add a swipe gesture on my ScrollView to go to the next image. It works.
My label is the title of my image.
When i swipe, i want my label disappear on the left and appear on the right and set the new title.
Do I need 2 different labels?
How can I animate my label?
Make your label the subview of your scrollView. Then it will scroll when your image does.
You could use
[UIView animateWithDuration:1 animations^{ label.frame = leftOutsideView;} completion^(BOOL finished){ label.frame = rightOutsideView; label.text = newCaption; [UIView animateWithDuration:1 animations^{ label.frame = originalPlace;}];}];
(This is pseudo code)
Try to use animations in that context, it looks good.
I have two UIImageView's, one that is a background that should scroll, and one an avatar that should move positions based on user input.
I'm able to move the avatar with no problem, and I'm able to scroll my background when necessary with:
CGRect oldPos = _fieldView.frame;
CGRect newPos = CGRectMake(oldPos.origin.x, oldPos.origin.y - 400, oldPos.size.width, oldPos.size.height);
[UIView animateWithDuration:5.0
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
[_fieldView setFrame:newPos];
}
completion:^(BOOL finished){
NSLog(#"Done with animation");
}];
However, when this _fieldview scrolls, my avatar on top of it scrolls with it. How can I move my background image without affecting the other images?
A few other settings that may affect this:
I've disabled AutoLayout
My background image mode is set to AspectFill
I'm using a storyboard with a single UIView
Thank you for any suggestions.
EDIT
The avatar, runnerView, is added as a subview of _fieldView, with
[_fieldView addSubview:_runnerView];
You want your background scroll image to not be a superview to your avatar, but sibling view instead.
(Not the best representation, not sure how to make it look better though)
-Main View
--Scroll view
--Avatar
i.e.
[self.view addSubview:backgroundView];
[self.view addSubview:avatarView];
not
-Main View
--Scroll view
---Avatar
i.e.
[backgroundView addSubview:avatarView];
[self.view addSubview:backgroundView];
If it is required to be a subview, then you will have to move the avatar view an equal and opposite amount so it will stay in the same relative location.
I want to do animation like NavigationController pushviewcontroller's animation.
but I don't have a NavigationController, I don't want to make it.
So I want to ask is it possible to do it's animation in UIViewController? thanks!
oh forgot to say, I'm trying to switch view after clicking button.
Using presentModalViewController now, but I don't like it's animation..
You could animate the origin property of your sub view, make it decreasing along the x axis just after adding it to the main view.
EDIT :
Use something like this :
// retrieve the screen bounds
CGRect sBounds = [[UIScreen mainScreen] bounds];
// the origin point is just on the right of the screen
CGRect newFrame = CGRectMake(sBounds.size.width,
0.0,
sBounds.size.width,
sBounds.size.height);
// set your view frame
[mySecondView setFrame:newFrame];
// add it to the main view
[mainView addSubview:mySecondView];
// then animate your view
[UIView animateWithDuration:0.5 // set the interval you want
animations:^{
mySecondView.frame.origin = sBounds.origin;
}
];
I would use a navigation controller and hide the navigation bar, but as you don't want to do that you can switch between views using [UIView beginAnimantion: #"myAnimation" withContext: nil]; and changing the frame or the origin.