简体   繁体   中英

Adding NSTimer in image slide and page contol to move images automatically

Here is the image slider works fine when dragging manually the image changes with page control indicator change but I want to add the timer to the following code and move the images and indicator automatically. Please help me to apply NS timer in this code and also want to move from the last image to 1st image and so on.

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

Please try this code for NSTimer (i use collectionView for easy use)

var timer : Timer?
var currentFeaturedCouponIndex = 0

//MARK: - NSTimer -
    func setupTimer(){
        timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.scrollFeatured), userInfo: nil, repeats: true);
    }

func scrollFeatured(){
        if self.arrFeaturedCoupon.count > 0 {
            //Becauese we scroll coupon if total is > 1
            if self.arrFeaturedCoupon.count > 1 {
                if self.currentFeaturedCouponIndex < self.arrFeaturedCoupon.count - 1 {
                    self.currentFeaturedCouponIndex = self.currentFeaturedCouponIndex + 1
                }
                else {
                    self.currentFeaturedCouponIndex = 0
                }

                self.pageControl.currentPage = currentFeaturedCouponIndex

                //When currentFeaturedCouponIndex == 0  we make animated: false
                if self.currentFeaturedCouponIndex == 0 {
                     self.featuredCouponColView.scrollToItem(at: self.featuredCouponCurrentIndexPath, at: UICollectionViewScrollPosition.left, animated: false)
                }
                else {
                     self.featuredCouponColView.scrollToItem(at: self.featuredCouponCurrentIndexPath, at: UICollectionViewScrollPosition.left, animated: true)
                }
            }
        }
    }

To invalidate timer when and where you want

func invalidateTimer(){
        if timer != nil {
            self.timer!.invalidate()
            self.timer = nil
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM