您好,登錄后才能下訂單哦!
輕擊:
需要在你的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];//開始觸摸
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView: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:nil afterDelay:2];
}
else if (deltaY >= kMinimumGestureLength &&
deltaX <= kMaximumVariance){
label.text = @"Vertical swipe detected";
[self performSelector:@selector(eraseText) withObject:nil
afterDelay:2];
}
//kMinimumGestureLength 最小移動長度 kMaximumVariance 最大偏移長度
}
多次輕擊判斷,比如雙擊,三擊等:
- (void)singleTap {
singleLabel.text = @"Single Tap Detected";//單擊動作響應事件
[self performSelector:@selector(eraseMe:)
withObject:singleLabel afterDelay:1.6f];//1.6秒后執行eraseMe方法
}
- (void)doubleTap {
doubleLabel.text = @"Double Tap Detected";//雙擊
[self performSelector:@selector(eraseMe:)
withObject:doubleLabel afterDelay:1.6f];
}
- (void)tripleTap {
tripleLabel.text = @"Triple Tap Detected";//三擊
[self performSelector:@selector(eraseMe:)
withObject:tripleLabel afterDelay:1.6f];
}
- (void)quadrupleTap {
quadrupleLabel.text = @"Quadruple Tap Detected";//四擊
[self performSelector:@selector(eraseMe:)
withObject:quadrupleLabel afterDelay:1.6f];
}
- (void)eraseMe:(UITextField *)textField {
textField.text = @"";
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];//實例一個uitouch
NSUInteger tapCount = [touch tapCount]; //計算touch的tapCount次數
switch (tapCount) {
case 1:
[self singleTap];
break;
case 2:
[self doubleTap];
break;
case 3:
[self tripleTap];
break;
case 4:
[self quadrupleTap];
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 {//清空lable
label.text = @"";
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {//開始觸碰
if ([touches count] == 2) {//檢測是否為兩個手指觸點
NSArray *twoTouches = [touches allObjects];
UITouch *first = [twoTouches objectAtIndex:0];
UITouch *second = [twoTouches objectAtIndex:1];
initialDistance = distanceBetweenPoints(
[first locationInView:self.view],
[second locationInView:self.view]);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {//移動手指
if ([touches count] == 2) {
NSArray *twoTouches = [touches allObjects];
UITouch *first = [twoTouches objectAtIndex:0];
UITouch *second = [twoTouches objectAtIndex:1];
CGFloat currentDistance = distanceBetweenPoints(
[first locationInView:self.view],
[second locationInView:self.view]);
if (initialDistance == 0)
initialDistance = currentDistance; //根據移動前后的坐標距離差檢測是捏合的手勢還是打開的手勢
else if (currentDistance - initialDistance > kMinimumPinchDelta) {//檢測是否大于最小移動值kMinimumPinchDelta
label.text = @"Outward Pinch";
[self performSelector:@selector(eraseLabel)
withObject:nil
afterDelay:1.6f];
}
else if (initialDistance - currentDistance > kMinimumPinchDelta) {
label.text = @"Inward Pinch";
[self performSelector:@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 = [touch previousLocationInView:self.view];
CGPoint currentPoint = [touch locationInView:self.view];
CGFloat angle = angleBetweenLines(lastPreviousPoint, //計算兩條線之間的角度
lastCurrentPoint,
previousPoint,
currentPoint);
if (angle >= kMinimumCheckMarkAngle&& angle <= kMaximumCheckMarkAngle
&& lineLengthSoFar > kMinimumCheckMarkLength) {//檢測手勢被承認的條件 kMinimumCheckMarkAngle 最小角度kMaximumCheckMarkAngle最大角度kMinimumCheckMarkLength 畫線最小長度
label.text = @"Checkmark";
[self performSelector:@selector(eraseLabel)
withObject:nil afterDelay:1.6];
}
lineLengthSoFar += distanceBetweenPoints(previousPoint, currentPoint);//lineLengthSoFar , lastPreviousPoint, lastCurrentPoint 重新賦值
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 line1End, CGPoint line2Start, 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);
return radiansToDegrees(rads);
//degs = degrees(atan((top - bottom)/(right - left)))
}
CGFloat angleBetweenLines(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, 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))));
return radiansToDegrees(rads);
}
把他加到工程里 在需要的地方#import "CGPointUtils.h" 就可以直接用了
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。