Autolayout during animation - objective-c

I put a button in the corner of a sub view in the UIViewController. Now what I want is when the sub view increase its height, the button should be still at the corner. In the design I use autolayout as follow:
sub view (left, top, width, height)
button of the subview (left, bottom, right)
[UIView animateWithDuration:0.3 animations:^{
//- Increase the subview's height
CGRect frame = _subview.frame;
frame.size.height = frame.size.height + 100;
_subview.frame = frame;
//- Update constraints
[_subview layoutIfNeeded];
}];
But the button is still the same position.

Related

iPhone animation that grows from corner

I'd like to create an animation that when I tap on a GMSmarker, a uiview will appear with growing effect from the left or right corner of the given control/ image and include the marker in it's corner (image shows final result)
I know how to create the XIB and the related UIView, as well as adding them to the ViewController, how can I do the animation part?
You know the start frame of the view...
CGRect startFrame = CGRectMake(pin.x, pin.y, 0, 0);
You know the end frame of the view...
CGFloat height = 100;
CGFloat width = 100;
CGRect endFrame = CGRectMake(pin.x, pin.y - height, width, height);
So you can animate it...
theView.frame = startFrame;
[UIView animateWithDuration:1.0 animations:^(){
theView.frame = endFrame;
}];
This will animate out from the bottom left corner.

Change height of UIView

I have an UIViewController with TableView which is showed when I pressed Button in another View. It slides correctly from the bottom of the screen, but I want its height to be half of screen height and stays at the bottom of the screen.
How can I get this?
Here is my code:
CupsViewController
This is the code when Button is pressed for showing TableView.
//DirectionViewController is the one that contains TableView
DirectionViewController *directionController = [self.storyboard instantiateViewControllerWithIdentifier:#"directionsView"];
// Position the options at bottom of screen
CGRect optionsFrame = directionController.view.frame;
optionsFrame.origin.x = 0;
optionsFrame.size.width = 320;
optionsFrame.origin.y = 423;
// For the animation, move the view up by its own height.
optionsFrame.origin.y += optionsFrame.size.height;
directionController.view.frame = optionsFrame;
[UIView beginAnimations:nil context:nil];
optionsFrame.origin.y -= optionsFrame.size.height;
directionController.view.frame = optionsFrame;
[UIView commitAnimations];
[self.navigationController pushViewController:directionController animated:YES];
Set the options frame height to half of screen size in your animation code.
Hope this is what you are looking for
https://github.com/slysid/iOS/tree/master/HalfTable
/Bharath

Resize UITableView when I slide up a UIView, just like how it happens when the keyboard is shown

I am sliding up a UIView which has a UIDatePicker as a subview. This is added above my UITableView, but unfortunately some of the tableView rows are still under my UIView.
UITableView is pushed up with keyboard:
UITableView is NOT pushed up with my view, covers up last few fields:
Is it possible to resize the UITableView dynamically when I slide up my view, just like when the keyboard is shown when in a tableView and all the rows are still able to be seen?
EDIT:
Just found a great Apple example: http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html
Yes, it is possible. You could just go ahead and change the size of the UITableView dynamically as part of the animation that slides up the view. If you want to do what the UITableView actually does in response to the keyboard, leave its size alone and instead change its content inset and scroll indicator insets. See my answer here and the examples linked to:
uitableview not resizing with keyboard
Ok, it was easier than I thought. In my case, when I touch a row, I want the picker to show and the tableView to change its height, so I use:
[UIView animateWithDuration:0.4 delay:0.0 options: UIViewAnimationCurveEaseOut animations:^{
self.pickerView.frame = CGRectMake(0, self.view.bounds.size.height-200, self.view.bounds.size.width, self.pickerView.frame.size.height);
// shrink the table vertical size to make room for the date picker
CGRect newFrame = self.tableView.frame;
newFrame.size.height -= self.pickerView.frame.size.height;
self.tableView.frame = newFrame;
} completion:nil];
Then when I click the done button, and want to return the tableView back to the full height and hide my datePicker, I use:
- (void)doneWithPicker:(BOOL)remove {
CGRect newFrame = self.tableView.frame;
newFrame.size.height += self.pickerView.frame.size.height;
self.tableView.frame = newFrame;
[UIView animateWithDuration:0.4 delay:0.0 options: UIViewAnimationCurveEaseOut animations:^{
self.pickerView.frame = CGRectMake(0, self.view.bounds.size.height+300, self.view.bounds.size.width, self.pickerView.frame.size.height);
} completion:^(BOOL finished){
if (remove) {
[self.pickerView removeFromSuperview];
self.pickerView = nil;
}
}];
}
Also, when adding the pickerView as a subview, make sure to add it to the window:
[self.view.window addSubview:self.pickerView];

Change UIView layout programmatically for iPhone 5

I am trying to change the layout of my UIView programmatically, and not by using Auto Layout, as I am trying to deploy this app for iOS 4 and up. I basically set my view up with the screen height at 455(iPhone 5)(I know the actual screen size is 568 points, but I have a tab bar on the bottom so I am assuming the size shown in IB has taken that into account). I recorded the origin of all of my buttons and labels relative to the longer screen size and programmatically changed them to how I preferred them. The problem is, some of the buttons and labels are going off the screen now, so I think I have to perform some kind of conversion to keep them in bounds, but I am not sure what. See the pictures attached below:
Image before my initMethod is called to change button and label origins
Image after my initMethod is called to change the origin
View properties in XCode for the old screen size
This is how I want the view to Look. I moved the buttons around and recorded the origin of each.
Picture of my init method in viewWillAppear. Notice how the frame of the label is zero.
Here is the code I used:
-(void)initView
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 480) {
//iphone 4,4s
//done
}else if(screenBounds.size.height == 568) {
//iphone 5
CGRect frame;
//Static Professor label
frame = [staticProfessorLabel frame];
frame.origin.x = 48;
frame.origin.y = 218;
[staticProfessorLabel setFrame:frame];
//Professor Label
frame = [professorLabel frame];
frame.origin.x = 192;
frame.origin.y = 218;
[professorLabel setFrame:frame];
//show button
frame = [showProfessorButton frame];
frame.origin.x = 67;
frame.origin.y = 258;
[showProfessorButton setFrame:frame];
//clear proff button
frame = [clearProfessor frame];
frame.origin.x = 240;
frame.origin.y = 258;
//note label
frame = [bottomNote frame];
frame.origin.x = 160;
frame.origin.y = 310;
[bottomNote setFrame:frame];
//search button
frame = [searchButton frame];
frame.origin.x = 158;
frame.origin.y = 424;
[searchButton setFrame:frame];
//spinner
frame = [actIndicator frame];
frame.origin.x = 266;
frame.origin.y = 424;
[actIndicator setFrame:frame];
}
}
Also, I am calling this in the viewDidAppear method because when I call it in viewDidLoad, none of the buttons and labels give me a valid frame(I am guessing they are not initialized yet).
Your Xcode screenshots show that your storyboard contains constraints, so we can tell that your storyboard has autolayout turned on. If you want to set the view frames directly in code, you have to turn off autolayout.
Take a look at my answer here if you need help turning off autolayout on your storyboard.
Your hard-coded coordinates are simply wrong. Just double-check your values.
For example, bottomNote starts at x 160, which is clearly too far to the right.

Hide and show scrollview with animation

In my app I have a scrollview and when I press a button it is hidden when I press again it shows up.
I use scrollview.hidden = YES (or NO) to do it.
But I want to do it with an animation. For example, it may disappear from the bottom of the screen by moving and shows up with the same way. How can I do that?
edit:
[UIView beginAnimations:#"myAnimation" context:nil];
CGRect Frame = bottomScroller.frame;
if(Frame.origin.y == 380){
Frame.origin.y = 460;
}else{
Frame.origin.y = 380;
}
bottomScroller.frame = Frame;
[UIView commitAnimations];
This solved my problem...
You may check UIView animations. It's pretty easy. For example to animate translation you may use something like:
[UIView beginAnimations:#"myAnimation" context:nil];
CGRect Frame = yourView.frame;
Frame.origin.y = 0;
yourView.frame = Frame;
[UIView commitAnimations];
Then to move it back:
[UIView beginAnimations:#"myAnimation" context:nil];
CGRect Frame = yourView.frame;
Frame.origin.y = 100;
yourView.frame = Frame;
[UIView commitAnimations];
Changes in frame, alpha and some other parameters will be automatically animated.
You can use animations like so:-
[UIView animateWithDuration:2.0 animations:^{
[scrollview setframe:CGRectMake(xpos, ypos, width, height)];
}];
If you want to slide the scrollview on or off the screen set its y position - the bottom of the view is you want to hide it, the normal y position if you want to show it.
The 2.0 is an animation length, this can be changed to whatever you need it to be!
You can do this by using simple view animations. Here's an example of how you might do this:
// myScrollView is your scroll view
-(void)toggleShow{
CGFloat targetAlpha = myScrollView.alpha == 1 ? 0 : 1;
CGFloat yPosition = targetAlpha == 1 ? 0 : self.view.frame.size.height;
[UIView animateWithDuration:1.0 animations:^{
myScrollView.frame = CGRectMake(myScrollView.frame.origin.x, yPosition, myScrollView.frame.size.width, myScrollView.frame.size.height);
myScrollView.alpha = targetAlpha;
}];
}
targetAlpha will always be the opposite of the current state (1 or 0), and if it's 0 then the y position will be set to the bottom of the parent view. Using the new-school UIView animations API with blocks we can execute these changes to the scroll view over 1 second (in my example).
ScrollView appers from down side of screen with animation. I think this up-animation is what you want.
// ScrollView appearing animation
- (void)ScrollViewUpAnimation
{
// put the scroll view out of screen
CGRect frame = ScrollView.frame;
[self.view addSubview:ScrollView];
ScrollView.frame = CGRectMake(0, 460, frame.size.width, frame.size.height);
// setting for animation
CGPoint fromPt = ScrollView.layer.position;
CGPoint toPt = CGPointMake(fromPt.x, fromPt.y - frame.size.height - 44);
CABasicAnimation* anime =
[CABasicAnimation animationWithKeyPath:#"position"];
anime.duration = 0.2;
anime.fromValue = [NSValue valueWithCGPoint:fromPt];
anime.toValue = [NSValue valueWithCGPoint:toPt];
// change the position of Scroll View when animation start
[ScrollView.layer addAnimation:anime forKey:#"animatePosition"];
ScrollView.layer.position = toPt;
}