0

UIPageControl を使用して、UIImageView を左から右にアニメーション化したいと考えています。私はこのコードを持っていますが、少し奇妙に思えます...具体的には、画像3に到達したらどうすればよいのでしょうか? 私は戻る必要があり、それは私が設定した方法で pageControl 配列内のビューの数学的計算を混乱させるでしょう:

    -(void)createUIViews{
        UIImage *image1 = [UIImage imageNamed:@"GiftCard.png"];
        firstView = [[UIImageView alloc] initWithImage:image1];
        firstView.backgroundColor = [UIColor grayColor];
        firstView.tag = 1;
        firstView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
        [self.view addSubview:firstView];
        CGRect newFrame = firstView.frame;
        newFrame.origin.x += 40;    // shift right by 50pts
        newFrame.origin.y += 40;

        [UIView animateWithDuration:1.0
                         animations:^{
                             firstView.frame = newFrame;
                         }];

        UIImage *image2 = [UIImage imageNamed:@"MyCard.png"];
        secondView = [[UIImageView alloc] initWithImage:image2];
        secondView.backgroundColor = [UIColor grayColor];
        secondView.tag = 1;
        secondView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);

        UIImage *image3 = [UIImage imageNamed:@"GiftCard.png"];
        thirdView = [[UIImageView alloc] initWithImage:image3];
        thirdView.backgroundColor = [UIColor grayColor];
        thirdView.tag = 1;
        thirdView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);


    }

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createUIViews];
    // Set up the views array with 3 UIImageViews
    views = [[NSArray alloc] initWithObjects:firstView, secondView, thirdView, nil];
    // Set pageControl's numberOfPages to 3
    self.pageControl.numberOfPages = [views count];
    // Set pageControl's currentPage to 0
    self.pageControl.currentPage = 0;

    // Call changePage each time value of pageControl changes
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
}

- (IBAction) changePage:(id)sender {
    NSLog(@"pageControl position %d", [self.pageControl currentPage]);
    int oldPageControlPosition = [self.pageControl currentPage] - 1;
    NSLog(@"old pageControl position %d", oldPageControlPosition);

    //0 Get the currentview as referenced by pageControl
    UIImageView *oldView = [views objectAtIndex:oldPageControlPosition];
    //NSLog(@"views objectAtIndex = %@",[views objectAtIndex:[self.pageControl currentPage]]);

    //1 Animate out the old view
    CGRect oldViewFrame = oldView.frame;
    oldViewFrame.origin.x -= 300;

    [UIView animateWithDuration:2.0
                          delay: 0.5
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         oldView.frame = oldViewFrame;
                     }

                     completion:nil];


    //3 Get next view from array
    UIImageView *nextView = [views objectAtIndex:[self.pageControl currentPage]];

    //4 Add it to mainview
    [self.view addSubview:nextView];

    //5 Animate in
    CGRect nextViewFrame = nextView.frame;
    nextViewFrame.origin.x += 40;    // shift right by 50pts
    nextViewFrame.origin.y += 40;

    [UIView animateWithDuration:1.0
                     animations:^{
                         nextView.frame = nextViewFrame;
                     }];
}

最後に到達したら、戻ろうとします。最後の位置は 2 で、その時点での oldPosition は 1 です。これは正しいです。配列は 0 から 1、2 に移動し、合計 3 つの位置になります。しかし、私が戻ったとき、ログは位置を 2 ではなく 1 として表示します。これは問題ありません。なぜなら、逆になったからです...しかし、古い位置は 2 ではなく 0 を示しています。

4

1 に答える 1

0

方向を判断することができなかったので、左スワイプと右スワイプのインスタンスでジェスチャ認識エンジンを使用することになりました。コードは次のとおりです。

//Help determine direction of swipe
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeNext:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.scrollView addGestureRecognizer:swipeLeft];

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipePrev:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.scrollView addGestureRecognizer:swipeRight];
于 2013-03-15T02:41:34.340 に答える