2

I want animation like home screen of this app.This screen has a view (with a label on it) on top and a table view under it. When i scroll the table the top view and title on it become small with animation and after a specific point it becomes like navigation bar with title in centre. And the same thing when i scroll down. Here is what i tried so far

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    NSLog(@"y is::: %f", offset.y);

    if (offset.y > 0 && offset.y < 16 && self.topView.frame.size.height > 64) {

        [UIView animateWithDuration:0.8 animations:^{

            CGRect rect = self.topView.frame;
            rect.size.height = rect.size.height-offset.y;
            self.topView.frame = rect;

        }completion:^(BOOL finish){

        }];

    }
    else if(offset.y <= 0 && offset.y > -16 && self.topView.frame.size.height < 80)
    {
        [UIView animateWithDuration:0.8 animations:^{

            CGRect rect = self.topView.frame;
            rect.size.height = 80;
            self.topView.frame = rect;

        }completion:^(BOOL finish){

        }];
    }

}

Note that my top view initial height is 80 and i want to make it 64 when scroll up and vice versa. with this code the view height animated but it depends upon animation duration.So if i scroll fast then it is not working properly.The original app i mentioned have very smooth animation. How could i achieve that? Screen shot of original App- enter image description here These screen shots showing three different scroll position, please note the "MySurgery" text on top.1- Larger text and right align. 2- small text and near centre position 3- smaller text and centralised(like navigation bar)

Bharat
  • 2,987
  • 2
  • 32
  • 48
  • No clue? comon guys i need your help. – Bharat Mar 24 '15 at 07:37
  • I think problem is related with the animation time. You can try to set scrollview scrollEnabled to be false when animation begins and again set it true when animation ends. Or you can use less time for the animation. – ondermerol Mar 24 '15 at 08:14
  • Maybe you can delete the animations that are running or waiting to run when a new animation starts. Note that I don't know if this is possible or not :) – ondermerol Mar 24 '15 at 08:17
  • setScrollEnabled to false cause problem when i scroll slowly(it stops scrolling), and even in faster scroll i didn't get the desired result. – Bharat Mar 24 '15 at 09:24
  • Another advice is calling removeAllAnimations before each new animation starts. – ondermerol Mar 24 '15 at 09:38
  • This can help you [UIScrollview animation depending on content offset](http://stackoverflow.com/a/26159561/1351327). You can reduce the animation time to be 0.2 and set topview frame as not directly to be 80, you can calculate a value according to percentage verticalOffset. – ondermerol Mar 24 '15 at 13:42

1 Answers1

3

I think you dont need animation for this, you just need some calculations and setting frame of the top view. like,

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;

    if (offset.y>0 && offset.y<64) {
        [topView layoutIfNeeded];
        CGRect viewFrame = topView.frame;
        viewFrame.size.height--;
        topView.frame = viewFrame;
    }
}

Note that layoutIfNeeded is required if your view is using autolayout

Abhi
  • 593
  • 4
  • 8