关于touch的一些方法和代码

关于手指触摸的几个方法

//开始触摸

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

{

    NSLog(@"开始触摸");


}

//触摸过程中

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

{

    NSLog(@"触摸过程中");

}

//触摸结束

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

{

    NSLog(@"触摸结束");   

}

//触摸取消

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

{

    NSLog(@"触摸取消");

}



怎样让色块随着手指的拖动移动···




@implementation MoveTouch

CGPoint offsetPoint;

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

{

    UITouch *touch=[touches anyObject];

    offsetPoint=[touch locationInView:self];

}

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

{

//    NSLog(@"移动");

    UITouch *touch=[touches anyObject];

//    NSLog(@"%@",touch);//打印每次移动的位置:相对于整个window的位置和相对于自身所在的frame的位置

    //获取当前的点,相对于window的位置

    CGPoint currentPoint =[touch locationInView:self.window];

//    NSLog(@"%@",NSStringFromCGPoint(currentPoint))//打印当前位置

    self.frame=CGRectMake(currentPoint.x-offsetPoint.x, currentPoint.y-offset.y, self.frame.size.width, self.frame.size.height);

}

@end


响应者链

响应者链事件处理的顺序与触摸检测查询相反。
阻断,无法完成检测查询。
(BOOL) userInteractionEnabled
YES 停止与用户进行交互
NO  能够与用户进行交互

你可能感兴趣的:(UI学习)