【发布时间】:2015-02-07 20:33:23
【问题描述】:
我想知道如何扩展 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;
}
}
}
}
【问题讨论】: