简体   繁体   English

自动滚动UIScrollView,允许用户中断动画

[英]Autoscroll UIScrollView, allow user to interrupt animation

I'm trying to animate a bunch of images in a UIScrollView. 我正在尝试对UIScrollView中的一堆图像进行动画处理。 The images should start scrolling when the screen shows, which I can do. 屏幕显示时,图像应开始滚动,我可以这样做。 At the moment I'm using UIView animations as follows: 目前,我正在使用UIView动画,如下所示:

    [Export("ScrollToRight")]
    private void ScrollRight()
    {
        if (!imagesShouldScroll)
            return;
        UIView.BeginAnimations("ScrollRight");
        UIView.SetAnimationCurve(UIViewAnimationCurve.Linear);
        UIView.SetAnimationDuration(test.Count * 10);
        scrlImages.ContentOffset = new PointF(((scrlImages.Frame.Height + 2) * test.Count) - scrlImages.Frame.Width, 0);
        UIView.SetAnimationDelegate(this);
        UIView.SetAnimationDidStopSelector(new Selector("ScrollToLeft"));
        UIView.CommitAnimations();
    }

    [Export("ScrollToLeft")]
    private void ScrollLeft()
    {
        if (!imagesShouldScroll)
            return;
        UIView.BeginAnimations("ScrollLeft");
        UIView.SetAnimationCurve(UIViewAnimationCurve.Linear);
        UIView.SetAnimationDuration(test.Count * 10);
        scrlImages.ContentOffset = new PointF(0, 0);
        UIView.SetAnimationDelegate(this);
        UIView.SetAnimationDidStopSelector(new Selector("ScrollToRight"));
        UIView.CommitAnimations();
    }

This works perfectly to scroll back and forth in the UIScrollView. 这非常适合在UIScrollView中来回滚动。 What I need to be able to do is let the user interrupt the animation by swiping on the ScrollView as they would normally scroll through the list. 我需要做的就是让用户通过像往常一样滚动列表的方式在ScrollView上滑动来中断动画。 So I have attached a listener to the ScrollView as follows: 因此,我将侦听器附加到ScrollView,如下所示:

        scrlImages.DraggingStarted += (sender, e) => 
        {
           if (imagesShouldScroll)
           {
               scrlImages.Layer.RemoveAllAnimations();
               imagesShouldScroll = false;
           }
        };

This works to stop the animations as expected. 这样可以按预期停止动画。 The problem is that the ContentOffset has already been set to the edge where it's busy animating to. 问题在于ContentOffset已经设置为忙于动画的边缘。 So when the user swipes the ScrollView it suddenly jumps to the end of the list. 因此,当用户滑动ScrollView时,它突然跳到列表的末尾。 I'd like the user to be able to stop the animation where it is and take over the scrolling manually. 我希望用户能够将动画停在原处并手动接管滚动。 I'm thinking there should be a way to use the animation's start time and the current graphics time to determine how long the animation has been running to determine where the ContentOffset would have been at that time, then jumping back there. 我在想应该有一种方法可以使用动画的开始时间和当前图形时间来确定动画已经运行了多长时间,以确定当时ContentOffset会在哪里,然后跳回那里。 That sounds very hacky to me though, less than ideal. 不过,对我来说,这听起来很棘手,不尽人意。

Note: I know I could just have used UIView.Animate with UIViewAnimationOptions.Autoreverse, I just didn't know about that at the time. 注意:我知道我可以将UIView.Animate与UIViewAnimationOptions.Autoreverse一起使用,当时我还不知道。 I've also been able to implement the behaviour I want using a timer to set the ContentOffset periodically. 我还能够使用计时器来实现我想要的行为,以定期设置ContentOffset。 It's CPU bound though, so it's not very smooth. 虽然这是受CPU限制的,所以它不是很流畅。

在从滚动视图的图层中删除所有动画之前,请从其presentationLayer读取bounds.origin并将其contentOffset设置为该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM