自定义页面控件,其中当前点的图像大于其余不工作的图像

问题描述 投票:0回答:1

我使用过一些以前的解决方案。 stackoverflow中的以下代码有所帮助,但当前的点不会显示大于其余的代码(尽管名为:“currentPageDot”的图像大于UIImage(名称:“defaultPageDot”))。我也不想在他们变大的时候打扰他们的位置。我应该如何修改此代码来执行相同的操作。

看起来如何:

应该如何看待:

import Foundation
import UIKit

class EnhancedPageControl: UIPageControl {

    let activeImage:UIImage = UIImage(named: "currentPageDot")!
    let inactiveImage:UIImage = UIImage(named: "defaultPageDot")!

    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 = nil
                    imageView.image = self.activeImage
                } else {
                    imageView.image = nil
                    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 swift xcode
1个回答
0
投票

尝试不更改图像,但在页面控制子视图上使用transform属性。像这样的东西

 pageControl.subviews[pageControl.currentPage].transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
© www.soinside.com 2019 - 2024. All rights reserved.