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);

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章