首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >逐渐刷UIView XAMARIN IOS

逐渐刷UIView XAMARIN IOS
EN

Stack Overflow用户
提问于 2019-12-20 00:15:43
回答 1查看 113关注 0票数 0

我正在做一个项目,我不能使用xamarin.forms或故事板。我想滑动一个UIView,让它逐渐消失(在我滑动之后,如果你知道我想说什么的话)。我可以刷卡,我用的是SwipeGesture,但是我怎么才能让刷卡变得生动起来呢?在我刷卡的时候慢慢褪色吗?

代码:

代码语言:javascript
复制
      UIView testeS = new UIView(new CGRect(0, 0, 5, UIScreen.MainScreen.Bounds.Height));
            testeS.BackgroundColor = UIColor.Clear;
            Add(testeS);
            UISwipeGestureRecognizer swipeRight = new UISwipeGestureRecognizer(OnSwipeRight);
            swipeRight.Direction = UISwipeGestureRecognizerDirection.Right;
            testeS.AddGestureRecognizer(swipeRight);


            UISwipeGestureRecognizer swipeLeft = new UISwipeGestureRecognizer(OnSwipeLeft);
            swipeLeft.Direction = UISwipeGestureRecog



        private void OnSwipeRight()
        {
            Add(tab);
        }

        private void OnSwipeLeft()
        {
            tab.RemoveFromSuperview();
        }

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-20 04:01:29

我不确定你所说的“消失”是指淡出(不透明/Alpha)还是你想让视图改变宽度直到它消失。

滑动手势识别器只有在你做一个滑动手势时才能感知,它不能“跟随”你的触摸。为此,您需要子类UIView并实现touches方法,例如BeginTouches等。下面的UIView子类将观察触摸(仅限单点触摸,默认情况下视图上不启用多点触摸),并在触摸移动时调整视图的宽度:

代码语言:javascript
复制
public class SwipeView : UIView
{
    nfloat initialTouchXLocation;
    CGRect initialFrame;

    public SwipeView(CGRect rect) : base(rect)
    {
        initialFrame = rect;
    }

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

        initialTouchXLocation = touches.ToArray<UITouch>()[0].LocationInView(this).X;
    }

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

        nfloat newTouchXLocation = touches.ToArray<UITouch>()[0].LocationInView(this).X;

        nfloat newWidth = (newTouchXLocation - initialTouchXLocation) + initialFrame.Width;
        if (newWidth < 5)
            newWidth = 5; // keeps the view from getting too narrow to touch
        Frame = new CGRect(initialFrame.X, initialFrame.Y, newWidth, initialFrame.Height);
    }

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

        initialFrame = this.Frame;
    }

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

        initialFrame = this.Frame;
    }
}

然后只需添加此视图,而不是常用的UIView:

代码语言:javascript
复制
SwipeView testeS = new SwipeView(new CGRect(0, 0, 5, UIScreen.MainScreen.Bounds.Height));
testeS.BackgroundColor = UIColor.Red;
Add(testeS);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59413400

复制
相关文章

相似问题

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