2013-03-12 66 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];