简体   繁体   中英

How i can do an uiimageView zoom animation linked to uiscrollview contentoffest?

I'm trying to achive an animation that zoom an imageview accordingly to a scrollview offset (i have seen something similar in spotify and some others apps). How i can do? I have tried something like:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  if (Scroll.contentOffset.y<-10 && Scroll.contentOffset.y>-20) {

        [UIView animateWithDuration:0.1
                              delay:0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:(void (^)(void)) ^{
                             ImageView.transform=CGAffineTransformMakeScale(1.1, 1.1);
                         }
                         completion:^(BOOL finished){


                         }];

    }
if (Scroll.contentOffset.y<-20 && Scroll.contentOffset.y>-30) {

        [UIView animateWithDuration:0.1
                              delay:0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:(void (^)(void)) ^{
                             ImageView.transform=CGAffineTransformMakeScale(1.2, 1.2);
                         }
                         completion:^(BOOL finished){


                         }];

    }
}

and so on until a value of 1.6 . Naturally this methods doesn't works well, it is called to many times and visually it jitters... I wanto to achive this result: The user scroll down the scrollview while an image view placed in the background is scaled until an arbitray value (and a reverse behaviour when the user returns to scroll up). What is a correct approch?

Try setting the transform depending on the contentOffset without using animations, like

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  CGFloat scale = 1;
  if(Scroll.contentOffset.y<0){
    scale -= Scroll.contentOffset.y/10; //calculate the scale factor as you like
  }

  ImageView.transform = CGAffineTransformMakeScale(scale,scale);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM