0

I have a view controller that have a scroll view that bounce horizontally , and in this scroll view I have a label.

I can now hold the label and scroll it down, and if I release it will bounce up back.

What I want is that: When I scroll the view y coordinate (using myScrollView.contentOffset.y) to some value, lets say -33 and under I can release my fine and the label will animate to the bottom of the screen and disappear, and now I can set the label to be a new value, and It will animate from top to the label original position.

Here a photo of how the view controller looks like:

enter image description here

And this is the relevant method I already implemented (powered by @rebello95):

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (self.myScrollView.contentOffset.y <= -73) {

        [UIView animateWithDuration:0.3 animations:^{
            self.homeLabel.alpha = 0.0;
        } completion:^(BOOL finished) {
            [self.homeLabel removeFromSuperview];
            self.homeLabel = nil;
        }];
    }

    NSLog(@"%f", self.myScrollView.contentOffset.y);
}

Now I want it to slide to the bottom of the page and fade.

thanks!

nick shmick
  • 905
  • 1
  • 9
  • 25

1 Answers1

1

EDIT: The animation now moves the label to the bottom of the view controller then fades it out.

You can use an animation block to move the label, then put another block inside the completion block to fade out the label, then remove it after the animation completes.

Example:

[UIView animateWithDuration:0.3 animations:^{
    [self.myLabel setFrame:CGRectMake(self.myLabel.frame.origin.x, self.view.frame.size.height - self.myLabel.frame.size.height, self.myLabel.frame.size.width, self.myLabel.frame.size.height)];
    self.labelRemoving = YES;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.3 animations:^{
        self.myLabel.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self.myLabel removeFromSuperview];
        self.myLabel = nil;
        self.labelRemoving = NO;
    }];
}];

Sidenote: You should probably be using <= instead of == in your if statement to achieve your desired results. In addition, you may want to set a flag to indicate that your label is being removed (since that method will inevitably be called multiple times). Something like this:

//.h
@property (nonatomic, assign) BOOL labelRemoving;

//.m
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (self.myScrollView.contentOffset.y <= -33 && !self.labelRemoving) {

        [UIView animateWithDuration:0.3 animations:^{
            [self.myLabel setFrame:CGRectMake(self.myLabel.frame.origin.x, self.view.frame.size.height - self.myLabel.frame.size.height, self.myLabel.frame.size.width, self.myLabel.frame.size.height)];
            self.labelRemoving = YES;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                self.myLabel.alpha = 0.0;
            } completion:^(BOOL finished) {
                [self.myLabel removeFromSuperview];
                self.myLabel = nil;
                self.labelRemoving = NO;
            }];
        }];
    }

    NSLog(@"%f", self.myScrollView.contentOffset.y);
}
rebello95
  • 8,486
  • 5
  • 44
  • 65
  • that is what I looked for, sorry if the animation type wasn't clear but this is almost what I meant, just now I want it to slide to the bottom bottom of the view controller and fade there..can you please add this? thanks !! – nick shmick Jan 06 '15 at 22:05
  • I updated the method in my question with your code suggestion – nick shmick Jan 06 '15 at 22:09
  • @nickshmick see my edit. It fades the label out while moving it to the bottom of the view. You should be able to figure out what you need from that. Please accept my answer if it helped – rebello95 Jan 06 '15 at 22:10
  • it still not sliding to the bottom bro, it is fading – nick shmick Jan 06 '15 at 22:16
  • now its fading out and sliding from the top to the place it was fading from. I would love to get it sliding to the bottom and fading there..please :) – nick shmick Jan 06 '15 at 22:44