Guest User

Untitled

a guest
Apr 28th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. Scrolling an image continuosly
  2. - (void)viewDidLoad {
  3. [super viewDidLoad];
  4.  
  5. // add the last image (image4) into the first position
  6. [self addImageWithName:@"image4.jpg" atPosition:0];
  7.  
  8. // add all of the images to the scroll view
  9. for (int i = 1; i < 5; i++) {
  10. [self addImageWithName:[NSString stringWithFormat:@"image%i.jpg",i] atPosition:i];
  11. }
  12.  
  13. // add the first image (image1) into the last position
  14. [self addImageWithName:@"image1.jpg" atPosition:5];
  15.  
  16. scrollView.contentSize = CGSizeMake(320, 2496);
  17. [scrollView scrollRectToVisible:CGRectMake(0,416,320,416) animated:NO];
  18. }
  19.  
  20. - (void)addImageWithName:(NSString*)imageString atPosition:(int)position {
  21. // add image to scroll view
  22. UIImage *image = [UIImage imageNamed:imageString];
  23. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  24. imageView.frame = CGRectMake(0,position*416,320, 416);
  25. [scrollView addSubview:imageView];
  26. [imageView release];
  27. }
  28.  
  29. - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
  30. NSLog(@"%f",scrollView.contentOffset.y);
  31. // The key is repositioning without animation
  32. if (scrollView.contentOffset.y == 0) {
  33. // user is scrolling to the left from image 1 to image 4
  34. // reposition offset to show image 4 that is on the right in the scroll view
  35. [scrollView scrollRectToVisible:CGRectMake(0,1664,320,416) animated:NO];
  36. }
  37. else if (scrollView.contentOffset.y == 2080) {
  38. // user is scrolling to the right from image 4 to image 1
  39. // reposition offset to show image 1 that is on the left in the scroll view
  40. [scrollView scrollRectToVisible:CGRectMake(0,416,320,416) animated:NO];
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment