【发布时间】:2016-11-21 06:28:13
【问题描述】:
我以编程方式将 imageViews 中的图像添加到 for 循环中的父 scrollView 中,用于数组中的 5 个对象。每个 imageView 都有一个 TapGestureRecognizer,所以我可以点击它们,它们会调用一个函数。
我希望该函数将 scrollView 中的 setContentOffset 设置为点击图像的 x 值中心的位置。我不确定如何从点击的 imageView 中提取 CGPoint。任何帮助都会很棒。
这是我目前所拥有的:
override func viewDidLoad() {
super.viewDidLoad()
/*for (index, element) in list.enumerated()*/
for (index, element) in cardsInHand.enumerated() {
let img = UIImage(named: "\(element)")
let imgView = UIImageView(image: img)
imgView.isUserInteractionEnabled = true
handScrollView.addSubview(imgView)
//add tap gesture recognizer
let tapRec = UITapGestureRecognizer(target: self, action: #selector(self.cardTapped))
imgView .addGestureRecognizer(tapRec)
imgView.frame = CGRect(x: 5 + (5 + WIDTH) * CGFloat(index), y: 5, width: WIDTH, height: HEIGHT)
}
//test at card tapped function
func cardTapped(){
handScrollView.setContentOffset(CGPoint(x: WIDTH, y: 0), animated: true)
}
到目前为止,它只是将scrollView上的偏移量设置为WIDTH(即110),无论点击哪张卡,因为我不知道如何引用点击的imgView。
【问题讨论】:
标签: ios iphone swift uiscrollview uikit