2016-08-10 73 views
1

當我需要自定義我的UIPageControl時,我使用thisthis解決方案。帶點圖像的自定義UIPageControl

稍微修改它迅速的新版本中,我們有:

class myPageControl: UIPageControl { 
    var activeImage: UIImage! 
    var inactiveImage: UIImage! 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     activeImage = UIImage(named: "active.png")! 
     inactiveImage = UIImage(named: "inactive.png")! 
    } 

    func updateDots() 
    { 
     for i in 0 ..< self.subviews.count 
     { 
      let dot : UIImageView = imageViewForSubview(self.subviews[i]) 
      if (i == self.currentPage) { 
       dot.image = activeImage 
      } 
      else { 
       dot.image = inactiveImage 
      } 
     } 
    } 

    func imageViewForSubview(view: UIView) -> UIImageView { 
     var dot: UIImageView? = nil 
     if (view.isKindOfClass(UIView)) { 
      for subview: UIView in view.subviews { 
       if (subview is UIImageView) { 
        dot = (subview as! UIImageView) 
       } 
      } 
      if dot == nil { 
       dot = UIImageView(frame: CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height)) 
       view.addSubview(dot!) 
      } 
     } 
     else { 
      dot = (view as! UIImageView) 
     } 
     return dot! 
    } 

    func setPage(page: Int) { 
     super.currentPage = page 
     self.updateDots() 
    } 
} 

我的問題是我無法改變的圖片,當你第一次啓動應用程序。 僅當頁面更改時纔會更改。

viewDidLoad()中,我加了setPage(0)updateDots(),但沒有結果。我可能做錯了什麼?

回答

6

Swift 3.0 ...你知道如果你接受陳述的風險是可以的:「修改現有控件的子視圖是脆弱的」。

您必須在viewDidAppear()和您的valueChanged頁面控件的處理函數中調用「updateDots()」。

import UIKit 

class CustomImagePageControl: UIPageControl { 

    let activeImage:UIImage = UIImage(named: "SelectedPage")! 
    let inactiveImage:UIImage = UIImage(named: "UnselectedPage")! 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     self.pageIndicatorTintColor = UIColor.clear 
     self.currentPageIndicatorTintColor = UIColor.clear 
     self.clipsToBounds = false 
    } 

    func updateDots() { 
     var i = 0 
     for view in self.subviews { 
      if let imageView = self.imageForSubview(view) { 
       if i == self.currentPage { 
        imageView.image = self.activeImage 
       } else { 
        imageView.image = self.inactiveImage 
       } 
       i = i + 1 
      } else { 
       var dotImage = self.inactiveImage 
       if i == self.currentPage { 
        dotImage = self.activeImage 
       } 
       view.clipsToBounds = false 
       view.addSubview(UIImageView(image:dotImage)) 
       i = i + 1 
      } 
     } 
    } 

    fileprivate func imageForSubview(_ view:UIView) -> UIImageView? { 
     var dot:UIImageView? 

     if let dotImageView = view as? UIImageView { 
      dot = dotImageView 
     } else { 
      for foundView in view.subviews { 
       if let imageView = foundView as? UIImageView { 
        dot = imageView 
        break 
       } 
      } 
     } 

     return dot 
    } 
} 
+1

下面是調用「updateDots()」的更好方法。還沒有嘗試過,但德米特里建議重寫numberOfPages和currentPage變量 - > http://stackoverflow.com/questions/12190147/customize-dot-with-image-of-uipagecontrol-at-index-0-of- uipagecontrol/41136917#41136917 – CodenameDuchess

0

您可以使用UIStackView來達到您想要的效果。

將您的activeImageView和其他inactiveImageView s分成aStackView。比只更改activeImageView的索引scrollViewDidScroll(_:)

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    let index = Int(scrollView.contentOffset.x/scrollView.frame.width) 
    aStackView.insertArrangedSubview(activeImageView, at: index) 
} 

ps。 UIStackView的insertArrangedSubview(_:at:)只是更新已排列子視圖的堆棧索引(如果它已位於排列的子視圖列表中)。