我有一个图像滑块,其中多个图像被添加到滚动视图。每个都存储在我的自定义数据类型(名为Slide)的数组中。我像这样循环:
for (index, slide) in slides.enumerated() {
let imageView = UIImageView()
imageView.image = UIImage(named: slide.image) //set the imageView with the imageName in this slide
imageView.contentMode = .scaleAspectFit
let xPos = self.view.frame.width * CGFloat(index) //calculate x depending upon slide index
imageView.frame = CGRect(x: xPos, y: -200, width: self.SVSlider.frame.width, height: self.SVSlider.frame.height)
SVSlider.contentSize.width = SVSlider.frame.width * CGFloat(index + 1) //add slide image to scrollview
SVSlider.addSubview(imageView)
//add tap function to slide
imageView.isUserInteractionEnabled = true
imageView.tag = index
let tapImage = UITapGestureRecognizer(target: self, action: #selector(slideAction(_:)))
imageView.addGestureRecognizer(tapImage)
}
每张幻灯片图片都附在“slideAction”方法的下方:
@objc func slideAction(_ sender: UITapGestureRecognizer) {
let selName = slides[(sender.view?.tag)!].target
NSObject.perform(Selector(selName))
}
但是单击图像时应用程序会抛出错误。通过调试,我可以看到“标签”被正确设置为幻灯片索引号。我想执行幻灯片对象中保存的目标操作。 (目前,目标是方法名称的字符串 - 以':'结尾)。
如何让“slideAction”转到幻灯片对象中的方法?
答案 0 :(得分:0)
试试这个,在自定义类callMethodDirectly
中创建目标方法并将其命名为
@objc func slideAction(_ sender: UITapGestureRecognizer) {
let selName = slides[(sender.view?.tag)!]
selName.callMethodDirectly()
}
class slide:NSObjcet
{
func callMethodDirectly()
{
// implement target code here
}
}