ViewController-UITouch
//触摸开始
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];
//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];
NSLog(@"point == %@",NSStringFromCGPoint(point));
}
//触摸移动
- (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];
//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];
NSLog(@"point == %@",NSStringFromCGPoint(point));
}
//触摸结束
- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];
//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];
NSLog(@"point == %@",NSStringFromCGPoint(point));
}
//触摸取消
- (void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event
{
//获取坐标信息
//获取到触摸点
UITouch *touch = [touches anyObject];
//转化为当前视图的具体坐标
CGPoint point = [touch locationInView:self.view];
NSLog(@"point == %@",NSStringFromCGPoint(point));
}
左右滑动切换图片练习
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_imgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
_imgView.image = [UIImage imageNamed:@”1.jpg”];
[self.view addSubview:_imgView];_count = 1;
}(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
UITouch *touch = [touches anyObject];
_beiginPoint = [touch locationInView:self.view];
}(void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
{
UITouch *touch = [touches anyObject];
_endPoint = [touch locationInView:self.view];[self compare];
}(void)compare
{
//右滑
if (_beiginPoint.x - _endPoint.x > 0) {
_count ++;
if (_count == 5) {
_count = 1;
}
_imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@”%ld.jpg”,_count]];
}//左滑
else if(_beiginPoint.x - _endPoint.x < 0)
{
_count –;
if (_count == 0) {
_count = 4;
}
_imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@”%ld.jpg”,_count]];
}
}