Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoscrolling feature for UILabel

i have google and search for it but didn't find anything on the internet to solve my problem but i think my problem is going to be super easy to solve but i can't find the right keyword for it.

I have a viewcontroller that contains a text called ReadingView ,a view that has a very long text to be able to scroll to read text. When user go into ReadingView , it will autoscroll down to the bottom by itself, user can increase speed of scrolling by UISlider that i implemented

the problem is I CAN do autoscrolling without any problem by this code

 [UIScrollView beginAnimations:@"scrollAnimation" context:nil];
 [UIScrollView setAnimationDuration:1.0f];
 [scroll setContentOffset:CGPointMake(x, y)];
 [UIScrollView commitAnimations];

but the code aboved is based on time. I have to find a animation based on frame or pixel. Because , some text is very long if i used animation based on time it will scroll down to the bottom very very fast, while the short text will be very slow.

Do you guys have any idea if i can do animation like

"Hey scrollview, every FRAME you have to move down by 10 pixel until the end of context and i don't care how long does it take, Fixed the speed!!"

Thank you for your help, you guys could help me by giving me a keyword or anything

UPDATE Done by Fogmeister's recommend. The concept is use block animation and calculate the time of each text by using time = text length/speed and you will get different time of each text and use that time to do animation. I never think of this.... what i'm looking for all the time is function to do animation by PIXEL which is a wrong keyword.

CGFloat scrollingPointSpeed = myslider.value; //value of UI Slider//
CGFloat time = offset/scrollingPointSpeed ;
[UIView animateWithDuration:time delay:0.0 options:UIViewAnimationOptionCurveLinear animation:^()
{
 self.scroll.contentOffset = CGPointMake(0, offset);
}
completion:nil];
like image 997
Einzeln Avatar asked Oct 02 '22 12:10

Einzeln


1 Answers

OK, first, you shouldn't be using CABasicAnimation. The best practise method is to use the block based animations.

Second, this should be fairly easy.

You can calculate the maximum offset of a scroll view...

CGFloat maxOffsetWidth = self.scrollView.contentSize.width - CGRectGetWidth(self.scrollView.frame);

This is the maximum amount of scroll that can be applied. i.e. the distance.

Now, to work out a duration (time) you need to have a speed in mind. Lets say 50 points per second.

So now lets put it together...

CGFloat maxOffsetWidth = self.scrollView.contentSize.width - CGRectGetWidth(self.scrollView.frame);

CGFloat scrollingPointsPerSecond = 50.0;

CGFloat time = maxOffsetWidth / scrollPointsPerSecond;

[UIView animateWithDuration:time
                 animations:^() {
    self.scrollView.contentOffset = CGPointMake(maxOffsetWidth, 0);
}];

Obviously, this is for horizontal scrolling, if you want vertical then just use the different values.

QUICK EDIT

The above animation function uses the default animation curve which is UIAnimationOptionCurveEaseInOut. This will mean that the speed of the scroll will slowly accelerate from 0 at the beginning and then slowly decelerate back to 0 at the end.

What you will probably want is a linear motion. i.e. the speed of the scroll is the same all the way through. To do this you need a slightly different function from the one above. It just adds a couple extra parameters...

[UIView animateWithDuration:time
                      delay:0.0
                    options:UIViewAnimationOptionCurveLinear
                  animation:^() {
                      self.scrollView.contentOffset = CGPointMake(maxOffsetWidth, 0);
                  }
                 completion:nil];

This will do exactly what you want with a linear movement.

like image 191
Fogmeister Avatar answered Oct 11 '22 15:10

Fogmeister