OStack程序员社区-中国程序员成长平台

标题: IOS设置UIViewController.view.frame = CGCGRect(x : -200. .) 不能点击按钮 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:02
标题: IOS设置UIViewController.view.frame = CGCGRect(x : -200. .) 不能点击按钮

我想把几个 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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4