//声明一个block类型 typedef void(^HideShowViewBlock)(int index); //声明一个block属性 @property (nonatomic,copy) HideShowViewBlock hideViewBlock; //传递参数的方法 - (void)didHideShowViewWithBlock:(HideShowViewBlock)hideViewBlock;
//实现传递参数的函数
- (void)didHideShowViewWithBlock:(HideShowViewBlock)hideViewBlock
{
self.hideViewBlock = hideViewBlock;
}
//监听需要传递值的变化
- (void)tapEvent
{
self.hideViewBlock(0);
}
swift 闭包 Demo的代码
class ShowView: UIView
{
typealias hideShowView = (Int) -> Void
var muFunc:hideShowView?
private var viewFram:CGRect?
override init(frame:CGRect )
{
super.init(frame: frame)
self.viewFram = frame
self.backgroundColor = UIColor.gray
self.createUI()
}
func createUI()
{
var centerLabel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: (self.viewFram?.width)!, height: 20))
centerLabel.center = self.center
centerLabel.text = "测试"
centerLabel.textColor = UIColor.white
centerLabel.textAlignment = NSTextAlignment.center
centerLabel.font = UIFont.boldSystemFont(ofSize: 16.0)
self.addSubview(centerLabel)
let tap = UITapGestureRecognizer.init(target: self, action: #selector(ShowView.tapEvent))
tap.cancelsTouchesInView = false
self.addGestureRecognizer(tap)
}
func tapEvent()
{
muFunc!(0)
}
func didSelectedToHideView(hideFunc:@escaping (Int)->Void)
{
muFunc = hideFunc
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class ViewController: UIViewController
{
let WIDTH = UIScreen.main.bounds.size.width
let HEIGHT = UIScreen.main.bounds.size.height
override func viewDidLoad()
{
super.viewDidLoad()
}
@IBAction func selectedToDoSomething(_ sender: UIButton)
{
let showView = ShowView.init(frame: CGRect.init(x: 0, y: 0, width: WIDTH/2, height: WIDTH/2))
showView.center = self.view.center
showView.didSelectedToHideView { (para) in
NSLog("%d", para)
}
self.view.addSubview(showView)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有