【问题标题】:How change the page control indicator on image slide inside scrollview using NSTimer如何使用 NSTimer 更改滚动视图内图像幻灯片上的页面控制指示器
【发布时间】:2018-06-22 13:23:45
【问题描述】:

这是工作正常的图像滑块。手动拖动时,图像会发生变化,指示器也会发生变化,但是当我添加动画时,只有图像会自动变化而不是指示器。

我想滚动图像而不是动画图像,以便指示器发生变化 就像它在拖动时改变一样。我也想给它添加计时器。

@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;

          imgView.animationImages      = animationArray;
          imgView.animationDuration    = 8;
          imgView.animationRepeatCount = 0;
          [imgView startAnimating];
          [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;
  }

我想滚动图像而不是动画图像,以便指示器发生变化 就像它在拖动时改变一样。我也想给它添加计时器。

【问题讨论】:

    标签: ios objective-c pagecontrol


    【解决方案1】:

    我认为您必须在 for 循环中设置当前页面,以便使用图像自动更改页面控件。

    //move this up
    self.pageControl.currentPage = 0;
    
    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;
              imgView.animationImages      = animationArray;
              imgView.animationDuration    = 8;
              imgView.animationRepeatCount = 0;
              [imgView startAnimating];
              self.pageControl.currentPage = i; //set i to current page
              [self.scroller addSubview:imgView];
      }
    

    【讨论】:

    • 静止页面控件没有自动移动..我已经删除了动画部分,我想应用 NSTimer ,你能告诉我在哪里以及如何应用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 2017-10-05
    相关资源
    最近更新 更多