在图像幻灯片和页面控件中添加 NSTimer 以自动移动图像

Posted

技术标签:

【中文标题】在图像幻灯片和页面控件中添加 NSTimer 以自动移动图像【英文标题】:Adding NSTimer in image slide and page contol to move images automatically 【发布时间】:2018-06-24 16:34:15 【问题描述】:

当手动拖动图像随页面控制指示器的变化而变化时,图像滑块工作正常,但我想将计时器添加到以下代码并自动移动图像和指示器。请帮助我在此代码中应用 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;
  

【问题讨论】:

您当前使用哪个控件来显示此内容?滚动视图? yes ScrollView 里面有 ImageView。 我有相同的功能,但我使用collectionview,如果可能的话尝试使用collectionview。或者如果我能得到任何解决方案,我会为你提供。 你使用了带有页面控制指示器的collectionview?你有那个代码的链接吗?我想看看。 对不起兄弟,我没有任何链接,但我会给你一些想法。 【参考方案1】:

请为 NSTimer 尝试此代码(我使用 collectionView 方便使用)

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)
                
            
        
    

让计时器在您想要的时间和地点失效

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

【讨论】:

以上是关于在图像幻灯片和页面控件中添加 NSTimer 以自动移动图像的主要内容,如果未能解决你的问题,请参考以下文章

通过图像更改为单个 UIImageView 编写多个动画

如何在 iOS 中使用 UIImageview 添加 UIScrollview 的页面控件

如何添加不与更新内容功能冲突的自动图像幻灯片放映

VB在图片上添加文字的方法。

AS3 幻灯片的音频

将 ActiveX 控件插入到 Powerpoint 幻灯片中