Change UIScrollView contentSize with animation - ios

Is it possible to change a UIScrollView's contentSize with an animation so the size decreases / increases on a certain period, something like
UIView.animateWithDuration(0.3, animations: {
scrollView.contentSize.width -= 100.0
}
Doesn't work. I don't know if there's a way to do that ?

Try the transitionWithView method:
float newHeight = scroller.contentSize.height - someChangeFloat;
[UIView transitionWithView:scroller
duration:0.3f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
scroller.contentSize = CGSizeMake(0, newHeight);
}
completion:^(BOOL finished){
}];

Related

UIView animateWithDuration not setting frame size, when performed the first time

I have an instance of [UIView animateWithDuration: delay: options: animations: completion:] within a scrollViewDidEndDecelerating method that resizes a UIView to be a smaller height if the contentOffset is greater than 0.
For some reason, the frame doesn't stay the way it should on the second scroll. Meaning that despite the resize and the visible change in the frame, the frame jumps to it's larger height and reanimates back to the smaller height. This only seems to happen the second time scrolling, but it retains the desired height henceforth, throughout the lifecycle of the app.
I have no frame changes done other than in my instances of [UIView animateWithDuration: delay: options: animations: completion:], so why does this frame not stay the way it should at that time?
I like to point out that the scrollView is actually a horizontal collectionView, here is the code:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGFloat offset = self.collectionView.contentOffset.x;
if(offset == 0){
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headingView.frame = CGRectMake(0, 0, 320, 95);
} completion:^(BOOL finished){
}];
}else if(self.headingView.frame.size.height != 60){
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headingView.frame = CGRectMake(0, 0, 320, 60);
} completion:^(BOOL finished){
}];
}
}

iOS Animating main UIViewController's View height

I have a text field pinned to the bottom of my main UIViewController's view (self.view), and when the user clicks on it this function is called (via a UIKeyboardWillShowNotification) which will alter the height of the self.view.frame:
-(void)keyboardWillShow: (NSNotification*) notification {
NSDictionary* info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat textFieldHeight = activeField.frame.size.height;
CGPoint textFieldOrigin = activeField.frame.origin;
textFieldOrigin.y += textFieldHeight;
CGRect visibleRect = self.view.frame;
visibleRect.size.height -= keyboardSize.height;
if (!CGRectContainsPoint(visibleRect, textFieldOrigin)) {
CGRect r = self.view.frame;
r.size.height -= keyboardSize.height;
[UIView animateWithDuration:1.0
animations:^{
self.view.frame = r;
}];
}
}
It resizes the self.view.frame fine, so it is all being called and run, but for some reason refuses to animate it over a second - it just appears in place immediately.
What do I need to do in order to animate this change in height?
If you have auto layout on you shouldn't change the frame directly, instead you should change on of the constrains.
Add an IBOutlet to the constrain (the bottom constrain), and change the constant like so:
myTextFieldConstrain.constant -= YOUR_VALUE
Also, if you want it to animate, call [YOURSUPERVIEW layoutIfNeeded]; after you change the constant.
Example:
[UIView animateWithDuration:1.0
animations:^{
myTextFieldConstrain.constant -= YOUR_VALUE;
[YOURSUPERVIEW layoutIfNeeded];
}];
Try to dispatch it with a slight delay, like:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1.0
animations:^{
self.view.frame = r;
}];
}
It usually makes the trick.

iOS7 view transition effect zoomed in

