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

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.

Related

Unable to hide UIView completely

I am trying to hide a UIView but nothing work so far. The UIView is at the bottom of a UITableView, not inside it as a cell. Here is what I tried so far:
self.viContainerLocation.hidden = YES;
self.viContainerLocation.alpha=0.0;
CGRect Conframe = self.viContainerLocation.frame;
Conframe.size.height = self.view.bounds.size.height;
self.viContainerLocation.frame = Conframe;
self.tableView.translatesAutoresizingMaskIntoConstraints=NO;
__block CGRect frame=self.tableView.frame;
frame.size.height= self.view.frame.size.height-frame.origin.y;
frame.size.width=self.view.frame.size.width;
self.tableView.frame=frame;
viContainerLocation is the UIView I am trying to hide. The above code hide all elements inside the UIView, but leaves a white space with the dimensions of the UIView.
For a reason I don't understand, viContainerLocation is hidden when I use this code:
[UIView transitionWithView:self.viContainerLocation
duration:0.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{ }
completion:^(BOOL finish){
__block CGRect frame=self.tableView.frame;
frame.size.height= self.view.frame.size.height-frame.origin.y;
frame.size.width=self.view.frame.size.width;
self.tableView.frame=frame;
}];
But using this code the white space gets displayed of a fraction of a second and it doesn't look good.
How can I make it go away from the beginning ?
You are misunderstanding something.
When hidding an UIView, the space occupied by this view is still visible.
If you want your tableview filling the missing space, the quickest way is to ember your tableview and your view into a UIStackView.
In a UIStackView when you set one subview to isHidden=true, the view is removed, and other views fill the left space.
More infos here : https://developer.apple.com/documentation/uikit/uistackview

Navigation drawer- sliding issue in ios

I am trying to create a sliding menu (like the one in google maps app) in which menu screen should be shown or added as subview over the existing screen on tapping a button. While showing it, the sub view should be animated from left to right. I tried to animate while adding it as subview, but failed in doing so. Can any one suggest how to do it? (or) Can I do it using navigation controller?
Thanks in advance!!
You need to add the subview, and set it's frame out of the screen.(320,0,100,568 for example).
Of course it's better to use auto layout for that.
Add as subview:
[self.view addSubView:sideMenu];
When you want to show it, just change it's frame using animation block.
[UIView animateWithDuration:0.2 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
sideMenu.frame = CGRectMake(220,0,100,568);
} completion:^(BOOL finished) {
}];
As I said, it's better to use auto layout and not setting frames. I did it only for the example.

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!!

How I could animate changing UIViewControllers?

I have a UIViewController based app and I want to changes data on view with animation (handle touchesbegan, touchesmove, touchesended). All data I save in a base). For example, I have main screen, and when (touches ended) handles, I want to show other view.
Whatever i will be to do, screens could be rotate (to right or to left, if position changes more then 1/2 of width View, but if labels count number more then count of rows in my table, I want to set state at image 2.
Could you see right way to do animations? For example, I could to create my own class or use app delegate, but I not sure from what I must to start.
Use UIView's transitionsWithView method:
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{ [fromView removeFromSuperview]; [containerView addSubview:toView]; }
completion:NULL];
More details, including above example can be found here:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816
Tim

Resources