首先,将滑动手势识别器从库拖放到视图中。
然后您在 View 中检查取消的项目。
编写代码以响应滑动手势。
- (IBAction)swipe:(id)sender {
v.backgroundColor = [UIColor blueColor];
}
然后,编写触摸委托方法。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];
layer.frame = CGRectMake(pt.x, pt.y, 100, 100);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];
layer.frame = CGRectMake(pt.x, pt.y, 100, 100);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
layer.frame = CGRectMake(0, 0, 100, 100);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.backgroundColor = [UIColor redColor];
}
现在可以不取消移动图像,并且可以滑动屏幕设置颜色为蓝色(滑动手势识别成功)。你都可以。并且,当触摸结束时,窗口颜色变为红色。
您可以下载此示例项目并运行它:
https://github.com/weed/p120812_TouchAndGesture