ios – 带点图像的自定义UIPageControl

ios – 带点图像的自定义UIPageControl,第1张

概述当我需要定制我的UIPageControl时,我使用了 this和 this解决方案. 稍微修改它为新版本的swift我们有: class myPageControl: UIPageControl { var activeImage: UIImage! var inactiveImage: UIImage! required init?(coder aDecoder: N 当我需要定制我的UIPageControl时,我使用了 this和 this解决方案.

稍微修改它为新版本的swift我们有:

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(),但是没有结果.我能做错什么?

解决方法 Swift 3.0 …你知道你是否接受陈述的风险:“修改现有控件的子视图是脆弱的”.

您必须在vIEwDIDAppear()中调用“updateDots()”,并为页面控件调用valueChanged处理程序.

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     } }
总结

以上是内存溢出为你收集整理的ios – 带点图像自定义UIPageControl全部内容,希望文章能够帮你解决ios – 带点图像的自定义UIPageControl所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1005664.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存