我正在做一个项目,我不能使用xamarin.forms或故事板。我想滑动一个UIView,让它逐渐消失(在我滑动之后,如果你知道我想说什么的话)。我可以刷卡,我用的是SwipeGesture,但是我怎么才能让刷卡变得生动起来呢?在我刷卡的时候慢慢褪色吗?
代码:
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();
}谢谢
发布于 2019-12-20 04:01:29
我不确定你所说的“消失”是指淡出(不透明/Alpha)还是你想让视图改变宽度直到它消失。
滑动手势识别器只有在你做一个滑动手势时才能感知,它不能“跟随”你的触摸。为此,您需要子类UIView并实现touches方法,例如BeginTouches等。下面的UIView子类将观察触摸(仅限单点触摸,默认情况下视图上不启用多点触摸),并在触摸移动时调整视图的宽度:
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:
SwipeView testeS = new SwipeView(new CGRect(0, 0, 5, UIScreen.MainScreen.Bounds.Height));
testeS.BackgroundColor = UIColor.Red;
Add(testeS);https://stackoverflow.com/questions/59413400
复制相似问题