Мне было интересно, как я могу расширить UIView и обработать целевой селектор UIButton внутри функции расширения, но я не понимаю его. Он просто не отвечает на целевой селектор. Я даже пытался использовать раскрытие прокси. Вот код.UIButton внутри расширения UIView
расширение 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;
}
}
}
}