常用的一些触屏操作(UITouch) 1人收藏此文章, 我要收藏 发表于3个月前(2013-01-14 14:57) , 已有32次阅读 ,共0个评论

轻击:

需要在你的ViewController里重写几个方法:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

messageLabel.text=@"Touches Began"; //开始触摸的方法

[selfupdateLabelsFromTouches:touches];

}

- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event{

messageLabel.text=@"Touches Cancelled";//触摸取消的方法

[selfupdateLabelsFromTouches:touches];

}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {

messageLabel.text=@"Touches Stopped.";//触摸结束的方法

[selfupdateLabelsFromTouches:touches];

}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {

messageLabel.text=@"Drag Detected";//触摸移动的方法

[selfupdateLabelsFromTouches:touches];

}

触摸-清扫:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

UITouch*touch = [touchesanyObject];

gestureStartPoint= [touchlocationInView:self.view];//开始触摸

}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {

UITouch*touch = [touchesanyObject];

CGPointcurrentPosition = [touchlocationInView:self.view];

CGFloatdeltaX =fabsf(gestureStartPoint.x- currentPosition.x);

CGFloatdeltaY =fabsf(gestureStartPoint.y- currentPosition.y);

if(deltaX >=kMinimumGestureLength&& deltaY <=kMaximumVariance) {

label.text=@"Horizontal swipe detected";

[selfperformSelector:@selector(eraseText)

withObject:nilafterDelay:2];

}

elseif(deltaY >=kMinimumGestureLength&&

deltaX <=kMaximumVariance){

label.text=@"Vertical swipe detected";

[selfperformSelector:@selector(eraseText)withObject:nil

afterDelay:2];

}

//kMinimumGestureLength 最小移动长度kMaximumVariance 最大偏移长度

}

多次轻击判断,比如双击,三击等:

- (void)singleTap {

singleLabel.text=@"Single Tap Detected";//单击动作响应事件

[selfperformSelector:@selector(eraseMe:)

withObject:singleLabelafterDelay:1.6f];//1.6秒后执行eraseMe方法

}

- (void)doubleTap {

doubleLabel.text=@"Double Tap Detected";//双击

[selfperformSelector:@selector(eraseMe:)

withObject:doubleLabelafterDelay:1.6f];

}

- (void)tripleTap {

tripleLabel.text=@"Triple Tap Detected";//三击

[selfperformSelector:@selector(eraseMe:)

withObject:tripleLabelafterDelay:1.6f];

}

- (void)quadrupleTap {

quadrupleLabel.text=@"Quadruple Tap Detected";//四击

[selfperformSelector:@selector(eraseMe:)

withObject:quadrupleLabelafterDelay:1.6f];

}

- (void)eraseMe:(UITextField*)textField {

textField.text=@"";

}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

UITouch*touch = [touchesanyObject];//实例一个uitouch

NSUIntegertapCount = [touchtapCount]; //计算touch的tapCount次数

switch(tapCount) {

case1:

[selfsingleTap];

break;

case2:

[selfdoubleTap];

break;

case3:

[selftripleTap];

break;

case4:

[selfquadrupleTap];

break;

default:

break;

}

}

[selfperformSelector:@selector(eraseMe:)withObject:singleLabelafterDelay:1.6f]中

performSelector:@selector(eraseMe:)withObject:singleLabelafterDelay:1.6f方法为将来afterDelay1.6秒之后调用eraseMe方法

同样的[NSObect cancelPreviousPerformSelector:withObject :afterDelay:];

方法为取消这些将来的调用

捏合操作:

