触摸的基本概念及应用

//1.UIView支持触摸事件,因为继承于UIResponse,而且支持多点触摸

//2.userInteractionEnabled = NO这个方法可以阻断响应链的响应。这个设置以后就不能拖动了。

下面是一个button随着鼠标拖动而移动的代码:

/触摸开始
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    
    UITouch *tou =[touches anyObject];
    
    _startpoint = [tou locationInView:self];

}
//开始移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    //这个是获取触摸点得方法
    UITouch *touch = [touches anyObject];
    
    CGPoint currrntPoint = [touch locationInView:self];
    
    CGFloat dx = currrntPoint.x - _startpoint.x;
    
    CGFloat dy = currrntPoint.y - _startpoint.y;
    
   // self.center = CGPointMake(self.center.x + dx, self.center.y + dy);
    //
    
    CGPoint f = self.center;
    
    f.x += dx;
    
    f.y += dy;
    
    self.center = f;
}
//移动取消
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"取消");
}
//移动结束
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"结束");
    
}


你可能感兴趣的:(触摸的基本概念及应用)