IOS设备滑动事件

只要手指触摸屏幕,滑动,从屏幕离开,系统都会产生UIEvent对象类型的事件---当然包括UITouch事件 – touchesBegan:withEvent:   当用户触摸到屏幕时调用方法 – touchesMoved:withEvent:  当用户触摸到屏幕并移动时调用此方法 – touchesEnded:withEvent:  当触摸离开屏幕时调用此方法

– touchesCancelled:withEvent:  当触摸被取消时调用此方法

 例子如下:

Cpp代码  
  1. //滑动开始事件  
  2. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event    
  3. {    
  4.     UITouch *touch = [touches anyObject];    
  5.     CGPoint pointone = [touch locationInView:self.view];//获得初始的接触点    
  6.     self.startPoint  = pointone;    
  7. }  
  8. //滑动移动事件  
  9. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event    
  10. {    
  11.     UITouch *touch = [touches anyObject];    
  12. //imgViewTop是滑动后最后接触的View  
  13.         CGPoint pointtwo = [touch locationInView:imgViewTop];  //获得滑动后最后接触屏幕的点    
  14.           
  15.         if(fabs(pointtwo.x-startPoint.x)>100)  
  16.         {  //判断两点间的距离    
  17.             bMove = YES;  
  18.         }   
  19. }    
  20. //滑动结束处理事件  
  21. -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  22. {  
  23.     UITouch *touch = [touches anyObject];  
  24.         CGPoint pointtwo = [touch locationInView:self.view];  //获得滑动后最后接触屏幕的点   
  25.         if((fabs(pointtwo.x-startPoint.x)>50)&&(bMove))  
  26.         {  
  27.             //判断点的位置关系 左滑动  
  28.             if(pointtwo.x-startPoint.x>0)  
  29.             {   //左滑动业务处理  
  30.                 if (clicks>0) {  
  31.                     clicks--;  
  32.                     if ([wyKPI.sortZbs count]>1) {  
  33.                         [btnRight setEnabled:YES];  
  34.                         if(clicks==0)  
  35.                         {  
  36.                             [btnLeft setEnabled:NO];  
  37.                             [btnRight setEnabled:YES];  
  38.                         }  
  39.                         [labTitle setText:[wyKPI.sortZbs objectAtIndex:(NSUInteger)clicks]];  
  40.                         [labTitle setFont:[UIFont systemFontOfSize:14.0f]];  
  41.                         [tabWyKPI reloadData];  
  42.                     }  
  43.                 }  
  44.             }  
  45.             //判断点的位置关系 右滑动  
  46.             else  
  47.             {  //右滑动业务处理  
  48.                 if (clicks<[wyKPI.sortZbs count]-1) {  
  49.                     clicks++;  
  50.                     if ([wyKPI.sortZbs count]>1) {  
  51.                         [btnLeft setEnabled:YES];  
  52.                         if(clicks==[wyKPI.sortZbs count]-1)  
  53.                         {  
  54.                             [btnLeft setEnabled:YES];  
  55.                             [btnRight setEnabled:NO];  
  56.                         }  
  57.                         [labTitle setText:[wyKPI.sortZbs  objectAtIndex:clicks]];  
  58.                         [labTitle setFont:[UIFont systemFontOfSize:14.0f]];  
  59.                         [tabWyKPI reloadData];  
  60.                     }  
  61.                 }  
  62.             }    
  63.         }  
  64. }  
//滑动开始事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
{  
    UITouch *touch = [touches anyObject];  
    CGPoint pointone = [touch locationInView:self.view];//获得初始的接触点  
    self.startPoint  = pointone;  
}
//滑动移动事件
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
{  
    UITouch *touch = [touches anyObject];  
//imgViewTop是滑动后最后接触的View
        CGPoint pointtwo = [touch locationInView:imgViewTop];  //获得滑动后最后接触屏幕的点  
        
        if(fabs(pointtwo.x-startPoint.x)>100)
        {  //判断两点间的距离  
            bMove = YES;
        } 
}  
//滑动结束处理事件
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
        CGPoint pointtwo = [touch locationInView:self.view];  //获得滑动后最后接触屏幕的点 
        if((fabs(pointtwo.x-startPoint.x)>50)&&(bMove))
        {
            //判断点的位置关系 左滑动
            if(pointtwo.x-startPoint.x>0)
            {   //左滑动业务处理
                if (clicks>0) {
                    clicks--;
                    if ([wyKPI.sortZbs count]>1) {
                        [btnRight setEnabled:YES];
                        if(clicks==0)
                        {
                            [btnLeft setEnabled:NO];
                            [btnRight setEnabled:YES];
                        }
                        [labTitle setText:[wyKPI.sortZbs objectAtIndex:(NSUInteger)clicks]];
                        [labTitle setFont:[UIFont systemFontOfSize:14.0f]];
                        [tabWyKPI reloadData];
                    }
                }
            }
            //判断点的位置关系 右滑动
            else
            {  //右滑动业务处理
                if (clicks<[wyKPI.sortZbs count]-1) {
                    clicks++;
                    if ([wyKPI.sortZbs count]>1) {
                        [btnLeft setEnabled:YES];
                        if(clicks==[wyKPI.sortZbs count]-1)
                        {
                            [btnLeft setEnabled:YES];
                            [btnRight setEnabled:NO];
                        }
                        [labTitle setText:[wyKPI.sortZbs  objectAtIndex:clicks]];
                        [labTitle setFont:[UIFont systemFontOfSize:14.0f]];
                        [tabWyKPI reloadData];
                    }
                }
            }  
        }
}

 

你可能感兴趣的:(ios)