Animate image view inside a container view in iOS? - ios

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

Related

how to expand and collapse views?

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.

Zoom multiple UIimageview inside the dynamic UiscrollView without taking any main container view for paging view

How to zoom ScrollView along with all its content without taking main container view.
I am working on a app where i have to show two image in a scrollview dynamically like a pager view for 80 images coming from url.i am able to show two images(like pages of book) in scroll view and i want to zoom both image in the scroll view simultaneously without taking any main container view.
I think it can be achieved by doing something like below
[UIView animateWithDuration:.2 animations:^{
imageView.transform=CGAffineTransformMakeScale(self.scrollview.zoomScale, self.scrollview.zoomScale);
}];
Note: Code has not been tested, please try, and update.
Update
for(UIView *subview in self.scrollView.subviews){
for(UIImageView *imageView in subview.subviews){
[UIView animateWithDuration:.2 animations:^{
imageView.transform=CGAffineTransformMakeScale(self.scrollview.zoomScale, self.scrollview.zoomScale);
}];
}
}
Cheers.

How to translate an entire UIView and retain gesture recognition?

I have a UIView "MainView" that initially appears as follows:
The gradient bar is part of MainView, the whitespace beneath is part of a Container View subview.
When the search button in top-right is tapped, I animate a searchBar from offscreen to be visible in the view:
I manage to do this by the following code:
CGRect currentViewFrame = self.view.bounds;
currentViewFrame.origin.y += searchViewHeight;
[UIView animateWithDuration:0.4
delay:0.0
usingSpringWithDamping:1.0
initialSpringVelocity:4.0
options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.view.frame = currentViewFrame;
}
completion:^(BOOL finished)
{
}];
Visually, the result of this animation is perfect. The entire view shifts, and the searchBar is now on screen. However, the searchBar does not respond to interaction. Correct me if I'm wrong, but I expect this is because the MainView's frame no longer includes the screen area that the searchBar now occupies, so its effectively a gesture deadzone.
So this makes me think that instead of lazily animating the entire MainView down to accomodate the searchBar, I must instead individually translate all subviews of MainView one at a time. In this simple situation, that would not be a big problem, but I can envision a circumstance with tens of subviews making that completely unrealistic.
What is the best method to accomplish what I am trying to do? Is there a secret to animating entire views/subviews without having gesture deadzones? Thanks in advance!!

With Objective-C, how can I animate the frame position of a bottom layer without affecting top layers?

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.

Animating a container view code

I have an app with two UIViews in a IB ViewController. Each of these is a container view having two UIImageViews.
I use Core Motion gravity to move these views as the device is tipped. There are also two UIImageViews in the IB view.
I am trying to animate the UIImageViews inside the container views. However, when the animation happens, it takes place immediately instead of taking place over 5 seconds that I specify in the annimation.
I have tried to animate the container view and the UIImageViews but the same thing occurs.
Any ideas?
This is my code
[UIView animateWithDuration:5 delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
[self.myView setFrame:0,0,11,23self.myView2 setFrame:0,0,11,23))];
)];
}
completion:^(BOOL finished
This changes the size of the container view Myview and myview2.
Your code is:
[UIView animateWithDuration:5 delay:0
Buw that you want to achieve is:
[UIView animateWithDuration:5 delay:5

Resources