UIView.animateWithDuration in UITextfield contentoffset: it will copy the text (Swift)

I am using the following code:

    UIView.animateWithDuration(30, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
        self.textView.contentOffset = CGPointMake(0, 700)
        }, completion: nil)

      

If the contentOffset is about 100, the animation works correctly and all the text is visible. Anything higher than this causes the text to disappear at the start of the text label during animation. The higher the contentOffset value, the more text disappears during animation. This way I see the gap for a while and then the remaining text goes into animation. Also: After the animation ends, all the text is shown again when scrolling up.

I tried several UITextviews in different supervisors. The general idea is a kind of Credits like animation where all the text moves slowly upwards for about 30 seconds.

+1


source to share


2 answers


You can try to combine small animations together as shown below.



func animate(count:Int)
{
    if (count > 100)
    {
        return
    }
    UIView.animateWithDuration(1, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {

        self.textView.contentOffset = CGPointMake(0, self.textView.contentOffset.y + 10)
        }, completion: { finished in
            dispatch_async(dispatch_get_main_queue(),{
                self.animate(count+1)
        })
    })

}

      

+3


source


This still seems to be a problem even in the IOS 10 SDK.

To get around this, I manually animated the scrolling with setContentOffset:

, delaying each step with, performSelector: withObject: afterDelay:

and applying a method that scrolls one step at a time (1/2 point in my case).

To prevent users from manually overriding the scroll animation, you need to manually disable user interaction ( animateWithDuration:

automatically disables user scrolling while the animation is playing), you must also have scrolling. I also turned off highlighting and editing because it matched my use:

Note: Objective-C code follows



textBox.editable = NO;
textBox.selectable = NO;
textBox.scrollEnabled = YES;
textBox.userInteractionEnabled = NO;

float contentLength = textBox.contentSize.height - textBox.frame.size.height;
float timePerLine = audioLength / contentLength;

for (float i = 0; i < contentLength; i+=0.5){
    [self performSelector:@selector(scrollOneLine) withObject:nil afterDelay:timePerLine * i];
}

      

And then implemented a method to be used as a selector to scroll one increment at a time

-(void) scrollOneLine {
    float currentPosition = textBox.contentOffset.y;
    [textBox setContentOffset:CGPointMake(0, currentPosition + 0.5) animated:NO];
}

      

0


source







All Articles