gpt4 book ai didi

ios - 快速更改页面指示器图像 [SMPageControl]/EAIntroView

转载 作者:搜寻专家 更新时间:2023-11-01 06:06:07 24 4
gpt4 key购买 nike

我正在按照 EAIntroView 中的示例尝试更改指示器图像

这是来自 EAIntroView 的 objective-c 代码

SMPageControl *pageControl = [[SMPageControl alloc] init];
pageControl.pageIndicatorImage = [UIImage imageNamed:@"pageDot"];
pageControl.currentPageIndicatorImage = [UIImage imageNamed:@"selectedPageDot"];
[pageControl sizeToFit];
intro.pageControl = (UIPageControl *)pageControl;
intro.pageControlY = 130.f;

这是我正在尝试实现的快速代码

    // SMPageControl
pageControl = SMPageControl()
pageControl.pageIndicatorImage = UIImage(named: "pageDot")
pageControl.currentPageIndicatorImage = UIImage(named: "selectedPageDot")
pageControl.sizeToFit()
pageControl.backgroundColor = UIColor.clearColor()

intro.pageControl = pageControl as? UIPageControl

swift代码在这里有一个警告

        intro.pageControl = pageControl as? UIPageControl

警告是:

Cast from 'SMPageControl!' to unrelated type 'UIPageControl' always fails

有什么帮助吗?

最佳答案

为了快速更改页面指示器图像 [SMPageControl]/EAIntroView,我创建了 UIPageControl 的自定义类,如下所示:

import UIKit 
class CustomPageControl: UIPageControl {

let activePage: UIImage = UIImage(named: "icon-selected-dot")!
let inActivePage: UIImage = UIImage(named: "icon-dot")!

override var numberOfPages: Int {
didSet {
updateDots()
}
}

override var currentPage: Int {
didSet {
updateDots()
}
}

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 {
var imageView = self.imageView(forSubview: view)
if imageView == nil {
if i == self.currentPage {
imageView = UIImageView(image: activePage)
} else {
imageView = UIImageView(image: inActivePage)
}
imageView!.center = view.center
view.addSubview(imageView!)
view.clipsToBounds = false
}
if i == self.currentPage {
imageView!.alpha = 1.0
imageView?.image = activePage
} else {
imageView!.alpha = 0.5
imageView?.image = inActivePage
}
i += 1
}
}

fileprivate func imageView(forSubview 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
}
}

然后在您的类(class)中只需添加这些行,您就可以为点使用自定义图像

//Custom page Control
let pageControl = CustomPageControl()
pageControl.updateDots()
intro.pageControl = pageControl

希望它对你有用! @ynamao

关于ios - 快速更改页面指示器图像 [SMPageControl]/EAIntroView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36965228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com