只要手指触摸屏幕,滑动,从屏幕离开,系统都会产生UIEvent对象类型的事件---当然包括UITouch事件 – touchesBegan:withEvent: 当用户触摸到屏幕时调用方法 – touchesMoved:withEvent: 当用户触摸到屏幕并移动时调用此方法 – touchesEnded:withEvent: 当触摸离开屏幕时调用此方法
– touchesCancelled:withEvent: 当触摸被取消时调用此方法
例子如下:
Cpp代码//滑动开始事件 - (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]; } } } } }