我想知道如何扩展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;
}
}
}
}
答案 0 :(得分:0)
尝试在执行方法前添加@objc
,即@objc func execute()...
请参阅this link。
如果您的Swift类继承自Objective-C类,则所有 类中的方法和属性可以作为Objective-C获得 选择。否则,如果你的Swift类没有继承 Objective-C类,您需要为要使用的符号添加前缀 具有@objc属性的选择器