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

时间:2016-11-10 10:06:22

标签: ios swift xamarin

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

我的幻灯片菜单:

enter image description here

我的代码:

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) =>
       {

       };
    }
}

1 个答案:

答案 0 :(得分:0)

我使用AutoLayout和UIView动画来上下移动控件视图。 (想想键盘,但更灵活。)电路板有一个“完成”按钮,动画地将电路板(及其子视图)移出视野。

由于我有一系列电路板,因此这里有额外的代码来处理tag属性。这就是我找到合适的电路板的地方。

关键是:

  • (1)更改heightAnchor(或在您的情况下为widthAnchor)
  • (2)通过调用superview的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() }
}