我创建为iPad一个谜引擎(iOS版SDK 4.2)错误水平
我有控制一个UIView(rootView)保持较小的UIView(puzzleView)和十二拼图一个PuzzleViewController (PuzzlePieceView类扩展UIImageView)。
理念很简单。 puzzlePieces围绕着puzzleView并被挑选并拖动到puzzleView。当挑选这些作品时,他们会将它们放大一倍,并将其放在中心位置以放置手指触摸的位置。 当它们被放置在正确的位置时,它们将保持放置(它们从rootView中移除并作为子视图添加到puzzleView中),如果它们落在错误的位置,它们会返回到原始位置和大小。
目前我最重要的触摸开始/移动/结束/取消在PuzzlePieceView类,使每个智力玩具控制自己的动作。
这里就是他们做的
#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 -
似乎很容易,它是。
我只剩下一个bug了。 当我试图制作一个横向移动任何puzzlePiece触摸得到取消。 这只发生在移动以快速方式开始时。如果我接触这件作品并等待片刻(大约一秒钟),那么这些作品就会完美地移动。 这些碎片也在垂直方向上完美移动。
我试着不改变puzzlePiece大小视图时第一次接触卜错误依然存在,
我最近试图处理在rootview中的触摸事件,并有相同的错误...... :( – romapires 2011-03-09 10:43:49