34813

How do I animate a UIImageView left to right with UIPageControl?

Question:

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:

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

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.

Answer1:

I couldnt get it to determine direction so I ended up using gesture recognizers with swipe left and swipe right instances. Here is the code:

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

Recommend

  • How to remove dots in UITextfield?
  • IPhone Keyboard hides UISeachBar
  • Please explain me about this leak
  • iOS 7 vs iOS 8 Constraints differ in sizes
  • Simple promise example with bluebird and coffeescript works half the time
  • UnsatisfiedLinkError in Netbeans when calling native DLL after Windows 10 upgrade
  • Android+Eclipse: Can't push files to SD Card [duplicate]
  • How to display a Subclass of UIControl on the screen
  • How to avoid line breaks within |…| in Emacs
  • Segmentation fault when using strcpy?
  • View Animation does not work - ios Swift 3
  • Opengl Iphone SDK: How to tell if you're touching an object on screen?
  • Dynamically change cell's height while typing text, and reload the containing tableview for res
  • UIVisualEffectView creates a grey box instead of blurring
  • 'Terminating app due to uncaught exception'.. it couldn't find MainWindow, which no l
  • Merge two imageViews into one and save iOS swift
  • UITableView gives empty table, does not load data
  • How to Update UILabel from Another ViewController
  • UITableViewCell change selectedBackgroundView alpha?
  • this class is not key value coding-compliant for the key PerformRotate
  • Custom font for MKAnnotationView Callout
  • How to make a local git repository that mirrors an upstream repository?
  • Accessing Windows Azure Queues from client side javascript/jquery
  • iOS 9 errors and correct conversion to swift 2
  • How to format code on aptana 3?
  • Best practice for switching iPhone views?
  • JSON - slashes not escaping
  • Count New Lines in Text File
  • Google Maps api v3 get start and end coordinates of a street
  • Plotting densities in R
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • wxPython: displaying multiple widgets in same frame
  • R - Combining Columns to String Based on Logical Match
  • Adding a button at the bottom of a table view
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Convert array of 8 bytes to signed long in C++
  • embed rChart in Markdown
  • Unable to use reactive element in my shiny app
  • git trying to push non-existent file … after clearing cache
  • How to push additional view controllers onto NavigationController but keep the TabBar?