ios - Swift:用于 uiimageview 的 UITapGestureRecognizer 在自定义 View 中终止应用程序

标签 ios swift uitapgesturerecognizer

我有一些从基类 (MSC_CLItem) 扩展的类,如下所示:

class MSC_CLItem
{
    var Type:MSC_CustomListType!

    func RenderUI(Point:CGPoint) -> UIView
    {
        return UIView(frame: CGRect.zero)
    }
}

每个扩展类必须重写 RenderUI func 来生成自身。所有扩展对象都将添加到 UIScrollView 中。现在我的问题是: 自定义 View 内带有 TapGesture 的 uiimageview 无法检测操作。例如我的类(class)是:

class MSC_CLItem_Tizer : MSC_CLItem
{
    var Title:String!
    var Video:MSC_CLItem_TizerVideo!
    var Detail:MSC_CLItem_TizerDetail!

    private init(title:String!)
    {
        super.init()
        self.Title = title
        super.Type = .Tizer
    }

    override func RenderUI(Point:CGPoint) -> UIView
    {
        let v = UIImageView()
        v.backgroundColor = UIColor.yellowColor()
        let screenSize: CGRect = UIScreen.mainScreen().bounds
        v.frame.size = CGSize(width: screenSize.width - 10, height: CGFloat(160))
        v.frame.origin = Point

        let vid = UIImageView()
        vid.image = UIImage(named: "default")
        vid.backgroundColor = UIColor.grayColor()
        vid.contentMode = .ScaleAspectFill
        vid.af_setImageWithURL(NSURL(string: "Image HTTP url")!)
        vid.frame.size = v.frame.size
        vid.frame.origin = CGPoint(x: 0, y: 0)
        vid.clipsToBounds = true
        vid.userInteractionEnabled = true
        v.userInteractionEnabled = true
        let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped:"))
        vid.addGestureRecognizer(tapRecognizer)
        v.addSubview(vid)
    }

    func imageTapped(gestureRecognizer: UITapGestureRecognizer) {
        //Not detected to here
    }
}

当我点击图像时发生以下错误:

NSForwarding:警告:“MSC_CLItem_Tizer”类的对象 0x7c8cc830 未实现 methodSignatureForSelector:-- 前方有麻烦 无法识别的选择器 -[MSC_CLItem_Tizer imageTapped:]

我很困惑我的代码的哪一部分是错误的?

谢谢

最佳答案

您必须将父类(super class)声明为 NSObject

class MSC_CLItem : NSObject
{
  ......
}


let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped:"))
tapGesture.numberOfTouchesRequired = 1;
tapGesture.numberOfTapsRequired = 1;
vid.addGestureRecognizer(tapRecognizer)
v.addSubview(vid)
self.view.addSubview(v)

更多详情

Does not implement methodSignatureForSelector: — trouble ahead

Got Unrecognized selector -replacementObjectForKeyedArchiver: crash when implementing NSCoding in Swift [Xcode 6 GM]

关于ios - Swift:用于 uiimageview 的 UITapGestureRecognizer 在自定义 View 中终止应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33866904/

相关文章:

ios - 与UIWebView相比,WKWebView重定向问题

ios - Xcode Interface Builder 高度/宽度设置与 View Controller

ios - 在 firebase 函数中拥有自己的完成处理程序(自定义类方法)

ios - 当我们在 ios 中的长按手势上有 tableview 单元格的索引路径时,如何在单元格中获取按钮的引用作为 subview

ios - 点击手势识别器干扰清除按钮

ios - IOS 中的股票代码水平 ScrollView ?

java - 检测 Android 中的数据更改

ios - 在 iOS 后台运行一个 Http 服务器

ios - Swift 中的函数可以返回不同的数据类型吗?

ios - 状态开始和结束未被触发