iOS学习笔记——触摸事件

iOS设备都是可以多点触摸的,是指手指放在iOS设备的屏幕上从屏幕上拖动或抬起。系统当前视图响应触摸事件,若无响应则向上层传递,构成响应者链。触摸事件的函数有4个。

创建一个视图,继承UIView类,在视图控制器中把视图加载到视图控制器上:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //创建一个视图对象,响应触摸动作
    LinView * pView = [[LinView alloc]initWithFrame:CGRectMake(0, 0, 320, 450)];
    //设置视图的背景颜色,与根视图区别
    pView.backgroundColor = [UIColor blueColor];
    //把视图添加到当前视图,或视图控制器
    [self.view addSubview:pView];
    //释放创建的对象
    [pView release];
}
在.h文件中添加成员变量:

@interface LinView : UIView
{
    //创建一个成员,存放两点之间距离变化的信息
    float _lastDistance;
}

@end
在.h文件中对各类事件进行相关的实现、处理:

@implementation LinView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //⭐️开启多点触摸
        self.multipleTouchEnabled = YES;
        self.userInteractionEnabled = YES;
    }
    return self;
}
#pragma mark--------触摸开始时调用此方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //获取任意一个touch对象
    UITouch * pTouch = [touches anyObject];
    //获取对象所在的坐标
    CGPoint point = [pTouch locationInView:self];
    //以字符的形式输出触摸点
    NSLog(@"触摸点的坐标:%@",NSStringFromCGPoint(point));
    //获取触摸的次数
    NSUInteger tapCount = [pTouch tapCount];
    //对触摸次数判断
    if (tapCount == 1)
    {
        //在0.2秒内只触摸一次视为单击
        [self performSelector:@selector(singleTouch:) withObject:nil afterDelay:0.2];
    }
    else if(tapCount == 2)
    {
        //取消单击响应,若无此方法则双击看做是:单击事件和双击事件
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTouch:) object:nil];
        //确定为双击事件
        [self doubleTouch:nil];
    }
}
//单击方法
- (void)singleTouch:(id)sender
{
    //对应单击时发生的事件
    NSLog(@"此时是单击的操作");
}
//双击方法
- (void)doubleTouch:(id)sender
{
    //双击时对应发生的事件
    NSLog(@"此时是双击的操作");
}
#pragma mark----------触摸的移动(滑动)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //获取所有的触摸对象
    NSArray * array = [touches allObjects];
    //分别取出两个touch对象
    UITouch * pTouch1 = [array objectAtIndex:0];
    UITouch * pTouch2 = [array objectAtIndex:1];
    //获取两个touch对象的坐标点
    CGPoint point1 = [pTouch1 locationInView:self];
    CGPoint point2 = [pTouch2 locationInView:self];
    //用封装的方法计算两触摸点之间的距离
    double distance = [self distanceOfPoint:point1 withPoint:point2];
    //判断两点间距离的变化
    if ((distance - _lastDistance) > 0)
    {
        //两点距离增大的方法,一般为图片、文字等的放大,捏合
        NSLog(@"两点距离变大");
    }
    else
    {
        //两点距离减小的方法,一般为图片、文字等的缩小,放开
        NSLog(@"两点距离变小");
    }
    //把现在的距离赋值给原来的距离,覆盖之
    _lastDistance = distance;
}
//编写一个计算两点之间距离的方法,封装此方法,方便调用
- (double)distanceOfPoint:(CGPoint)point1 withPoint:(CGPoint)point2
{
    double num1 = pow(point1.x - point2.x, 2);
    double num2 = pow(point1.y - point2.y, 2);
    double distance = sqrt(num1 + num2);
    return distance;
}
#pragma mark--------手离开屏幕,触摸事件结束
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //触摸结束时发生的事件
}
#pragma mark--------触摸事件被打断,比如电话打进来
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    //一般不写此方法,可以把程序挂起,放在后台处理
}

@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值