读书人

为UIView添加下上左右滑动操作

发布时间: 2012-08-28 12:37:01 作者: rapoo

为UIView添加上下左右滑动操作

方法一:
在viewDidLoad时为UIView添加操作

#define HORIZ_SWIPE_DRAG_MIN 12#define VERT_SWIPE_DRAG_MAX 4CGPoint startTouchPosition;NSString *dirString ;- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{UITouch *touch = touches.anyObject;startTouchPosition = [touch locationInView:self.view];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event    {        UITouch *touch = touches.anyObject;        CGPoint currentTouchPosition = [touch locationInView:self.view];    if (fabsf(startTouchPosition.x - currentTouchPosition.x) >=            HORIZ_SWIPE_DRAG_MIN &&            fabsf(startTouchPosition.y - currentTouchPosition.y) <=            VERT_SWIPE_DRAG_MAX)        {            // Horizontal Swipe           if (startTouchPosition.x < currentTouchPosition.x) {               NSLog(@"from left");   dirString = @"left";        }           else{                NSLog(@"from right");   dirString = @"right";}}       else if (fabsf(startTouchPosition.y - currentTouchPosition.y) >=                 HORIZ_SWIPE_DRAG_MIN &&                 fabsf(startTouchPosition.x - currentTouchPosition.x) <=                 VERT_SWIPE_DRAG_MAX)   {            // Vertical Swipe           if (startTouchPosition.y < currentTouchPosition.y) {   NSLog(@"from bottom");dirString = @"bottom";}        else    {NSLog(@"from top");dirString = @"top";}    } else        {   //dirString = @"";}   } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{if (dirString == @"right") {NSLog(@"event right");}else if (dirString == @"left") {NSLog(@"event left");}}

读书人网 >移动开发

热点推荐