繁体   English   中英

幻灯片菜单-从右向左和向后移动

[英]Slide menu - move from right to left and back

当我单击屏幕菜单时,我从右到左显示幻灯片菜单。 如果单击按钮从左向右移动,如何编写代码?

我的幻灯片菜单:

在此处输入图片说明

我的代码:

private void UpdateTableViewPosition(CGPoint positon)
    {
        // set the position of the button based on the provided argument
        _buttonToggleVisibility.Frame = new CoreGraphics.CGRect(positon.X , _buttonToggleVisibility.Frame.Y, _buttonToggleVisibility.Frame.Width, _buttonToggleVisibility.Frame.Height);

        // TODO:
        // if button is outside of the screen
        //     move it back into the screen

        // then move tableview so it is right aligned of the button
        _tableView.Frame = new CoreGraphics.CGRect(_buttonToggleVisibility.Frame.Right, _tableView.Frame.Y, _tableView.Frame.Width, _tableView.Frame.Height);
    }

    public override void TouchesEnded(NSSet touches, UIEvent evt)
    {
        base.TouchesEnded(touches, evt);

        UITouch touch = (UITouch)touches.AnyObject;

        _moving = false;

        // TODO: fix so that only the button is clickable
        // if the touch clicked the button
        //     open (or close) the view with the following animation code
        UIView.Animate(0.9f, () =>
        {
            // TODO: this animation code is incorrect. Currently it moves the view 100px relatively to the 
            // current position, but it should rather either set the button to the left corner or the right 
            // corner at fixed positions
            UpdateTableViewPosition(new CGPoint(_buttonToggleVisibility.Frame.X - _buttonToggleVisibility.Frame.Left, 0));

        }, null);
    }

    public override void TouchesCancelled(NSSet touches, UIEvent evt)
    {
        base.TouchesCancelled(touches, evt);

        _moving = false;
    }

    public override void TouchesBegan(Foundation.NSSet touches, UIEvent evt)
    {
        base.TouchesBegan(touches, evt);

        UITouch touch = (UITouch)touches.AnyObject;

        CoreGraphics.CGPoint pos = touch.LocationInView(this);

         if (pos.X > _buttonToggleVisibility.Frame.X && pos.X < _buttonToggleVisibility.Frame.Right && pos.Y > _buttonToggleVisibility.Frame.Y && pos.Y < _buttonToggleVisibility.Frame.Bottom)
        {
            // did click on the view
            _moving = true;
        }

        _buttonToggleVisibility.TouchUpInside += (sender, e) =>
       {

       };
    }
}

我使用AutoLayout和UIView动画上下移动控件视图。 (请考虑使用键盘,但具有更大的灵活性。)面板具有“完成”按钮,该按钮可以动态地将面板(及其子视图)移出视线。

由于我有一块木板,因此这里有一些额外的代码可以解决tag属性。 那是我找到与哪个板一起工作的地方。

关键是:

  • (1)更改heightAnchor(或在您的情况下,更改widthAnchor)
  • (2)通过调用超级视图的layoutIfNeeded进行动画处理

..

private var boardIn:[NSLayoutConstraint] = []
private var boardOut:[NSLayoutConstraint] = []
private var tabBoards:[TabBoard] = []

public func createControlBoard(
    _ tag:Int
    )  -> ControlBoard {
    let newBoard = ControlBoard(tag)
    boardOut.append(newBoard.heightAnchor.constraint(equalToConstant: 100))
    boardIn.append(newBoard.heightAnchor.constraint(equalToConstant: 0))
    newBoard.doneButton.addTarget(self, action: #selector(hideTabBoard), for: .touchUpInside)
    tabBoards.append(newBoard)
    return newBoard
}

public func showTabBoard(_ tag:Int) {
    for i in 0...tabBoards.count-1 {
        NSLayoutConstraint.deactivate([boardOut[i]])
        NSLayoutConstraint.activate([boardIn[i]])
        tabBoards[i].makeControlsHidden(true)
    }
    NSLayoutConstraint.deactivate([boardIn[tag-1]])
    NSLayoutConstraint.activate([boardOut[tag-1]])
    tabBoards[tag-1].makeControlsHidden(false)
    UIView.animate(withDuration: 0.3) { self.superview?.layoutIfNeeded() }
}

public func hideTabBoard(_ tag:Int) {
    NSLayoutConstraint.deactivate([boardOut[tag-1]])
    NSLayoutConstraint.activate([boardIn[tag-1]])
    tabBoards[tag-1].makeControlsHidden(true)
    UIView.animate(withDuration: 0.3) { self.superview?.layoutIfNeeded() }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM