滑动到下一张图片时,PageControl 没有动画

Posted

技术标签:

【中文标题】滑动到下一张图片时,PageControl 没有动画【英文标题】:PageControl dosn't animate when swiping to next picture 【发布时间】:2018-12-15 22:01:43 【问题描述】:

我有一组图片,这些图片已传递到新 ViewController 上的 imageView。我可以让我的 pageControl 显示这些点,但是当我滑动到下一张图片时它们没有动画。关于我所缺少的任何想法,因为我无法弄清楚?谢谢!

import UIKit
import GoogleMobileAds


class imageViewController: UIViewController,GADBannerViewDelegate, UIGestureRecognizerDelegate, UIScrollViewDelegate 

    var bannerView: GADBannerView!

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var myImageView: UIImageView!
    @IBOutlet weak var pageControl: UIPageControl!


    var passedImage : UIImage! = nil
    var passedArray : [UIImage]!
    var index = 0


    override func viewDidLoad()
        super.viewDidLoad()
        self.myImageView.image = passedArray.first

        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 5.0

        configurePageControl()


        //---------ADS----------------
        // In this case, we instantiate the banner with desired ad size.
        bannerView = GADBannerView(adSize: kGADAdSizeBanner)
        addBannerViewToView(bannerView)
    

    func viewForZooming(in scrollView: UIScrollView) -> UIView? 
        return myImageView
    

    func configurePageControl() 
        self.pageControl.numberOfPages = passedArray.count
        self.pageControl.currentPage = 0
        self.pageControl.pageIndicatorTintColor = UIColor.black
    self.pageControl.currentPageIndicatorTintColor = UIColor.red
        self.view.addSubview(pageControl)

    



    @IBAction func pictureswipe(_ sender: Any) 
        let pictureString = self.passedArray[index]
        self.myImageView.image = pictureString

        index = (index < passedArray.count-1) ? index+1 : 0
    

    @IBAction func pictureswipeback(_ sender: Any) 
        let pictureString = self.passedArray[index]
        self.myImageView.image = pictureString

        index = (index > passedArray.count+1) ? index-1 : 0
    

【问题讨论】:

【参考方案1】:

您还需要使用新索引更新您的pageControlcurrentPage,尝试更新您的IBAction 方法如下。

    @IBAction func pictureswipe(_ sender: Any) 
    let pictureString = self.passedArray[index]
    self.myImageView.image = pictureString
    self.pageControl.currentPage = index
    index = (index < passedArray.count-1) ? index+1 : 0



@IBAction func pictureswipeback(_ sender: Any) 
    let pictureString = self.passedArray[index]
    self.myImageView.image = pictureString
    self.pageControl.currentPage = index
    index = (index > passedArray.count+1) ? index-1 : 0

【讨论】:

以上是关于滑动到下一张图片时,PageControl 没有动画的主要内容,如果未能解决你的问题,请参考以下文章

引导轮播 - 滑动时暂停 html 视频

部署后,fancybox 无法移动到下一张/上一张图片

用JS 怎么实现点击上一张图片,下一张图片的显示?

Swiper滚动图片显示当前图片是第几张图片

iOS 使用带有 UILabel 的 UITapGestureRecognizer 滑动到下一页

如何知道用户何时在 PDFKit 的 PDFView 中滑动到下一页?