2013-10-11 37 views
0

我想在UIScrollView中动画一堆图像。当屏幕显示时,图像应该开始滚动,我可以做。目前我使用的UIView动画如下:Autoscroll UIScrollView,允许用户中断动画

[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(); 
    } 

这工作完全在UIScrollView中来回滚动。 我需要做的是让用户通过在ScrollView上滑动来中断动画,就像他们通常在列表中滚动一样。所以我附加了一个听众ScrollView如下:

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

这可以按预期停止动画。问题在于ContentOffset已经被设置为忙于动画的边缘。所以当用户滑动ScrollView时,它突然跳到列表的末尾。我希望用户能够停止它的动画并手动接管滚动。我认为应该有一种方法来使用动画的开始时间和当前的图形时间来确定动画运行多长时间以确定ContentOffset在那时的位置,然后跳回到那里。尽管这听起来很不合理,但并不理想。

注意:我知道我可以使用UIView.Animate与UIViewAnimationOptions.Autoreverse,我当时不知道这一点。我也已经能够实现我想要使用定时器定期设置ContentOffset的行为。这是CPU绑定,所以它不是很顺利。

回答

0

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

+0

工程就像一个魅力!我可能应该在API中挖掘更多。感谢您的帮助。 – Iyashu5040

+0

非常欢迎。除非你已经知道你在找什么,否则通常很难知道去哪里寻找! – hatfinch