2

UILabelのテキストをジェスチャの速度に設定しているときに、UIPanGestureRecognizerを使用してビューを単純に変換する際に問題が発生します。

私は(IBの助けを借りて)次のようなパンジェスチャでビューを変換することに成功しました:

- (IBAction)handleGesture:(UIPanGestureRecognizer *)sender

{   
    CGPoint translation = [sender translationInView:self.view];

    sender.view.center = CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y);

    [sender setTranslation:CGPointMake(0, 0) inView:self.view];
} 

しかし、このメソッドに次の行を追加すると、ビューの変換は停止しますが、UILabelは速度で更新されます。

self.myLabel.text = [NSString stringWithFormat:@"%@", [NSNumber numberWithInt:[sender velocityInView:self.roundShape].y]];

どうすればこれを修正できますか?

4

3 に答える 3

5

基本的に、Pan Gesture Recognizer から取得する変換値は相対値です。ジェスチャがタッチの認識を開始した開始点を基準にします。したがって、ジェスチャが開始された元のポイントを保持し、ビューの現在の位置ではなく、そのポイントに翻訳を適用する必要があります。

- (void)gestureDidTranslate:(UIPanGestureRecognizer *)panGesture
{
    if([panGesture state] == UIGestureRecognizerStateBegan){
        self.originalPoint = self.movingView.center;
    }

    CGPoint translation = [panGesture translationInView:self.view];
    self.movingView.center = CGPointMake(self.originalPoint.x+translation.x, self.originalPoint.y+translation.y);
    self.label.text = NSStringFromCGPoint([panGesture velocityInView:self.view]);
}

認識している間に翻訳値をレコグナイザーにゼロアウトしたい時は考えられません。

于 2012-12-05T00:00:34.610 に答える
0

オートレイアウト付き!

これは、Interface Builder を使用してすべて可能です。これを行うために使用したコードは次のとおりです。

@property (weak,nonatomic) IBOutlet NSLayoutConstraint *buttonXConstraint;
@property (weak,nonatomic) IBOutlet NSLayoutConstraint *buttonYConstraint;

次に、これらの IBOutlet を Interface Builder の Horizo​​ntal Constraint(X Position) および Vertical Constraint(Y Position) に接続します。Constraints が最も近いビューではなく、ベース ビューに接続されていることを確認してください。

パン ジェスチャを接続し、次のコードを使用してオブジェクトをドラッグします。

- (IBAction)panPlayButton:(UIPanGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateBegan){

    } else if(sender.state == UIGestureRecognizerStateChanged){
        CGPoint translation = [sender translationInView:self.view];

        //Update the constraint's constant
        self.buttonXConstraint.constant += translation.x;
        self.buttonYConstraint.constant += translation.y;

        // Assign the frame's position only for checking it's fully on the screen
        CGRect recognizerFrame = sender.view.frame;
        recognizerFrame.origin.x = self.buttonXConstraint.constant;
        recognizerFrame.origin.y = self.buttonYConstraint.constant;

        // Check if UIImageView is completely inside its superView
        if(!CGRectContainsRect(self.view.bounds, recognizerFrame)) {
            if (self.buttonYConstraint.constant < CGRectGetMinY(self.view.bounds)) {
                self.buttonYConstraint.constant = 0;
            } else if (self.buttonYConstraint.constant + CGRectGetHeight(recognizerFrame) > CGRectGetHeight(self.view.bounds)) {
                self.buttonYConstraint.constant = CGRectGetHeight(self.view.bounds) - CGRectGetHeight(recognizerFrame);
            }

            if (self.buttonXConstraint.constant < CGRectGetMinX(self.view.bounds)) {
                self.buttonXConstraint.constant = 0;
            } else if (self.buttonXConstraint.constant + CGRectGetWidth(recognizerFrame) > CGRectGetWidth(self.view.bounds)) {
                self.buttonXConstraint.constant = CGRectGetWidth(self.view.bounds) - CGRectGetWidth(recognizerFrame);
            }
        }

        //Layout the View
        [self.view layoutIfNeeded];
    } else if(sender.state == UIGestureRecognizerStateEnded){

    }

    [sender setTranslation:CGPointMake(0, 0) inView:self.view];
}

フレームをチェックして、ビューの外に出ていないことを確認するコードをそこに追加しました。オブジェクトを部分的に画面からはみ出したい場合は、遠慮なく取り出してください。

ビューをリセットしていたのは AutoLayout の使用であることに気付くのに少し時間がかかりましたが、制約はここで必要なことを行います!

于 2014-03-19T03:56:56.040 に答える