在Xcode 5中,我创建了an iPhone app,其中包含5个“字母图块”,可以拖动它们:
使用Tile.xib
(此处为Tile)将图块实现为fullscreen类:
tile.png
是一张没有阴影的小图片:
dragged.png
是带阴影的较大图片:
后一张图片由Tile.m中的touchesBegan
显示:
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
_background.image = kDragged;
[_letter setFont:[UIFont systemFontOfSize:48]];
[_value setFont:[UIFont systemFontOfSize:20]];
[self.superview bringSubviewToFront:self];
[super touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
_background.image = kTile;
[_letter setFont:[UIFont systemFontOfSize:36]];
[_value setFont:[UIFont systemFontOfSize:16]];
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
{
_background.image = kTile;
[_letter setFont:[UIFont systemFontOfSize:36]];
[_value setFont:[UIFont systemFontOfSize:16]];
[super touchesCancelled:touches withEvent:event];
}
使用ViewController.m中的UIPanGestureRecognizer
完成拖动:
- (IBAction)dragTile:(UIPanGestureRecognizer *)recognizer
{
Tile* tile = (Tile*)recognizer.view;
UIView* parent = tile.superview;
if (recognizer.state == UIGestureRecognizerStateBegan ||
recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:parent];
[tile setCenter:CGPointMake(tile.center.x + translation.x,
tile.center.y + translation.y)];
[recognizer setTranslation:CGPointZero inView:parent];
}
}
我的问题是:
当我触摸一块瓷砖时,它的尺寸会增加并显示阴影(这是可以的)。
但是一旦我开始拖动瓷砖,它的尺寸会重新变回小而没有阴影(我不明白)。
我在touchesEnded
和touchesCancelled
设置了断点 - 当拖动开始时,后者正在被击中。但是为什么以及如何制止这个?
答案 0 :(得分:1)
正如评论中所讨论的,该问题的解决方案是将cancelsTouchesInView
的{{1}}属性设置为UIGestureRecognizer
,以便NO
方法不会被touchesCancelled:withEvent:
方法调用识别器。
请参阅文档here。