首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
触屏事件
时间:
2016-01-02 22:24:10
阅读:
231
评论:
0
收藏:
0
[点我收藏+]
标签:
iphone ipad开发: 关于触屏事件的一些操作
[cpp]
view plain
copy
print
?
//轻击:
//需要在你的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];
//实例一个uitouch
NSUInteger 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
{
//清空lable
label.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)
{
//检测是否大于最小移动值kMinimumPinchDelta
label.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 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);
returnradiansToDegrees(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))));
returnradiansToDegrees(rads);
}
触屏事件
标签:
原文地址:http://www.cnblogs.com/W-Kr/p/5095326.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!