我創建爲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