UITouch 触摸事件

//1.创建视图对象

{

    //保存前一次点击的坐标

    CGPoint _prePoint;

}

- (void)createImageView

{

    UIImageView *imageView = [[UIImageView alloc] init];

    imageView.frame = CGRectMake(30, 50, 300, 400);

    imageView.image = [UIImage imageNamed:@"17_5.jpg"];

    imageView.tag = 100;

    [self.view addSubview:imageView];

}

//2.创建触摸事件

//2.1单击事件

//UIEvent代表一个事件 , UITouch代表了一个触摸,一个事件中,可能由多个触摸对象

//UIEvent是系统捕获并发送给应用程序,经过hittest测试确定该事件发生在那个控件上。

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

{

    //保存第一点的位置

    UITouch *touch = [touches anyObject];

    //1. 保存起始点坐标

    _prePoint = [touch locationInView:self.view];

}

//2.2触摸移动事件

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

{

    UITouch *touch = [touches anyObject];

    //2. 取到触摸当前点得坐标

    CGPoint curPoint = [touch locationInView:self.view];

    //3. 计算两个点之间的偏移量

    CGPoint trans = CGPointMake(curPoint.x - _prePoint.x, curPoint.y - _prePoint.y);

    //4. 根据计算的偏移量移动 imageview;

    UIImageView *imageView = (id)[self.view viewWithTag:100];

    imageView.center = CGPointMake(imageView.center.x + trans.x, imageView.center.y + trans.y);

    //5. 设置之前点得坐标为当前触摸的坐标

    _prePoint = curPoint;

}


//3.触摸响应事件调用方法

//单击放大图片为全屏

- (void)tapOnce

{

    UIImageView *imageView = (id)[self.view viewWithTag:100];

    imageView.frame = self.view.bounds;

}



- (void)tapTwice

{

    UIImageView *imageView = (id)[self.view viewWithTag:100];

    imageView.frame = CGRectMake(3050300400);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值