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

时间:2018-09-04 11:07:00

标签: ios swift xcode

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

外观:

How it looks

外观如何:

How it should look

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

1 个答案:

答案 0 :(得分:0)

尝试不更改图像,而是在页面控件子视图上使用transform属性。像这样

 pageControl.subviews[pageControl.currentPage].transform = CGAffineTransform(scaleX: 1.5, y: 1.5)