0

100枚の画像があります(a400e3x_0.png、.....、a400e3x_98.png、a400e3x_99.png)。これらの画像は、ユーザーが制御できるアニメーションのように、UIImageView のスワイプに応じて変化します。問題は、ユーザーがパンを停止してから再開すると、アニメーションが画像 0 に戻ることです。ユーザーがパンを停止したときの位置を保存する必要があります。しかし、それを追加すると、追加は常にそこにあります。例: 画像 16 でパンを停止し、16 から再開します。(curX%100) がゼロの場合でも、常に 16 が追加されます。このためのアルゴリズムを教えてください。

- (IBAction)PanGesAction:(id)sender
{
 CGPoint translation = [_PanGes translationInView:self.view];
 int curX = (int)translation.x;
 addition = (curX%100)+Savedif;

 NSLog(@"%d", addition);

 [_ImageV2 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"a400e3x_%d.png", addition]]];


    if (_PanGes.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"End");
        Savedif = addition;
        //When user end

    }

    if (_PanGes.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"Began");
         //When user begin

    }
}
4

2 に答える 2

1

新しいジェスチャが開始されると、送信される値を編集する機会があります。これにより、開始値をジェスチャに転送し、物事をシンプルに保つことができます。

- (IBAction)PanGesAction:(id)sender
{
    if (_PanGes.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"Began");
        //When user begin
        [_PanGes setTranslation:(CGPoint){Savedif, 0} inView:self.view];
        Savedif = 0;
    }

    CGPoint translation = [_PanGes translationInView:self.view];
    int curX = (int)translation.x;
    addition = (curX%100);

    NSLog(@"%d", addition);

    [_ImageV2 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"a400e3x_%d.png", addition]]];


    if (_PanGes.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"End");
        Savedif = addition;
        //When user end
    }
}
于 2013-09-22T13:24:23.537 に答える
-1

何も保存しなくてもいい方法がありますが、

transformの物件紹介UIView

このコードを試してください:

- (void)handlePanGesture:(UIPanGestureRecognizer *)gesture
{
    UIView *imageView = gesture.view;
    if (gesture.state == UIGestureRecognizerStateChanged) {
        CGPoint transition = [gesture translationInView:gesture.view];
        imageView.transform = CGAffineTransformMakeTranslation(imageView.transform.tx + transition.x, imageView.transform.tx + transition.y);
        [gesture setTranslation:CGPointZero inView:gesture.view];
    }
    else if (gesture.state == UIGestureRecognizerStateEnded) {
        imageView.center = CGPointMake(imageView.center.x + imageView.transform.tx, imageView.center.y + imageView.transform.ty);
        imageView.transform = CGAffineTransformIdentity;
    }
}

imageView の位置は常に次のとおりです。

imageView.center.x+image.transform.tx
imageView.center.y+image.transform.ty

それに応じて行動することができます。

于 2013-09-22T13:27:48.010 に答える