Hi maybe this is a duplicated question and I just don't know the keyword to describe my question, if this is the case please point out.
I'm currently implementing a transition between two view, as the second view shows, it should looked like it's zoomed into the screen from outside, kind of fall on the screen. My idea is to zoom the second view 9 * original size and place the origin to -original_width, -original_height, and set alpha to 0. At the end I set alpha to 1 and show the original size.
so the following code is not working, the subviews are not resized. Could anyone tell me what I miss here or better show me some API which do the task?
[toVC.view setFrame:CGRectMake(0-fromVC.view.frame.size.width, 0-screenRect.size.height, fromVC.view.frame.size.width*3, fromVC.view.frame.size.height*3)];
for (UIView* view in toVC.view.subviews) {
CGRect frame = view.frame;
[view setFrame:CGRectMake(frame.origin.x*3, frame.origin.y*3, frame.size.width*3, frame.size.height*3)];
}
[toVC.view layoutIfNeeded];
[toVC.view setAlpha:0.1];
[UIView animateWithDuration:duration*5
animations:^{
[toVC.view setAlpha:1.0];
[toVC.view setFrame:CGRectMake(0, 0, fromVC.view.frame.size.width, fromVC.view.frame.size.height)];
for (UIView* view in toVC.view.subviews) {
CGRect frame = view.frame;
[view setFrame:CGRectMake(frame.origin.x/3, frame.origin.y/3, frame.size.width/3, frame.size.height/3)];
}
}
completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
You could just set the view transform prior and after the transition:
CGAffineTransform scaleTransform = CGAffineTransformIdentity;
scaleTransform = CGAffineTransformScale(rotationTransform, 9., 9.);
view.transform = scaleTransform;
view.alpha = 0;
[UIView animateWithDuration:duration*5
animations:^{
view.transform = CGAffineTransformIdentity;
view.alpha = 1.;
}
}
completion:^(BOOL finished) {
}];

Animation Grow/Decrease Size imageView iOS

I'm trying to animate a custom button using CGAffineTransformMakeScale as follows:
if (stateButton == 0) { //The button is gonna appear
self.selected = YES;
self.imageView.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
// animate it to the identity transform (100% scale)
self.imageView.transform = CGAffineTransformIdentity;
} completion:nil];
}
else if (stateButton ==1) { //The button is gonna disappear
self.imageView.transform = CGAffineTransformMakeScale(1, 1);
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
// decrease button
self.imageView.transform = CGAffineTransformMakeScale(.01, .01);
} completion:^(BOOL finished){
self.selected = NO;
}];
}
The button grows perfectly to the original size, however, i don't know the reason but when i click on the button to decrease it, it decreases from a size like 100% bigger than the original size to the original size, instead of beginning the decrease in the original size and achieve an scale of 0.01 as I indicated in the code.
Please help!!
You can animate the grow and decrease size of the image view using the following code
[UIView animateWithDuration:2.0 animations:^{
self.imageView.transform = CGAffineTransformMakeScale(0.5, 0.5);
}
completion:^(BOOL finished){
[UIView animateWithDuration:2.0 animations:^{
self.imageView.transform = CGAffineTransformMakeScale(1, 1);
}];
}];
This will make the imageview decrease in size initially and when animation is over it will get back to its original size with an animation.
SWIFT 3 Version
UIView.animate(withDuration: 2.0, animations: {() -> Void in
self.imageView?.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
}, completion: {(_ finished: Bool) -> Void in
UIView.animate(withDuration: 2.0, animations: {() -> Void in
self.imageView?.transform = CGAffineTransform(scaleX: 1, y: 1)
})
})

Animating UIView size jumps

Tyring to animate a UIView by changing its height and position. The first animation jumps? I have the size of the UIView (theView) set in Storyboards to be height of 216. So the app loads and the theView is shown. I click theView and it quickly jumps in height to 200+ pixels then animates as it should?
- (void)singleTapAction:(UIGestureRecognizer *)singleTap {
UIView *view = singleTap.view;
[UIView animateWithDuration:1.2 delay:0 options:UIViewAnimationCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^{
CGRect theFrame = self.theView.frame;
theFrame.size.height += 100.f; <!-- when it hits here the view jumps then animates
self.theView.frame = theFrame;
}
completion:^(BOOL finished) {
if(finished){
[UIView animateWithDuration:1.2 delay:0 options:UIViewAnimationCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^{
CGRect theFrame = self.theView.frame;
theFrame.origin.y -= 100.f;
self.theView.frame = theFrame;
}
completion:^(BOOL finished) {
NSLog(#"animations complete");
}];
}
}];
}
Try moving lines
CGRect theFrame = self.theView.frame;
theFrame.size.height += 100.f; <!-- when it hits here the view jumps then animates
outside the block. Put it before the animation method is called.

Resources