[ActionScript 3.0] AS3 用于拖动对象时一次一页的缓动

package com.fylibs.components.effects{
    import com.tweener.transitions.Tweener;
    
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.InteractiveObject;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;

    /**
     * 用于拖动时一次一页的缓动
     * @author Frost.Yen
     * @E-mail [email protected]
     * @create 2015-12-11 下午5:06:57
     *
     */
    public class DragMovePage
    {
        private var _direction:String = "horizontal";
        /**
         * 按下的坐标(当horizontal时为x坐标,当vertical时为y坐标)
         */
        private var _downCoor:Number;
        /**
         * 移动的坐标(当horizontal时为x坐标,当vertical时为y坐标)
         */
        private var _moveCoor:Number;
        /**
         * 坐标标记((当horizontal时为mouseX,当vertical时为mouseY))
         */
        private var _mouseCoor:String = "mouseX";

        /**
         * 标记是否按下状态
         */
        private var _isDown:Boolean;
        
        private var _slidingDis:Number;
        private var _slidingNum:Number = 300;
        private var _page:uint;
        private var _dis:uint;
        private var _count:int;
        private var _target:Sprite;
        private var _dotBar:Sprite;
        /**
         * 拖拽缓动
         * @param target  需要拖拽的对象
         * @param page  页数
         * @param dis 每页的距离
         * @param direction  缓动方向
         * @param dotBar 指示当前页的小圆点
         */
        public function DragMovePage(target:Sprite,page:uint,dis:uint,direction:String="horizontal",dotBar:Sprite=null)
        {
            _page = page;
            _dis = dis;
            _direction = direction;
            _dotBar = dotBar;
            _direction == "horizontal"?_mouseCoor = "mouseX":_mouseCoor = "mouseY";
            _target = target;
            _target.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
        }
        /**
         * 拖拽缓动
         * @param page  页数
         * @param dis 每页的距离
         */
        public function refresh(page:uint,dis:uint):void
        {
            _page = page;
            _dis = dis;
        }
        private function onDown(e:MouseEvent):void
        {
            Tweener.removeTweens(_target);
            _isDown = true;
            _downCoor = _target.parent[_mouseCoor];
            //注意,-(_page)*_dis中为什么是page,而不是page-1
            _direction == "horizontal"?_target.startDrag(false,new Rectangle(-(_page)*_dis,_target.y,(_page+1)*_dis,0)):_target.startDrag(false,new Rectangle(_target.x,-(_page)*_dis,0,(_page+1)*_dis));
            _target.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
            _target.stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
        }
        private function onMove(e:MouseEvent):void
        {
            _moveCoor = _target.parent[_mouseCoor];
        }
        private function onUp(e:MouseEvent):void
        {
            _isDown = false;
            _moveCoor = _target.parent[_mouseCoor];
            _target.stopDrag();
            _target.stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
            _target.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
            _slidingDis = _downCoor-_moveCoor;
            if(_slidingDis>_slidingNum){
                _count++;
            }else if(_slidingDis<-_slidingNum){
                _count--;
            }
            if(_count>=_page){
                _count = _page-1;
            }else if(_count<0){
                _count = 0;
            }
            _direction == "horizontal"?Tweener.addTween(_target,{x:-_count*_dis,time:0.7,transition:"easeOutCubic",onComplete:moveEnd}):Tweener.addTween(_target,{y:-_count*_dis,time:0.7,transition:"easeOutCubic",onComplete:moveEnd});
        }
        private function moveEnd():void
        {
            if(_dotBar){
                for(var i:int = 0;i<_dotBar.numChildren;i++){
                    if(_count == i){
                        _dotBar.getChildAt(i).alpha = 1;
                    }else{
                        _dotBar.getChildAt(i).alpha = 0.5;    
                    }
                }
            }
        }
        /**
         * 移动方向,horizontal横向,vertical纵向
         */
        public function get direction():String
        {
            return _direction;
        }
        
        /**
         * @private
         */
        public function set direction(value:String):void
        {
            _direction = value;
        }

        /**
         * 手指或者鼠标滑动的实际矢量距离(有方向)
         */
        public function get slidingDis():Number
        {
            return _slidingDis;
        }
        /**
         *  当手指或者鼠标划过的距离大于这个值时才识别为翻页
         */
        public function get slidingNum():Number
        {
            return _slidingNum;
        }

        /**
         * @private
         */
        public function set slidingNum(value:Number):void
        {
            _slidingNum = value;
        }


    }
}

 

你可能感兴趣的:([ActionScript 3.0] AS3 用于拖动对象时一次一页的缓动)