首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

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

2012-08-28 
为UIView添加上下左右滑动操作方法一:在viewDidLoad时为UIView添加操作#define HORIZ_SWIPE_DRAG_MIN 12#d

为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");}}

热点排行