检测UIPageControl页面更改

时间:2017-08-09 16:25:53

标签: ios swift uipagecontrol

我想在UIPageController内添加UICollectionViewCell。我在里面创建了一个带有collectionViewCell的自定义nib文件。在单元格内,我添加了UIPageControl

@IBOutlet weak var pageControl: UIPageControl!

    override func awakeFromNib() {
        super.awakeFromNib()

       self.isUserInteractionEnabled = true
        pageControl.isUserInteractionEnabled = true
        self.backgroundColor = UIColor.darkGray
       setupPageControl()
    }

private func setupPageControl() {

    pageControl.numberOfPages = 7
    pageControl.translatesAutoresizingMaskIntoConstraints = false
    pageControl.currentPageIndicatorTintColor = UIColor.orange
    pageControl.pageIndicatorTintColor = UIColor.lightGray.withAlphaComponent(0.8)
}

如何检测页面更改?点也在视图的中间,是否可以将它们放在中间?

enter image description here

2 个答案:

答案 0 :(得分:4)

enter image description here 来自Apple Documentation

  

当用户点击页面控件移动到下一页或上一页时,   控件发送valueChanged事件以供处理   代表。然后,委托可以将currentPage属性评估为   确定要显示的页面。页面控件只前进一页   在任何一个方向。当前查看的页面以白色表示   点。根据设备的不同,会显示一定数量的点   在它们被裁剪之前在屏幕上。

您也可以使用

class ViewClient : WebViewClient { MainActivity _activity; public int iTentativasLogin { get; private set; } public bool ErroReportado { get; private set; } public ViewClient(MainActivity activity) { _activity = activity; } public override void OnReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, string host, string realm) { Toast t ; if (Usuario.Length == 0 | Senha.Length == 0) { t = Toast.MakeText(Android.App.Application.Context, "Usuário ou senha não preenchidos", ToastLength.Long); t.SetGravity(GravityFlags.Center, 0, 0); t.Show(); handler.Cancel(); ErroReportado = true; } else { iTentativasLogin++; if (iTentativasLogin < 4) { handler.Proceed(Usuario, Senha); } else { t = Toast.MakeText(Android.App.Application.Context, "Usuário ou senha incorretos", ToastLength.Long); t.SetGravity(GravityFlags.Center, 0, 0); t.Show(); handler.Cancel(); ErroReportado = true; } } } }

答案 1 :(得分:1)

斯威夫特 5

1) addTaget

您可以在 setupPageControl 函数上添加一个检测值变化的目标:

private func setupPageControl() {

    pageControl.numberOfPages = 7
    pageControl.translatesAutoresizingMaskIntoConstraints = false
    pageControl.currentPageIndicatorTintColor = UIColor.orange
    pageControl.pageIndicatorTintColor = UIColor.lightGray.withAlphaComponent(0.8)
    pageControl.addTarget(self, action: #selector(pageControlHandle), for: .valueChanged)
}

创建一个选择器来改变你想要的值:

@objc private func pageControlHandle(sender: UIPageControl){
    print(sender.currentPage)

}