首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >收听LongPressGesture on UITabBarItem

收听LongPressGesture on UITabBarItem
EN

Stack Overflow用户
提问于 2018-03-03 20:39:51
回答 1查看 552关注 0票数 1

我想在单个long press上附加一个简单的UITabBarItem。这在swift 4中是可能的吗?我尝试了如下所示,但是UITabBarItem没有我可以实现的事件成员

uiTabBar是出口。我也看到了我可以使用UITabBarDelegate,但我还没有让它开始工作。longTap函数的签名如下所示:

代码语言:javascript
复制
@objc func longTap(_ sender: UIGestureRecognizer){}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-06 08:43:15

最后,我将一个UIButton放在我想要将UILongPressGestureRecognizer附加到的UITabBarItem之上。我使用以下函数编写了按钮:

代码语言:javascript
复制
 //Declare the button 
 let uiTabBar = UIButton(frame: CGRect.zero)

func setupMiddleButton() {
        // Create image
        let africaIcon = UIImage(named: "ic_africa")

        let numberOfItems = CGFloat(tabBar.items!.count)
        let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
        uiTabBar.frame = CGRect(x: 0, y: 0, width: tabBarItemSize.width, height: tabBar.frame.size.height)
        var menuButtonFrame = uiTabBar.frame
        menuButtonFrame.origin.y = self.view.bounds.height - menuButtonFrame.height - self.view.safeAreaInsets.bottom
        menuButtonFrame.origin.x = self.view.bounds.width/2 - menuButtonFrame.size.width/2
        uiTabBar.frame = menuButtonFrame
        uiTabBar.setImage(africaIcon, for: UIControlState.normal)
        //uiTabBar.backgroundColor = UIColor.green
        self.view.addSubview(uiTabBar)
        self.view.layoutIfNeeded()
    }

为了附加这些事件,我做了以下工作:

代码语言:javascript
复制
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
                tapGesture.numberOfTapsRequired = 1
                uiTabBar.addGestureRecognizer(tapGesture)
                let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
                uiTabBar.addGestureRecognizer(longGesture)
                //Set default tab
                self.selectedIndex = 1;

选择器方法如下所示:

代码语言:javascript
复制
@objc func normalTap(_ sender: UIGestureRecognizer){
    self.selectedIndex = 1;
}

@objc func longTap(_ sender: UIGestureRecognizer){
    print("Long tap")
    if sender.state == .ended {
        print("UIGestureRecognizerStateEnded")
        //Do Whatever You want on End of Gesture
        //This is when the long event is triggered
    }
    else if sender.state == .began {
        print("UIGestureRecognizerStateBegan.")
        //Do Whatever You want on Began of Gesture
    }
}

中间的“TabBar”是用我想要的事件创建的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49088995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档