UITouch的方法

  • 在触摸开始时,调用 这个方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan");
    
    //手指在屏幕上每一次触摸都会产生一个触摸事件
    //这些事件会保存这个NSSet 类型的 touches里
    //我们可以取出这个事件
    UITouch * touch = [touches anyObject];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.center = [touch locationInView:self.window];
    }];
    
    
    NSLog(@"window:%@",touch.window);
    NSLog(@"view:%@",touch.view);
    NSLog(@"phase:%id",touch.phase);
}

  • 手指离开屏幕时,触发这个方法
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesEnded");
    //可以在这里不使用任何一个事件,只去写我们自己的方法

    [UIView animateWithDuration:0.3 animations:^{
        imageView.center = CGPointMake(160, 300);
    }];
    
}
  • 手指在屏幕上移动时,触发这个方法
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //那么,手指只要在屏幕有偏移那么就调用一次这个方法
    NSLog(@"touchesMove");
    UITouch * touch = [touches anyObject];
    NSLog(@"view:%@",touch.view);
    if (touch.view == imageView && touch.phase == UITouchPhaseMoved) {
        imageView.center = [touch locationInView:self.window];
 }
  • 触摸被打断时调用的方法
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    //程序运行过程中,进来电话,短信,推送等优先级比较高的事件
}

你可能感兴趣的:(UITouch的方法)