• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

IOS设置UIViewController.view.frame = CGCGRect(x : -200. .) 不能点击按钮

[复制链接]
菜鸟教程小白 发表于 2022-12-13 14:02:50 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想把几个 UIViewController 放在一起:

leftViewController.view.frame = CGRect(x: -200, y: 0.0, width: size.width/drawerSize, height: size.height)

    // Center Drawer
    centerViewController.view.frame = CGRect(x: leftViewController.view.frame.width, y: 0.0, width: centerWidth, height: size.height)

    // Right Drawer
    rightViewController.view.frame = CGRect(x: centerViewController.view.frame.origin.x + centerViewController.view.frame.size.width, y: 0.0, width: size.width/drawerSize, height: size.height)

第一行我使用

leftViewController.view.frame = CGRect(x: -200, y....)

它们会正确显示,但我无法单击 leftViewController 上的按钮。

如果

leftViewController.view.frame = CGRect(x: 0.0, y...)

然后可以单击按钮,但这种布局不是我想要的。

完整代码由@Kevin Scardina Slide Sidebar Menu IOS 8 Swift 发布

它现在的功能如下图所示,我正在尝试将其修改为可以隐藏左右菜单的滑动菜单栏。

/*
  To use simply instantiate NVMDrawerController as your root view in your AppDelegate, or in the 
   StoryBoard.
  Once NVMDrawerController is instantiated, set the drawerSize of the NVMDrawerController, 
 and its leftViewControllerIdentifier, centerViewControllerIdentifier, and 
   rightViewControllerIdentifier to the Storyboard Identifier of the UIViewController 
  you want in the different locations.
*/
class NVMDrawerController: UIViewController {

// This is where you set the drawer size (i.e. for 1/3rd use 3.0, for 1/5 use 5.0)
var drawerSize:CGFloat = 4.0
var leftViewControllerIdentifier:String = "LeftController"
var centerViewControllerIdentifier:String = "CenterController"
var rightViewControllerIdentifier:String = "RightController"

private var _leftViewController:UIViewController?
var leftViewController:UIViewController {
    get{
        if let vc = _leftViewController {
            return vc;
        }
        return UIViewController();
    }
}
private var _centerViewController:UIViewController?
var centerViewController:UIViewController {
    get{
        if let vc = _centerViewController {
            return vc;
        }
        return UIViewController();
    }
}
private var _rightViewController:UIViewController?
var rightViewController:UIViewController {
    get{
        if let vc = _rightViewController {
            return vc;
        }
        return UIViewController();
    }
}

static let NVMDrawerOpenLeft = 0
static let NVMDrawerOpenRight = 1
var openSide:Int {
    get{
        return _openSide;
    }
}
private var _openSide:Int = NVMDrawerOpenLeft

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    // Instantiate VC's with storyboard ID's
    _leftViewController = instantiateViewControllers(leftViewControllerIdentifier)
    _centerViewController = instantiateViewControllers(centerViewControllerIdentifier)
    _rightViewController = instantiateViewControllers(rightViewControllerIdentifier)

    // Call configDrawers() and pass the drawerSize variable.
    drawDrawers(UIScreen.mainScreen().bounds.size)

    self.view.addSubview(leftViewController.view)
    self.view.addSubview(centerViewController.view)
    self.view.addSubview(rightViewController.view)

}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in
        // This is for beginning of transition
            self.drawDrawers(size)
        }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
            // This is for after transition has completed.
        })

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Drawing View

func drawDrawers(size:CGSize) {
    // Calculate Center View's Size
    let centerWidth = (size.width/drawerSize) * (drawerSize - 1)

    // Left Drawer
    leftViewController.view.frame = CGRect(x: 0.0, y: 0.0, width: size.width/drawerSize, height: size.height)

    // Center Drawer
    centerViewController.view.frame = CGRect(x: leftViewController.view.frame.width, y: 0.0, width: centerWidth, height: size.height)

    // Right Drawer
    rightViewController.view.frame = CGRect(x: centerViewController.view.frame.origin.x + centerViewController.view.frame.size.width, y: 0.0, width: size.width/drawerSize, height: size.height)
    //rightViewController = rc

    // Capture the Swipes
    let swipeRight = UISwipeGestureRecognizer(target: self, action: Selector("swipeRightAction:"))
    swipeRight.direction = .Right
    centerViewController.view.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: Selector("swipeLeftAction:"))
    swipeLeft.direction = .Left
    centerViewController.view.addGestureRecognizer(swipeLeft)

    if(openSide == NVMDrawerController.NVMDrawerOpenLeft){
        openLeftDrawer()
    }
    else{
        openRightDrawer()
    }
}

// MARK: - Open Drawers

func openLeftDrawer() {
    _openSide = NVMDrawerController.NVMDrawerOpenLeft
    UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animations:
        { () -> Void in
            // move views here
            self.view.frame = CGRect(x: 0.0, y: 0.0, width: self.view.bounds.width, height: self.view.bounds.height)
        }, completion:
        { finished in
    })
}

func openRightDrawer() {
    _openSide = NVMDrawerController.NVMDrawerOpenRight
    UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animations:
        { () -> Void in
            // move views here
            self.view.frame = CGRect(x: self.view.bounds.origin.x - self.leftViewController.view.bounds.size.width, y: 0.0, width: self.view.bounds.width, height: self.view.bounds.height)
        }, completion:
        { finished in
    })
}

// MARK: - Swipe Handling

func swipeRightAction(rec: UISwipeGestureRecognizer){
    self.openLeftDrawer()
}

func swipeLeftAction(rec:UISwipeGestureRecognizer){
    self.openRightDrawer()
}

// MARK: - Helpers

func instantiateViewControllers(storyboardID: String) -> UIViewController {
    if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("\(storyboardID)") as? UIViewController{
        return viewController;
    }

    return UIViewController();
}

}

enter image description here



Best Answer-推荐答案


当您的 View 在其父 View 之外时,它无法接收任何触摸事件。您应该在 UIView(其父 View ) touchWithEvents 函数中枚举 subview 并使其接收事件。

关于IOS设置UIViewController.view.frame = CGCGRect(x : -200. .) 不能点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35100177/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap