0

スワイプの方向に応じて、画像を左右またはvvでスワイプするジェスチャレコグナイザーがいくつかあります。しかし、私はアニメーションをよりスムーズにしたいと思っています。これはこれまでの私のコードです:

-(void)swipeNext:(id)sender{
    //0 Get the currentview as referenced by pageControl
    UIImageView *oldView = [views objectAtIndex:currentViewIndex];
    //MOVE ON TO NEW VIEW
    currentViewIndex ++;
    NSLog(@"left swiped  - currentViewIndex:%d", currentViewIndex);
    UIImageView *newView = [views objectAtIndex:currentViewIndex];
    //NSLog(@"views objectAtIndex = %@",[views objectAtIndex:[self.pageControl currentPage]]);

    //1 Animate out the old view
    newView.frame = oldView.frame;
    newView.center = CGPointMake(oldView.center.x + CGRectGetWidth(oldView.frame), oldView.center.y);
    [oldView.superview addSubview: newView];

    [UIView animateWithDuration:1.0
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseIn //UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                     animations:^{
                         newView.center = oldView.center;
                         oldView.center = CGPointMake(oldView.center.x - CGRectGetWidth(oldView.frame), oldView.center.y);

                     }
                     completion:nil];
}

もう少し速くしたいのですが。エミュレートしようとしているアプリ、DunkinDonuts(Dunkin Donutsアプリが使用するビューコントローラーの種類)への参照として投稿しました。最初はUIPageControlを使用しているようですが、スワイプの方向を判断するのに問題がありました(UIPageControlを使用してUIImageViewを左から右にアニメーション化するにはどうすればよいですか?)、ジェスチャに切り替えました。ただし、アプリが表示されている場合は、スワイプの方が高速です。

4

2 に答える 2

1

適切にフォーマットされた(読み取り可能な)コードブロックを追加するために、コメントを使用できないことがわかりました。元の質問の更新として回答を使用することが提案されました。だからここにあります。

アップデート

このようにコードを変更しました。いつものように、viewDidLoadはこれを行うcreateUIViewsを呼び出します。

-(void)createUIViews{
    UIImage *image1 = [UIImage imageNamed:@"GiftCard.png"];
    firstView = [[UIImageView alloc] initWithImage:image1];

    CGRect newFrame1 = firstView.frame;
    newFrame1.origin.x = 50;
    newFrame1.origin.y = 10;
    firstView.frame = newFrame1;

    UIImage *image2 = [UIImage imageNamed:@"MyCard.png"];
    secondView = [[UIImageView alloc] initWithImage:image2];

    CGRect newFrame2 = secondView.frame;
    newFrame2.origin.x = 350;//CGRectGetWidth(firstView.superview.frame)/2;
    newFrame2.origin.y = 10;
    secondView.frame = newFrame2;

    //Add all Views offscreen except #1
    [self.scrollView addSubview:firstView]; //added ONSCREEN
    [self.scrollView addSubview:secondView]; //added OFFSCREEN
}

これにより、firstViewが画面上に設定され、secondViewが右側にオフになります。

ジェスチャレコグナイザーもviewDidLoadで設定されます。以下のように、swipeNextを呼び出します。

-(void)swipeNext:(id)sender{
//0 Get the currentview as referenced by pageControl
oldView = [views objectAtIndex:currentViewIndex];
//MOVE ON TO NEW VIEW
currentViewIndex ++;
newView = [views objectAtIndex:currentViewIndex];

//1 Animate out the old view **(Aaron, youre right, dont need this anymore)**
//newView.frame = oldView.frame;
//newView.center = CGPointMake(oldView.center.x + CGRectGetWidth(oldView.frame), oldView.center.y);
//[oldView.superview addSubview: newView];

[UIView animateWithDuration:1.0
                      delay: 0.0
                    options: UIViewAnimationOptionCurveEaseIn 
                 animations:^{
                     newView.center = oldView.center;
                     oldView.center = CGPointMake(oldView.center.x - CGRectGetWidth(oldView.frame), oldView.center.y);

                 }
                 completion:nil];

addSubviewをcreateUIViewsメソッドに移動したため、コメントアウトしました。それは同じように機能します、私はそれがただ迅速さの問題か、Imが欠けているいくつかのアニメーション機能の問題だと思います。ビデオを追加できたらいいのにと思います。オリジナルのDDアプリは、UIPageControlのようにアニメーション化が速いように見えます(私は思う)ここにビデオへのリンクがあります。遅いiOSUIViewアニメーション

于 2013-03-15T16:00:05.190 に答える
0

同じビューのアニメーションを開始するイベントループの同じターン中にビュー階層にビューを追加することは、パフォーマンスの低下やその他の奇妙なアニメーションの不具合のレシピです。コードでは、swipeNextが呼び出される前にnewViewがどのようになるかがわかります。です[views objectAtIndex:currentViewIndex + 1]。現在のビューが所定の位置にアニメーション化されたら、newView(およびそのことについては以前のビュー)をビュー階層に追加することをお勧めします。たとえば、「nextView」と「previousView」をビュー階層に配置するコードは、アニメーションの完了ブロックに入れることができます。nextViewとpreviousViewを事前にビュー階層に配置しますが、画面外に配置します。次に、swipeToNextで、中心をアニメーション化するだけで、アニメーションが完了するまでビュー階層の変更を回避できます。

于 2013-03-15T05:43:47.610 に答える