- (void)eraseLabel {//清空lable

label.text=@"";

}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {//开始触碰

if([touchescount] ==2) {//检测是否为两个手指触点

NSArray*twoTouches = [touchesallObjects];

UITouch*first = [twoTouchesobjectAtIndex:0];

UITouch*second = [twoTouchesobjectAtIndex:1];

initialDistance=distanceBetweenPoints(

[firstlocationInView:self.view],

[secondlocationInView:self.view]);

}

}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {//移动手指

if([touchescount] ==2) {

NSArray*twoTouches = [touchesallObjects];

UITouch*first = [twoTouchesobjectAtIndex:0];

UITouch*second = [twoTouchesobjectAtIndex:1];

CGFloatcurrentDistance =distanceBetweenPoints(

[firstlocationInView:self.view],

[secondlocationInView:self.view]);

if(initialDistance==0)

initialDistance= currentDistance;//根据移动前后的坐标距离差检测是捏合的手势还是打开的手势

elseif(currentDistance -initialDistance>kMinimumPinchDelta) {//检测是否大于最小移动值kMinimumPinchDelta

label.text=@"Outward Pinch";

[selfperformSelector:@selector(eraseLabel)

withObject:nil

afterDelay:1.6f];

}

elseif(initialDistance- currentDistance >kMinimumPinchDelta) {

label.text=@"Inward Pinch";

[selfperformSelector:@selector(eraseLabel)

withObject:nil

afterDelay:1.6f];

}

}

}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {//触碰结束

initialDistance=0;

}

自定义手势“√”:

- (void)eraseLabel {

label.text=@"";

}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

UITouch*touch = [touchesanyObject];

CGPointpoint = [touchlocationInView:self.view];

lastPreviousPoint= point;

lastCurrentPoint= point;

lineLengthSoFar=0.0f;

}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {

UITouch*touch = [touchesanyObject];

CGPointpreviousPoint = [touchpreviousLocationInView:self.view];

CGPointcurrentPoint = [touchlocationInView:self.view];

CGFloatangle =angleBetweenLines(lastPreviousPoint,//计算两条线之间的角度

lastCurrentPoint,

previousPoint,

currentPoint);

if(angle >=kMinimumCheckMarkAngle&& angle <=kMaximumCheckMarkAngle

&&lineLengthSoFar>kMinimumCheckMarkLength) {//检测手势被承认的条件kMinimumCheckMarkAngle 最小角度kMaximumCheckMarkAngle最大角度kMinimumCheckMarkLength 画线最小长度

label.text=@"Checkmark";

[selfperformSelector:@selector(eraseLabel)

withObject:nilafterDelay:1.6];

}

lineLengthSoFar+=distanceBetweenPoints(previousPoint, currentPoint);//lineLengthSoFar ,lastPreviousPoint,lastCurrentPoint 重新赋值

lastPreviousPoint= previousPoint;

lastCurrentPoint= currentPoint;

}

这里用到一个判断两条直线的角度的方法angleBetweenLines(CGPointline1Start,CGPointline1End,CGPointline2Start,CGPointlin2End);

给一个几何方法集的接口方法:

CGPointUtils.h 头文件

#import<CoreGraphics/CoreGraphics.h>

CGFloatdistanceBetweenPoints (CGPointfirst,CGPointsecond);

CGFloatangleBetweenPoints(CGPointfirst,CGPointsecond);

CGFloatangleBetweenLines(CGPointline1Start,CGPointline1End,CGPointline2Start,CGPointlin2End);

.c文件CGPointUtils.c

#include"CGPointUtils.h"

#include<math.h>

#define pi3.14159265358979323846

#define degreesToRadian(x) (pi * x /180.0)

#define radiansToDegrees(x) (180.0* x / pi)

CGFloat distanceBetweenPoints (CGPointfirst,CGPointsecond) {

CGFloatdeltaX = second.x- first.x;

CGFloatdeltaY = second.y- first.y;

returnsqrt(deltaX*deltaX + deltaY*deltaY );

};

CGFloat angleBetweenPoints(CGPointfirst,CGPointsecond) {

CGFloatheight = second.y- first.y;

CGFloatwidth = first.x- second.x;

CGFloatrads =atan(height/width);

returnradiansToDegrees(rads);

//degs = degrees(atan((top - bottom)/(right - left)))

}

CGFloat angleBetweenLines(CGPointline1Start,CGPointline1End,CGPointline2Start,CGPointline2End) {

CGFloata = line1End.x- line1Start.x;

CGFloatb = line1End.y- line1Start.y;

CGFloatc = line2End.x- line2Start.x;

CGFloatd = line2End.y- line2Start.y;

CGFloatrads =acos(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));

returnradiansToDegrees(rads);

}

把他加到工程里 在需要的地方#import"CGPointUtils.h" 就可以直接用了