Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I animate a UIImageView left to right with UIPageControl?
    primarykey
    data
    text
    <p>I want to animate a UIImageView left to right with UIPageControl. I have this code but it seems a bit odd...Specifically because what would I do when I reach image 3? I would have to return and that would mess up the mathematical calculations of the views in the pageControl array the way I have it set up:</p> <pre><code> -(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; }]; } </code></pre> <p>When I reach the end, I try to return, the last position is 2 and oldPosition at that time is 1. Which is correct, array goes from 0 to 1 to 2 for a total of 3 positions. But when I return, the log displays position as 1 instead of 2, which is fine, because I reversed...but old position shows 0 instead of 2.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload