2013-03-12 57 views
0

我想通过UIPageControl从左到右动画UIImageView。我有这个代码,但它似乎有点奇怪...特别是因为当我到达图像3时我会做什么?我会回来,那会陷入困境的的PageControl阵列中的意见,数学计算,我有它设置方式:如何使用UIPageControl从左到右设置UIImageView的动画效果?

-(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.

回答

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];