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 个位置。但是当我返回时,日志将位置显示为 1 而不是 2,这很好,因为我颠倒了……但旧位置显示的是 0 而不是 2。

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 回答