如何使用UIPageControl从左到右为UIImageView设置动画?

时间:2013-03-12 21:10:59

标签: animation uiimageview

我想用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。

1 个答案:

答案 0 :(得分: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];