UIView.animateWithDuration on UITextfield's co

2019-07-19 03:04发布

I use the following code:

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

If the contentOffset is up to about 100, the animation works correctly and all text is visible. Everything higher than that leads to the disappearance of text at the beginning of the textlabel during the animation. The higher the contentOffset, the more text disappears during the animation. So I see white space for a while and then the remaining text comes into animation. Also: Once the animation is done, all text is visible again when I scroll up.

I have tried multiple UITextviews in various superviews. The general idea is a kind of Credits like animation where all text moves slowly upwards for about 30 seconds.

2条回答
Luminary・发光体
2楼-- · 2019-07-19 03:35

You can try looping small animations together like the following.

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楼-- · 2019-07-19 03:56

This still appears to be an issue even in iOS 10 SDK.

To work around it, I manually animated the scrolling using setContentOffset:, delaying each step using performSelector: withObject: afterDelay: and implementing a method that scrolls one increment at a time (in my case 1/2 point).

To prevent users from manually overriding the scroll animation, you have to manually disable user interaction (animateWithDuration: automatically disables user scrolling while the animation is playing), you also must have scrolling enabled. I also disabled selection and editing, because that fit 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];
}
查看更多
登录 后发表回答