繁体   English   中英

UIView扩展内的UIButton

[英]UIButton inside UIView extension

我想知道如何扩展UIView并在扩展函数中处理UIButton的目标选择器,但我做得不好。 它只是不响应目标选择器。 我什至尝试使用代理人披露。 这是代码。

扩展UIView {

class ClosureDispatch {

    let action: () -> ()

    init(f:() -> ()) {
        self.action = f
    }

    func execute() -> () {
        action()
    }

}


func showMessageView(inView: UIView) {


    var isShowing = false;

    let height: CGFloat = 80.0;
    let paddingBottom: CGFloat = 20.0;


    var backgroundView: UIView  = UIView(frame: CGRectMake(0, inView.frame.size.height, inView.frame.size.width, height))
    backgroundView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.8);

    let close: ClosureDispatch = ClosureDispatch(f: {

        NSLog("go!")

        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

            backgroundView.frame = CGRectMake(0, inView.frame.size.height, inView.frame.size.width, height)

            }) { (completed) -> Void in

                isShowing = false;

        }
    })


    var closeButton: UIButton = UIButton(frame: CGRectMake(inView.frame.size.width-height-paddingBottom, 0, height, height))
    closeButton.setTitle("Close", forState: .Normal)
    closeButton.addTarget(close, action: "execute", forControlEvents: .TouchUpInside)
    closeButton.backgroundColor = UIColor.blueColor()

    closeButton.userInteractionEnabled = true;
    backgroundView.userInteractionEnabled = true;

    backgroundView.addSubview(closeButton)
    inView.addSubview(backgroundView)

    UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

        backgroundView.frame = CGRectMake(0, inView.frame.size.height-height-paddingBottom, inView.frame.size.width, height)

    }) { (completed) -> Void in

        isShowing = true;

        UIView.animateWithDuration(0.5, delay: 3.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

            backgroundView.frame = CGRectMake(0, inView.frame.size.height, inView.frame.size.width, height)

            }) { (completed) -> Void in

                isShowing = false;
        }
    }


}

}

尝试在execute方法前面添加@objc ,即@objc func execute()...

请参阅此链接

如果您的Swift类是从Objective-C类继承的,则该类中的所有方法和属性都可用作Objective-C选择器。 否则,如果您的Swift类不继承自Objective-C类,则需要在要用作选择器的符号前添加@objc属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM