locked
图片滑动问题 RRS feed

  • 问题

  • 有一图片,如电影胶片那样,是有很多张小图拼接起来的,怎么实现拨一下滑动好几张那样的效果,并且显示到屏幕上的是完整的3张或4张图,没有半张的情况,就是拨一下,若滚动了3.5张,会自动滚到4张或3张。

    现在我只实现了拖动,但是放开手指后的滚动效果怎么实现
    2012年4月25日 8:12

答案

  • 判断手指放开时的Point.X轴的值,当距离大于屏幕的一半,或者是3分之一,就执行滚动动画,如果没有,就执行回滚动画

    下面是WPF做的Slider的关键动画,你看看就明白了

    private void SliderPanel_MouseLeftButtonUp( object sender, System.Windows.Input.MouseButtonEventArgs e )
            {
                try
                {
                    this.MouseMove -= new System.Windows.Input.MouseEventHandler( SliderPanel_MouseMove );
    
                    this.MouseFinal = e.GetPosition( this );
                    this.ReleaseMouseCapture();
                    int tempWidth = (int) this.DesiredSize.Width / 4;
                    string oldValue = Counter.ToString();
                    string newValue = string.Empty;
                    if( (MouseFinal.X - MouseFirst.X) > 0 )
                    {
                        if( Math.Abs( MouseFinal.X - MouseFirst.X ) > tempWidth )
                        {
                            Counter = Counter + 1;
                            newValue = Counter.ToString();
                        }
                    }
                    else
                    {
                        if( Math.Abs( MouseFinal.X - MouseFirst.X ) > tempWidth )
                        {
                            Counter = Counter - 1;
                            newValue = Counter.ToString();
                        }
                    }
    
                    double pTo, pFrom;
                    pTo = Counter * this.DesiredSize.Width;
                    pFrom = (MouseFinal.X - MouseFirst.X) > 0 ? (pTo - this.DesiredSize.Width) + (MouseFinal.X - MouseFirst.X) : (pTo + this.DesiredSize.Width) + (MouseFinal.X - MouseFirst.X);
    
                    if( Math.Abs( MouseFinal.X - MouseFirst.X ) < tempWidth )
                        pFrom = pTo + (MouseFinal.X - MouseFirst.X);
    
                    if( Counter > 0 )
                    {
                        pTo = (Counter - 1) * this.DesiredSize.Width;
                        Counter = Counter - 1;
                        newValue = Counter.ToString();
                    }
                    else if( Counter <= Children.Count * -1 )
                    {
                        pTo = (Counter + 1) * this.DesiredSize.Width;
                        Counter = Counter + 1;
                        newValue = Counter.ToString();
                    }
    
                    for( int i = 0; i < Children.Count; i++ )
                    {
                        DoubleAnimation da = new DoubleAnimation( pFrom, pTo, new Duration( TimeSpan.FromSeconds( 0.3 ) ) );
    
                        ((TranslateTransform) Children [i].RenderTransform).BeginAnimation( TranslateTransform.XProperty, da );
                    }
    
                    SelectedIndex = Math.Abs( Counter );
                    if( this.Children.Count > 0 )
                    {
                        if( oldValue != newValue )
                        {
                            RaiseSelectionTabChangedEvent( oldValue, newValue );
                        }
                    }
    
                }
                catch
                {
                }
            }

    2012年4月25日 12:00

全部回复

  • 判断手指放开时的Point.X轴的值,当距离大于屏幕的一半,或者是3分之一,就执行滚动动画,如果没有,就执行回滚动画

    下面是WPF做的Slider的关键动画,你看看就明白了

    private void SliderPanel_MouseLeftButtonUp( object sender, System.Windows.Input.MouseButtonEventArgs e )
            {
                try
                {
                    this.MouseMove -= new System.Windows.Input.MouseEventHandler( SliderPanel_MouseMove );
    
                    this.MouseFinal = e.GetPosition( this );
                    this.ReleaseMouseCapture();
                    int tempWidth = (int) this.DesiredSize.Width / 4;
                    string oldValue = Counter.ToString();
                    string newValue = string.Empty;
                    if( (MouseFinal.X - MouseFirst.X) > 0 )
                    {
                        if( Math.Abs( MouseFinal.X - MouseFirst.X ) > tempWidth )
                        {
                            Counter = Counter + 1;
                            newValue = Counter.ToString();
                        }
                    }
                    else
                    {
                        if( Math.Abs( MouseFinal.X - MouseFirst.X ) > tempWidth )
                        {
                            Counter = Counter - 1;
                            newValue = Counter.ToString();
                        }
                    }
    
                    double pTo, pFrom;
                    pTo = Counter * this.DesiredSize.Width;
                    pFrom = (MouseFinal.X - MouseFirst.X) > 0 ? (pTo - this.DesiredSize.Width) + (MouseFinal.X - MouseFirst.X) : (pTo + this.DesiredSize.Width) + (MouseFinal.X - MouseFirst.X);
    
                    if( Math.Abs( MouseFinal.X - MouseFirst.X ) < tempWidth )
                        pFrom = pTo + (MouseFinal.X - MouseFirst.X);
    
                    if( Counter > 0 )
                    {
                        pTo = (Counter - 1) * this.DesiredSize.Width;
                        Counter = Counter - 1;
                        newValue = Counter.ToString();
                    }
                    else if( Counter <= Children.Count * -1 )
                    {
                        pTo = (Counter + 1) * this.DesiredSize.Width;
                        Counter = Counter + 1;
                        newValue = Counter.ToString();
                    }
    
                    for( int i = 0; i < Children.Count; i++ )
                    {
                        DoubleAnimation da = new DoubleAnimation( pFrom, pTo, new Duration( TimeSpan.FromSeconds( 0.3 ) ) );
    
                        ((TranslateTransform) Children [i].RenderTransform).BeginAnimation( TranslateTransform.XProperty, da );
                    }
    
                    SelectedIndex = Math.Abs( Counter );
                    if( this.Children.Count > 0 )
                    {
                        if( oldValue != newValue )
                        {
                            RaiseSelectionTabChangedEvent( oldValue, newValue );
                        }
                    }
    
                }
                catch
                {
                }
            }

    2012年4月25日 12:00
  • 那种拼接使用什么做的呢?Scrollview,还是?
    2012年8月24日 10:23