//轻击://需要在你的VIEwController里重写几个方法://开始触摸的方法- (voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {messageLabel.text = @”touches Began”;[self updateLabelsFromtouches:touches];}//触摸取消的方法- (voID)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{messageLabel.text = @”touches Cancelled”;[self updateLabelsFromtouches:touches];}//触摸结束的方法- (voID)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {messageLabel.text = @”touches Stopped.”;[self updateLabelsFromtouches:touches];}//触摸移动的方法- (voID)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {messageLabel.text = @”Drag Detected”;[self updateLabelsFromtouches:touches];}//触摸-清扫://开始触摸- (voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {UItouch *touch = [touches anyObject];gestureStartPoint = [touch locationInVIEw:self.vIEw];}//kMinimumGestureLength 最小移动长度 kMaximumVariance最大偏移长度- (voID)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {UItouch *touch = [touches anyObject];CGPoint currentposition = [touchlocationInVIEw:self.vIEw];CGfloat deltaX = fabsf(gestureStartPoint.x - currentposition.x);CGfloat deltaY = fabsf(gestureStartPoint.y - currentposition.y);if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {label.text = @”Horizontal swipe detected”;[self performSelector:@selector(eraseText) withObject:nilafterDelay:2];}else if (deltaY >= kMinimumGestureLength &&deltaX <= kMaximumVariance){label.text = @”Vertical swipe detected”;[selfperformSelector:@selector(eraseText)withObject:nilafterDelay:2];}}//多次轻击判断,比如双击,三击等://单击动作响应事件- (voID)singleTap {singleLabel.text = @”Single Tap Detected”;[selfperformSelector:@selector(eraseMe:) withObject:singleLabel afterDelay:1.6f];//1.6秒后执行eraseMe方法}//双击- (voID)doubleTap{doubleLabel.text = @”Double Tap Detected”;[selfperformSelector:@selector(eraseMe:) withObject:doubleLabel afterDelay:1.6f];}//三击- (voID)tripleTap {tripleLabel.text = @”Triple Tap Detected”;[selfperformSelector:@selector(eraseMe:) withObject:tripleLabel afterDelay:1.6f];}//四击- (voID)quadrupleTap {quadrupleLabel.text = @”Quadruple Tap Detected”;[selfperformSelector:@selector(eraseMe:) withObject:quadrupleLabel afterDelay:1.6f];}- (voID)eraseMe:(UITextFIEld *)textFIEld {textFIEld.text = @"";}- (voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {UItouch *touch = [touches anyObject]; //实例一个uitouchNSUInteger tapCount = [touch tapCount]; //计算touch的tapCount次数switch (tapCount){case 1:[selfsingleTap];break;case 2:[selfdoubleTap];break;case 3:[selftripleTap];break;case 4:[selfquadrupleTap];break;default:break;}}//[self performSelector:@selector(eraseMe:)withObject:singleLabel afterDelay:1.6f]中//performSelector:@selector(eraseMe:)withObject:singleLabel afterDelay:1.6f方法为将来afterDelay1.6秒之后调用eraseMe方法//同样的[NSObect cancelPrevIoUsPerformSelector:withObject :afterDelay:];//方法为取消这些将来的调用//捏合 *** 作:- (voID)eraseLabel { //清空lablelabel.text = @"";}//开始触碰- (voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {if([touches count] ==2){ //检测是否为两个手指触点NSArray *twotouches = [touchesallObjects];UItouch *first = [twotouchesobjectAtIndex:0];UItouch *second = [twotouchesobjectAtIndex:1];initialdistance = distanceBetweenPoints([first locationInVIEw:self.vIEw],[secondlocationInVIEw:self.vIEw]);}}//移动手指- (voID)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {if ([touches count] == 2) {NSArray *twotouches = [touchesallObjects];UItouch *first = [twotouchesobjectAtIndex:0];UItouch *second = [twotouchesobjectAtIndex:1];CGfloat currentdistance =distanceBetweenPoints([first locationInVIEw:self.vIEw],[secondlocationInVIEw:self.vIEw]);if (initialdistance ==0){initialdistance = currentdistance;//根据移动前后的坐标距离差检测是捏合的手势还是打开的手势}else if (currentdistance - initialdistance > kMinimumPinchDelta) { //检测是否大于最小移动值kMinimumPinchDeltalabel.text = @”Outward Pinch”;[selfperformSelector:@selector(eraseLabel) withObject:nil afterDelay:1.6f];}else if (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 = [touches anyObject];CGPoint point = [touch locationInVIEw:self.vIEw];lastPrevIoUsPoint = point;lastCurrentPoint = point;lineLengthSoFar = 0.0f;}- (voID)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {UItouch *touch = [touches anyObject];CGPoint prevIoUsPoint = [touchprevIoUsLocationInVIEw:self.vIEw];CGPoint currentPoint = [touch locationInVIEw:self.vIEw];//计算两条线之间的角度CGfloat angle = angleBetweenlines(lastPrevIoUsPoint,lastCurrentPoint,prevIoUsPoint,currentPoint);//检测手势被承认的条件 kMinimumcheckmarkAngle最小角度//kMaximumcheckmarkAngle最大角度//kMinimumcheckmarkLength画线最小长度if (angle >= kMinimumcheckmarkAngle && angle <= kMaximumcheckmarkAngle && lineLengthSoFar > kMinimumcheckmarkLength) {label.text = @”checkmark”;[selfperformSelector:@selector(eraseLabel) withObject:nil afterDelay:1.6];}//lineLengthSoFar,lastPrevIoUsPoint,lastCurrentPoint重新赋值lineLengthSoFar += distanceBetweenPoints(prevIoUsPoint,currentPoint);lastPrevIoUsPoint = prevIoUsPoint;lastCurrentPoint = currentPoint;}//这里用到一个判断两条直线的角度的方法//angleBetweenlines(CGPoint line1Start,CGPoint line1End,CGPoint line2Start,CGPointlin2End);//给一个几何方法集的接口方法://CGPointUtils.h 头文件#import <CoreGraphics/CoreGraphics.h>CGfloat distanceBetweenPoints (CGPoint first,CGPoint second);CGfloat angleBetweenPoints(CGPoint first,CGPoint second);CGfloat angleBetweenlines(CGPoint line1Start,CGPoint lin2End);//.c文件 CGPointUtils.c#include ”CGPointUtils.h”#include <math.h>#define pi 3.14159265358979323846#define degreesToRadian(x) (pi * x / 180.0)#define radiansTodegrees(x) (180.0 * x / pi)CGfloat distanceBetweenPoints (CGPoint first,CGPoint second) {CGfloat deltaX = second.x - first.x;CGfloat deltaY = second.y - first.y;return sqrt(deltaX*deltaX + deltaY*deltaY );};CGfloat angleBetweenPoints(CGPoint first,CGPoint second){CGfloat height = second.y - first.y;CGfloat wIDth = first.x - second.x;CGfloat rads = atan(height/wIDth);returnradiansTodegrees(rads);//degs = degrees(atan((top – bottom)/(right – left)))}CGfloat angleBetweenlines(CGPoint line1Start,CGPoint line2End) {CGfloat a = line1End.x - line1Start.x;CGfloat b = line1End.y - line1Start.y;CGfloat c = line2End.x - line2Start.x;CGfloat d = line2End.y - line2Start.y;CGfloat rads = acos(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));returnradiansTodegrees(rads);}总结
以上是内存溢出为你收集整理的iphone开发:关于触屏事件的一些 *** 作全部内容,希望文章能够帮你解决iphone开发:关于触屏事件的一些 *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)