UITouch 触摸事件

来源:互联网 发布:京东算法大赛代码 编辑:程序博客网 时间:2024/06/04 19:25
//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
原创粉丝点击