如何使用其他 objc 函数的变量? (长按并添加按钮)

Posted

技术标签:

【中文标题】如何使用其他 objc 函数的变量? (长按并添加按钮)【英文标题】:How to use other objc function's variable? (Long Press & Add a Button) 【发布时间】:2020-05-22 06:16:48 【问题描述】:

有什么方法可以使用其他objc函数的变量吗?这是场景:当我长按某个视图点时,菜单项出现,当我按下添加按钮时,我想在该位置创建一个圆形按钮

//Menu Items
@objc func longPressForView(sender: UILongPressGestureRecognizer) -> CGPoint 
    if sender.state == .began 
        let menu = UIMenuController.shared
        becomeFirstResponder()

        let menuItemAdd = UIMenuItem(title: "Add", action: #selector(addCircleMenuItemAction))
        let menuItemDelete = UIMenuItem(title: "Delete", action: #selector(handleMenuItemAction))
        menu.menuItems = [menuItemAdd, menuItemDelete]

        let location = sender.location(in: sender.view)
        let menuLocation = CGRect(x: location.x, y: location.y, width: 0, height: 0)
        menu.showMenu(from: sender.view!, rect: menuLocation)

        //I tried to return this location but I don't know how to use it
        return location
    


//Click Menu Items' Add Button to add a circle on that position.
@objc func addCircleMenuItemAction() 

    let longPressedlocation = longPressForView(sender: )

    print("Add item tapped")

    view.addSubview(addCircle)
    addCircle.layer.cornerRadius = CGFloat(circleAddDefaultSize/2)
    addCircle.translatesAutoresizingMaskIntoConstraints = false
    addCircle.backgroundColor = .black

    //Here is where I want to use the location to add a circle 
    addCircle.frame = CGRect(origin: longPressedlocation?????, size: CGSize(width: circleAddDefaultSize,height: circleAddDefaultSize))

【问题讨论】:

【参考方案1】:

您可以创建一个全局变量并在两个函数中使用它,就像这样

    //Menu Items
var location: CGPoint

@objc func longPressForView(sender: UILongPressGestureRecognizer) -> CGPoint 
    if sender.state == .began 
        let menu = UIMenuController.shared
        becomeFirstResponder()

        let menuItemAdd = UIMenuItem(title: "Add", action: #selector(addCircleMenuItemAction))
        let menuItemDelete = UIMenuItem(title: "Delete", action: #selector(handleMenuItemAction))
        menu.menuItems = [menuItemAdd, menuItemDelete]

        let location = sender.location(in: sender.view)
        let menuLocation = CGRect(x: location.x, y: location.y, width: 0, height: 0)
        menu.showMenu(from: sender.view!, rect: menuLocation)

        //I tried to return this location but I don't know how to use it
        self.location = location
        return location
    

@objc func addCircleMenuItemAction() 

    print("Add item tapped")

    view.addSubview(addCircle)
    addCircle.layer.cornerRadius = CGFloat(circleAddDefaultSize/2)
    addCircle.translatesAutoresizingMaskIntoConstraints = false
    addCircle.backgroundColor = .black

    //Here is where I want to use the location to add a circle 
    addCircle.frame = CGRect(origin: self.location, size: CGSize(width: circleAddDefaultSize,height: circleAddDefaultSize))

您的代码的问题是,当您长按视图时,longPressForView 会被执行,当您点击按钮时,它会在函数内再次执行:let longPressedlocation = longPressForView(sender: ),在这一行中。所以输出不会一样。

【讨论】:

以上是关于如何使用其他 objc 函数的变量? (长按并添加按钮)的主要内容,如果未能解决你的问题,请参考以下文章

UILongPressGesture 处理运动而不是长按

BranchIO Universal Link iOS 数据仅在长按并选择打开时有效

Swift 3 - 在 GMSMarker 上长按并打印其坐标(Google Maps iOS SDK)

使用 jQuery 在 Android 上确定长按(长按、按住)

JavaScript监听键盘事件

JavaScript监听键盘事件