2

メソッド touchesbegan、touchesmoved、および touchesended を使用して、ViewController からオブジェクトを移動したいと考えています。それは私が望むように機能します(移動距離が十分に大きくない場合、最初の位置に配置されます)が、ドラッグのアニメーションはスムーズではありません。誰かが私を助けて、流暢なドラッグの動きにすることができますか?

これは私のコードです:

#pragma mark -
#pragma mark Touch Gestures


- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [touches anyObject];
    if (touch.view == _topCard) {
        CGPoint touchPoint = [touch locationInView:_topCard];
        touchCenterOffset = CGPointMake([touch view].center.x - touchPoint.x,
                                        [touch view].center.y - touchPoint.y);
        [self animateFirstTouchAtPoint:touchPoint];
    }

}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches anyObject];

    if (touch.view == _topCard) {
        CGPoint location = [touch locationInView:_topCard];
        CGPoint pointOnTable = [_topCard convertPoint:[touch locationInView:_topCard] toView:_topCard.superview];
                CGPoint newCenter = CGPointMake(location.x + touchCenterOffset.x,
                                        location.y + touchCenterOffset.y);
        [touch view].center = newCenter;

        if (pointOnTable.x > 180) {
            NSLog(@"Aktionismus");
        } else {
            NSLog(@"KEIN Aktionismus");
        }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches anyObject];

    if (touch.view == _topCard) {
        CGPoint pointOnTable = [_topCard convertPoint:[touch locationInView:_topCard] toView:_topCard.superview];

        if (pointOnTable.x > 180) {
            NSLog(@"Aktionismus");
        } else {
            [self animatePutBackTopCard:FLIP_DURATION];
        }
    }
}

- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch* touch = [touches anyObject];

    if (touch.view == _topCard) {
        [self animatePutBackTopCard:FLIP_DURATION];
    }

}

#pragma mark - 
#pragma mark Animations

- (void)animateFirstTouchAtPoint:(CGPoint)touchPoint {
    /*[UIView animateWithDuration:GROW_ANIMATION_DURATION_SECONDS animations:^{
        _topCard.transform = CGAffineTransformScale(_topCard.transform, 1.2, 1.2);
    }];*/
}

- (void)animatePutBackTopCard:(NSTimeInterval)duration {
    [UIView animateWithDuration:duration animations:^{
        CGPoint originalCenter = CGPointMake(ORIGIN_CENTER_X,ORIGIN_CENTER_Y);
        _topCard.center = originalCenter;
    }];
}

どうもありがとう!

4

5 に答える 5

1

オブジェクトの移動をどのように行っているかを見てください。スムーズに動作しています。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    CGPoint location = [touch locationInView:self.superview];
    float difx = [[touches anyObject] locationInView:self].x - [[touches anyObject] previousLocationInView:self].x;

    float dify = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;

    CGAffineTransform newTransform1 = CGAffineTransformTranslate(self.transform, difx, dify);

    self.transform = newTransform1;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self.superview];
    float difx = [[touches anyObject] locationInView:self].x - [[touches anyObject] previousLocationInView:self].x;

    float dify = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;

    CGAffineTransform newTransform1 = CGAffineTransformTranslate(self.transform, difx, dify);

    self.transform = newTransform1;

}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}

あなたのニーズに適応できると思います。

于 2013-07-17T07:36:04.640 に答える
0

ドラッグする場合は、代わりに panGestureRecognizer を使用することをお勧めします。

情報はこちら

于 2013-07-17T07:08:26.053 に答える