Swift 的 UIPageControl
Posted
技术标签:
【中文标题】Swift 的 UIPageControl【英文标题】:UIPageControl at Swift 【发布时间】:2019-12-16 11:43:55 【问题描述】:我在我的应用程序中添加了UIPageController
。我从其余服务中填充它,然后当单击页面之一时,它将直接转到最新的一个。我错过了一些东西,但我无法解决。我怎样才能选择一个而不是最新的。
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.FikirClick))
for item in self.ArrayList
say = say + 1
self.frameFikirImage.origin.x = self.FikirscrollView.frame.size.width * CGFloat(say) + 10
self.frameFikirImage.origin.y = 55
self.imgFikirImage = UIImageView(frame: self.frameFikirImage)
self.FikirscrollView.tag = item.id as! Int
tapGesture.view?.tag = item.id as! Int
self.imgFikirImage.sd_setImage(with: URL(string: item.picture!))
self.imgFikirImage.contentMode = UIView.ContentMode.scaleAspectFit
self.FikirscrollView.addSubview(self.imgFikirImage)
self.frameFikirText.origin.x = self.FikirscrollView.frame.size.width * CGFloat(say) + 25
self.frameFikirText.origin.y = 190
self.lblFikir = UILabel(frame: self.frameFikirText)
self.lblFikir.tintColor = UIColor.white
self.lblFikir.text = item.adi
self.lblFikir.font = self.lblFikir.font.withSize(20)
self.FikirscrollView.addSubview(self.lblFikir)
self.frameFikirButton.origin.x = self.FikirscrollView.frame.size.width * CGFloat(say) + 20
self.frameFikirButton.origin.y = 140
self.FikirscrollView.addGestureRecognizer(tapGesture)
self.FikirscrollView.contentSize = CGSize(width:self.FikirscrollView.frame.size.width * pageControlFikir,height: self.FikirscrollView.frame.size.height)
@objc func FikirClick(sender: UITapGestureRecognizer)
var tag = sender.view!.tag
print(tag);
【问题讨论】:
【参考方案1】:手势需要在for循环内
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.FikirClick))
因为它一次只用于 1 个视图,所以当您单击 var tag = sender.view!.tag
时它会添加到最后一个视图,而 tapGesture.view?.tag = item.id as! Int
会更新到循环的最后一个标签
还要确定
self.imgFikirImage.addGestureRecognizer(tapGesture)
并为图像视图启用用户交互
【讨论】:
以上是关于Swift 的 UIPageControl的主要内容,如果未能解决你的问题,请参考以下文章
Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift