The use of swift closures and OC block types

  • 2020-06-01 11:07:24
  • OfStack

I've seen swift before, and I don't know how the closures in OC, block, swift are implemented. Today, I searched 1 time on the Internet, compared 1 time with the implementation method of block type in OC, and then wrote 1 time of Demo test 1.

Instructions:

swift version

1. Declaration type typealias hideShowView = (Int) - > Void

2. Declare property var muFunc:hideShowView?

3. Pass the parameter func didSelectedToHideView(hideFunc: @escaping (Int)- > Void) { muFunc = hideFunc }

4. Listen for changes in values func tapEvent() {muFunc! (0)}

5. Use showView.didSelectedToHideView {(para) in NSLog("%d", para)}

6.Void is the return value type, Int is the parameter type, and hideShowView is the type name of the closure

OC version

. h file


// The statement 1 a block type 
typedef void(^HideShowViewBlock)(int index); 
// The statement 1 a block attribute 
@property (nonatomic,copy) HideShowViewBlock hideViewBlock;
// Method of passing parameters 
 - (void)didHideShowViewWithBlock:(HideShowViewBlock)hideViewBlock;

. m file


// Implements a function that passes parameters 
- (void)didHideShowViewWithBlock:(HideShowViewBlock)hideViewBlock
{
 self.hideViewBlock = hideViewBlock;
}
// Listen for changes in values that need to be passed 
- (void)tapEvent
{
 self.hideViewBlock(0);
}
swift  closure  Demo The code of 
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 = " test "
  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.
 }
}

conclusion


Related articles: