您的位置:首页 > 产品设计 > UI/UE

iOS触摸手势——UITouch

2016-05-16 14:37 453 查看
</pre><pre name="code" class="objc">// 触摸开始
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesBegan - touch count = %d", [touches count]);

// 获得触摸点的集合,可以判断多点触摸事件
for (UITouch *touch in event.allTouches)
{
// 获取当前触摸坐标
CGPoint currentPoint = [touch locationInView:self.view];
NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);

// 获取当前触摸前一个坐标
CGPoint previousPoint = [touch previousLocationInView:self.view];
NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);

// touch.phase,触摸事件的阶段
NSLog(@"touch.phase = %d", touch.phase);
// touch.tapCount,触摸事件的轻碰次数,可以判断双击事件
NSLog(@"touch.tapCount = %d", touch.tapCount);
}

UITouch *touch = [touches anyObject];

// 单击或双击时,改变背景颜色
if (1 == touch.tapCount)
{
self.view.backgroundColor = [UIColor blueColor];
}
// 双击时,设置背景颜色为红色
if (2 == touch.tapCount)
{
self.view.backgroundColor = [UIColor redColor];
}

// 获取触摸区域的子视图
// 获取当前视图控制器的坐标
CGPoint currentPoint = [touch locationInView:self.view];
for (UIView *oneView in self.view.subviews)
{
if ([oneView isKindOfClass:[UIImageView class]])
{
// 是否选中图标(当前坐标是否包含所选图标)
if (CGRectContainsPoint(oneView.frame, currentPoint))
{
// 获取当前被选择图标
UIView *selectedView = (UIView *)oneView;
// 被选中视图放在最上面
[self.view bringSubviewToFront: selectedView];
}
}
}

}


// 触摸移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesMoved - touch count = %d", [touches count]);

// 获得触摸点的集合,可以判断多点触摸事件
for(UITouch *touch in event.allTouches)
{
// 获取当前触摸坐标
CGPoint currentPoint = [touch locationInView:self.view];
NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);

// 获取当前触摸前一个坐标
CGPoint previousPoint = [touch previousLocationInView:self.view];
NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);

// touch.phase,触摸事件的阶段
NSLog(@"touch.phase = %d", touch.phase);
// touch.tapCount,触摸事件的轻碰次数,可以判断双击事件
NSLog(@"touch.tapCount = %d", touch.tapCount);
}

UITouch *touch = [touches anyObject];
// 获取当前视图控制器的坐标
CGPoint currentPoint = [touch locationInView:self.view];
// 获取上一次位置
CGPoint previousPoint = [touch previousLocationInView:self.view];
// 坐标偏移量
float moveX = currentPoint.x - previousPoint.x;
float moveY = currentPoint.y - previousPoint.y;
}


// 触摸结束
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesEnded - touch count = %d", [touches count]);

// 获得触摸点的集合,可以判断多点触摸事件
for (UITouch *touch in event.allTouches)
{
// 获取当前触摸坐标
CGPoint currentPoint = [touch locationInView:self.view];
NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);

// 获取当前触摸前一个坐标
CGPoint previousPoint = [touch previousLocationInView:self.view];
NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);

// touch.phase,触摸事件的阶段
NSLog(@"touch.phase = %d", touch.phase);
// touch.tapCount,触摸事件的轻碰次数,可以判断双击事件
NSLog(@"touch.tapCount = %d", touch.tapCount);
}

UITouch *touch = [touches anyObject];
if (2 == touch.tapCount)
{
// 双击
}
}


// 触摸意外结束(如来电话)
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesCancelled - touch count = %d", [touches count]);

// 获得触摸点的集合,可以判断多点触摸事件
for (UITouch *touch in event.allTouches)
{
// 获取当前触摸坐标
CGPoint currentPoint = [touch locationInView:self.view];
NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);

// 获取当前触摸前一个坐标
CGPoint previousPoint = [touch previousLocationInView:self.view];
NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);

// touch.phase,触摸事件的阶段
NSLog(@"touch.phase = %d", touch.phase);
// touch.tapCount,触摸事件的轻碰次数,可以判断双击事件
NSLog(@"touch.tapCount = %d", touch.tapCount);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: