开发者

Bug when moving a UIView horizontally

I've created a puzzle engine for iPad (iOS SDK 4.2)

I have a PuzzleViewController that controls a UIView (rootView) that holds a smaller UIView (puzzleView) and twelve puzzle pieces (PuzzlePieceView class extends UIImageView).

The ideia is very simple. The puzzlePieces are around the puzzleView and are picked and dragged to the puzzleView. When the pieces are picked they double they're size and are centered to place where the finger is touching. When they're dropped in the right place they stay put (they're removed from rootView and added as subviews to the puzzleView) if they're drop in the wrong place they return to the original position and size.

Currently I'm overriding touches Began/Moved/Ended/Cancelled in the PuzzlePieceView class so that each PuzzlePiece controls its own movements.

here's what they do

#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)touch开发者_高级运维esCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"PuzzlePiece touchesCancelled");
    if (!isDeployed) {
        [self restoreToInitialSize];
        [self restoreToInitialPosition];
    } 
}

#pragma mark -

Seemed pretty easy and it was.

I've got only one bug left. When I try to make an horizontal move on any puzzlePiece the touch gets cancelled. This only happens when the move is started in a fast way. If i touch the piece and wait for a moment (about a second) the pieces move perfectly. The pieces also move perfectly in the vertical direction.

I've tried not changing the puzzlePiece size when the view is first touched bu the bug remains...


I found the solution to this. One of my team-mates registered a UISwipeGestureRecognizer in the RootView instead of the specific subview were the swipeGesture should be recognized.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