当图像滚动然后导航栏更新

时间:2018-05-19 11:45:49

标签: ios swift4

我有一组图像,我正在使用滚动视图移动图像,所有工作都很精细。 因为我是Swift的新手所以现在我需要一个解决方案,当图像移动时,我如何检查图像总数中显示的图像编号(或图像索引编号),然后相应地更新导航栏。

以下是我正在寻找的附件样本:

enter image description here

移动图片代码

class ImageScrollViewController: UIViewController {


@IBOutlet weak var mainScrollView: UIScrollView!

var imageArray = [UIImage]()
override func viewDidLoad() {

    //mainScrollView.frame = view.frame
    imageArray.append(UIImage(named: "nature-2")!)
    imageArray.append(UIImage(named: "nature")!)
    imageArray.append(UIImage(named: "nature4")!)
    imageArray.append(UIImage(named: "nature5")!)
    imageArray.append(UIImage(named: "nature6")!)

    for i in 0..<imageArray.count{
        let imageView = UIImageView()
        imageView.image = imageArray[i]
        imageView.contentMode = .scaleAspectFit
        let xPosition = (self.view.frame.width  * CGFloat(i)) + CGFloat(8)
        imageView.frame = CGRect(x: xPosition , y: 0, width: self.mainScrollView.frame.width - CGFloat(16), height: self.mainScrollView.frame.height)

        mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i+1)
        mainScrollView.addSubview(imageView)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

希望你们这些人,理解我的问题。 任何帮助将不胜感激。

先谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用..... .on(click, function (){ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content') }, $.ajax({ url: "adminUser/exportselected/1", type:'POST', data: {selected:selected, selectedField:selectedField}, dataType : "json", success: function(data) { window.location = data.path; if($.isEmptyObject(data.error)){ //alert(data); }else{ //alert(data.error); } } }); contentOffset方法UIScrollViewDelegate中持续跟踪排名。

因此,如果屏幕宽度为1000px:

scrollViewDidScroll

下面,func scrollViewDidScroll(_ scrollView: UIScrollView) { // how far from beginning of scrollView (ex: user scrolls 3300px to the right) let scrollDistance = mainScrollView.contentOffset.x // size of 1 image (each is 1000px wide + 8px spacing between images) let sizeOfOneImage = self.view.frame.width + 8 // number of images scrolled (3300px scrolled right = ~3.3 images scrolled so far...) let numberOfScreensScrolled = scrollDistance / sizeOfOneImage // floor(#) turns 3.3 into 3 (removes everything after decimal) var imageInteger = floor(numberOfScreensScrolled) // need +1, or else 1st image will be "0", 2nd image will be "1", etc. imageInteger = imageInteger + 1 // # / total self.title = "\(imageInteger)/\(imageArray count)" } 存在,当下一个图像的~50%出现在屏幕上时,可以转换到下一个数字(可以更改为最好的)。

+ self.view.frame.width/2