0

UPDATED
I want to make running text line, which way is better to make it?

 -(void)viewDidLoad
    {
         CGSize mySize = CGSizeZero;
            mySize = [kGrindFMRunningText sizeWithFont:[UIFont fontWithName:@"Verdana" size:16]  constrainedToSize:CGSizeMake(4000, 30) lineBreakMode:UILineBreakModeWordWrap];

        runningText = [[UIScrollView alloc] initWithFrame:CGRectMake(60, -5, 260, 50)];
            grind_fm_text = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, mySize.width, 30)];
            [grind_fm_text setUserInteractionEnabled:NO];
            [grind_fm_text setBackgroundColor:[UIColor clearColor]];
            [grind_fm_text setTextColor:[UIColor whiteColor]];
            [grind_fm_text setFont:[UIFont fontWithName:@"Verdana" size:16]];
            [grind_fm_text setText:kGrindFMRunningText];
            [runningText addSubview:grind_fm_text];
            [grind_fm_text release];
            [self animate];
    }


       - (void)animate
{
    [UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState animations:^{
        grind_fm_text.frame = CGRectMake(-grind_fm_text.frame.size.width, 15, grind_fm_text.frame.size.width, grind_fm_text.frame.size.height);
        // Do your animation in one direction until text is gone
    } completion:^(BOOL finished){
        grind_fm_text.frame = CGRectMake(260, 15, grind_fm_text.frame.size.width, grind_fm_text.frame.size.height); 
        // Move scroll position back to original position
        [self animate]; // Then call animate again to repeat
    }];
}

-(void)songChange
{
    CGSize mySize = CGSizeZero;
    mySize = [result sizeWithFont:[UIFont fontWithName:@"Verdana" size:16]  constrainedToSize:CGSizeMake(4000, 30) lineBreakMode:UILineBreakModeWordWrap];
    grind_fm_text.frame = CGRectMake(grind_fm_text.frame.origin.x, 15, mySize.width, 30);
    grind_fm_text.text = result;;
    [self animate];
}

- (void)startStopStream {
        [streamer stop];
        //[bufferIndicator stopAnimating];
        [CATransaction begin];
        [self.view.layer removeAllAnimations];
        [CATransaction commit];
        grind_fm_text.text = kGrindFMRunningText;
        CGSize mySize = CGSizeZero;
        mySize = [kGrindFMRunningText sizeWithFont:[UIFont fontWithName:@"Verdana" size:16]  constrainedToSize:CGSizeMake(4000, 30) lineBreakMode:UILineBreakModeWordWrap];
        grind_fm_text.frame = CGRectMake(grind_fm_text.frame.origin.x, 15, mySize.width, 30);
        [self animate];
}

[CATransaction begin]; [myView.layer removeAllAnimations]; [CATransaction commit]; doesn't work for me. UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState works strangely: first, it doesn't do completion block as i see, or maybe it does, but animation doesn't start again. And If i click button nothing happening, but when i click it second time, animation begins from another direction and slows until freeze.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Aft3rmath
  • 669
  • 2
  • 12
  • 21

1 Answers1

1

You should be able to do this more simply with nested UIView animation blocks. In the animation block have it scroll in the one direction, in the completion block have it scroll in the other direction and in that animation's completion block have it call your animate function over again so it repeats.

Something like this:

- (void)animate
{
    [UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear animations:^{
        // Do your animation in one direction
    } completion:^(BOOL finished){
        [UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear animations:^{
            // Do your animation in the other direction
        } completion:^(BOOL finished){
            [self animate];
        }];
    }];
}

Or if you want it to scroll all the way across then do it again, something like:

- (void)animate
{
    [UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear animations:^{
        // Do your animation in one direction until text is gone
    } completion:^(BOOL finished){
        // Move scroll position back to original position
        [self animate]; // Then call animate again to repeat
    }];
}

It sounds like by default, animations use the UIViewAnimationOptionCurveEaseInOut animation option. You want the UIViewAnimationOptionCurveLinear option. I've updated my code above to include that.


As per an answer to this question: Cancel a UIView animation? you should be able to cancel the animation by calling [myView.layer removeAllAnimations]; where myView is the scroll view being animated. Make sure to import <QuartzCore/QuartzCore.h> at the top to access CALayer methods.

Edit: you may need to call it like this to make sure it runs immediately and not after the next runloop iteration:

[CATransaction begin];
[myView.layer removeAllAnimations];
[CATransaction commit];

Or if that still doesn't work, then likely just changing the options parameters in the UIView method calls to UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState should work, with no need to call removeAllAnimations. In fact, try that first.

Community
  • 1
  • 1
Ben Baron
  • 14,496
  • 12
  • 55
  • 65
  • Thanks a lot! It works for me, but when i'm using it with 10 sec. duration, animation seems to be with normal speed at beginning (when first letters showing) but then it accelerated, and when last couple letters is visible, it has normal speed again. Use second code sample with 10 sec duration. I'll update my question – Aft3rmath May 19 '12 at 21:07
  • You are great! And 1 more question, when I click button I need animation to start again from begin, how can I stop current animation, and then call [self animation] again? Because without current animation stopping [self animation] won't working – Aft3rmath May 19 '12 at 21:25
  • Thank you for your answers. `[CATransaction begin]; [myView.layer removeAllAnimations]; [CATransaction commit];` doesn't work for me. `UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState` works strangely: first, it doesn't do completion block as i see, or maybe it does, but animation doesn't start again. And If i click button nothing happening, but when i click it second time, animation begins from another direction and slows until freeze. I'll update question – Aft3rmath May 20 '12 at 09:37
  • Hm, and looks like on ios 4.3 and lower this eternal animation doesn't let other interface parts work =\ – Aft3rmath May 20 '12 at 10:42
  • Take a look at this: http://www.cocoabuilder.com/archive/cocoa/292930-continuous-animation-locks-ui.html you may need to add `UIViewAnimationOptionAllowUserInteraction` to the animation options – Ben Baron May 20 '12 at 18:21