0

これは、手動でドラッグすると画像スライダーが正常に機能し、ページ コントロール インジケーターの変更に伴って画像が変更されますが、次のコードにタイマーを追加して、画像とインジケーターを自動的に移動したいと考えています。このコードで NS タイマーを適用し、最後の画像から最初の画像などに移動するのを手伝ってください。

@implementation DashboardViewController {
  NSArray * animationArray;
}

@synthesize scroller = scroller;
@synthesize pageControl = pageControl;




-(void) viewDidLoad {
  [super viewDidLoad];

  scroller.pagingEnabled = YES;
  scroller.showsHorizontalScrollIndicator = NO;
  scroller.delegate = self;



  animationArray = [NSArray arrayWithObjects: [UIImage imageNamed: @ "image1.jpg"],
    [UIImage imageNamed: @ "image2.jpg"],
    [UIImage imageNamed: @ "image3.jpg"],
    [UIImage imageNamed: @ "image4.png"],
    [UIImage imageNamed: @ "image5.jpg"],
    nil
  ];



  CGRect scrollFrame = CGRectMake(0, 0, self.view.frame.size.width, self.scroller.frame.size.height);
  scroller.frame = scrollFrame;



  self.scroller.contentSize = CGSizeMake(self.scroller.frame.size.width * animationArray.count, self.scroller.frame.size.height);

  for (int i = 0; i < animationArray.count; i++) {
    CGRect frame;
    frame.origin.x = self.scroller.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scroller.frame.size;
    UIImageView * imgView = [
      [UIImageView alloc] initWithFrame: CGRectMake(self.scroller.frame.size.width * i, 0, self.scroller.frame.size.width, self.scroller.frame.size.height)
    ];
    imgView.image = [animationArray objectAtIndex: i];
    imgView.frame = frame;


    [self.scroller addSubview: imgView];
  }

  self.pageControl.currentPage = 0;

}


-(void) scrollViewDidScroll: (UIScrollView * ) sender {
    if (!pageControlBeingUsed) {
      // Switch the indicator when more than 50% of the previous/next page is visible
      CGFloat pageWidth = self.scroller.frame.size.width;
      int page = floor((self.scroller.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
      self.pageControl.currentPage = page;
    }
  }

  -(void) scrollViewWillBeginDragging: (UIScrollView * ) scrollView {
    pageControlBeingUsed = NO;
  }

  -(void) scrollViewDidEndDecelerating: (UIScrollView * ) scrollView {
    [self setIndiactorForCurrentPage];

  }


  -(void) setIndiactorForCurrentPage {
    uint page = scroller.contentOffset.x / scroller.frame.size.width;
    [pageControl setCurrentPage: page];


  }



  - (IBAction) changePage {
    // Update the scroll view to the appropriate page
    CGRect frame;

    pageControl.currentPage = animationArray.count;

    frame.origin.x = self.scroller.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scroller.frame.size;
    [self.scroller scrollRectToVisible: frame animated: YES];
    pageControlBeingUsed = YES;


  }



  - (void) didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

    self.scroller = nil;
    self.pageControl = nil;
  }
4

1 に答える 1