我为iPad创建了一个拼图引擎(iOS SDK 4.2)
我有一个控制UIView(rootView)的PuzzleViewController,它包含一个较小的UIView(puzzleView)和十二个拼图(PuzzlePieceView类扩展了UIImageView)。
意识形态非常简单。 puzzlePieces围绕puzzleView并被拾取并拖动到puzzleView。当拾取这些碎片时,它们的尺寸加倍并且居中于手指触摸的位置。 当它们被放置在正确的位置时,它们会被放置(它们被从rootView中移除并作为子视图添加到puzzleView中),如果它们放在错误的位置,它们将返回到原始位置和大小。
目前我在PuzzlePieceView类中覆盖了触发/移动/结束/取消的触摸,以便每个PuzzlePiece控制它自己的动作。
这是他们做的事情
#pragma mark UIResponder overriding
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"PuzzlePiece touchesBegan");
if (!isDeployed) {
if (self.initialSize.width == 0 || self.initialSize.height ==0) { //if there is still no initial size
self.initialSize = self.frame.size;
}
NSLog(@"self.initialLocation.x %f %f", self.initialLocation.x, self.initialLocation.y);
if (self.initialLocation.x == 0. || self.initialLocation.y == 0.) { //if there is still no initial location
self.initialLocation = self.center; //record the initial location
}
UITouch *touch = [[event allTouches] anyObject];
self.center = [touch locationInView:self.superview]; //set the center of the view as the point where the finger touched it
[self.superview bringSubviewToFront:self]; //the piece brings itself as frontMostView so that it is always visible and never behind any other piece while moving
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"PuzzlePiece touchesMoved");
if (self.frame.size.width == self.initialSize.width) {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.image.size.width, self.image.size.height);
}
if (!isDeployed) {
UITouch *touch = [[event allTouches] anyObject];
self.center = [touch locationInView:self.superview]; //set the center of the view as the point where the finger moved to
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"PuzzlePiece touchesEnded");
if (!isDeployed) {
UITouch *touch = [[event allTouches] anyObject];
NSLog(@"point %@ x%f y%f", self.puzzleView, [touch locationInView:self.puzzleView].x, [touch locationInView:self.puzzleView].y);
if (CGRectContainsPoint(self.dropZone,[touch locationInView:self.puzzleView])) {
[self removeFromSuperview];
[self.puzzleViewController addPiece:self];
self.center = self.finalCenter;
self.isDeployed = YES;
}
else {
[self restoreToInitialSize];
[self restoreToInitialPosition];
}
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"PuzzlePiece touchesCancelled");
if (!isDeployed) {
[self restoreToInitialSize];
[self restoreToInitialPosition];
}
}
#pragma mark -
看起来非常简单,而且是。
我只剩下一个虫子了。 当我尝试在任何puzzlePiece上进行水平移动时,触摸会取消。 只有在快速启动移动时才会发生这种情况。如果我触摸这件作品并等待片刻(大约一秒钟),这些作品就会完美地移动。 这些碎片也在垂直方向上完美移动。
我已经尝试过在第一次触摸视图时不改变puzzlePiece大小,但bug仍然存在......
答案 0 :(得分:0)
我找到了解决方法。
我的一个队友在RootView中注册了一个UISwipeGestureRecognizer
而不是特定的子视图,应该识别swipeGesture。