Ios - Scrolling Text - Custom Animation

I searched the internet for several hours and I could not find a good solution.

I am trying to scroll text on my UIView from left to right and vice versa. I do not want to use CoreAnimations as I need to control the animation.

The movement follows the sine curve, from 0 to pi or 0-> 1-> 0.

What is the best way to implement this custom animation?

What I came up with after some research is the recovery algorithm, which calls it on its own and goes through the loop until it ends.

- (void) scrollText:(id)sender timeConstant:(float) _timeconstant timeOffset:(NSDate*) _timeoffset direction:(int)_direction { float p = [[NSDate date] timeIntervalSinceDate:_timeoffset] / _timeconstant * _direction; float ps = sinf( p * M_PI); CGSize txtsize = [msg_text sizeWithFont:font]; float offset = txtsize.width / 2; float screenwidth = self.view.frame.size.width; float screenheight= self.view.frame.size.height; int virtualwidth = txtsize.width; [ivText setFrame:CGRectMake(screenwidth/2 - offset + (virtualwidth / 2 * ps), 100, txtsize.width, txtsize.height)]; //redraw [[self view] setNeedsDisplay]; //keep going if (ps * _direction > 0) [self scrollText:self timeConstant:_timeconstant timeOffset:_timeoffset direction:_direction]; } 

Now the problem is that the view will not be updated :( And Im not sure if this is the best approach for this kind of animation.

+4
source share
1 answer

I think your view is not updated, because you never exit recursion, and therefore the loop cycle never ends the iteration. A better solution than a recursive method would be to use NSTimer , which disables your function once, say, 1/30 or 1/60 of a second, using +[NSTimer timerWithTimeInterval:target:selector:userInfo:repeats:] .

If you want to preserve the recursive structure that you are here, you can use -[NSObject performSelector:withObject:afterDelay:] to call your function after a delay. Just use something like 1 / 60.0 for the delay and pack any information you need into a dictionary or array to go to the withObject: parameter.

Any of these methods will register your method with a run loop, which will be called later. This allows you to start the start cycle of a new iteration, which means that user interface updates can occur.

Edit: if your line does not change during this animation, calculate the text size once and save it in the instance variable. Calculating the row size during each call to your method can be expensive.

+1
source

All Articles