JJChat 一个类似安卓QQ的拖动删除消息控件

xx****st UID.1130055
2016-08-10 发表

本帖最后由 xxlost 于 2016-8-10 17:39 编辑

Hello大家好!最近在做JJChat时需要实现一个类似QQ那样的拖动删除功能,无奈我没有找到这样的现成demo。那就自己动手做一个吧;
根据效果来看应该是俩圆加上一个闭合的贝塞尔曲线,OK 但是,Xmal里圆时不能放textblock的,那就只有用border咯。
By the way:今天突然想到控件的扩展属性在后台怎么访问;比如Canvas.Top;是这样的:Canvas.SetTop(Element,Value)
好了,废话就不多说了,直接上代码:
Xmal:
<UserControl
x:Class=“App1.MyUserControl1“
xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation“
xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml“
xmlns:local=“using:App1“
xmlns:d=“http://schemas.microsoft.com/expression/blend/2008“
xmlns:mc=“http://schemas.openxmlformats.org/markup-compatibility/2006“
mc:Ignorable=“d“
d:DesignHeight=“300“
d:DesignWidth=“400“>
<Grid>
<Canvas Name=“grid“ Width=“{x:Bind Radius}“ Height=“{x:Bind Radius}“>
<Border x:Name=“down“ Width=“{x:Bind Radius}“ Height=“{x:Bind Radius}“ Background=“Red“ VerticalAlignment=“Top“ HorizontalAlignment=“Left“/>
<Path x:Name=“path“ StrokeThickness=“1“ Fill=“Red“ VerticalAlignment=“Top“ HorizontalAlignment=“Left“ >
<Path.Data>
<GeometryGroup FillRule=“EvenOdd“>

<PathGeometry >
<PathGeometry.Figures>
<PathFigureCollection>

<PathFigure x:Name=“start1“ >
<PathFigure.Segments>
<PathSegmentCollection>
<BezierSegment x:Name=“ber1“ />
<LineSegment Point=“{Binding ElementName=ber2,Path=Point3,Mode=OneWay}“/>
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
<PathFigure x:Name=“start2“ >
<PathFigure.Segments>
<PathSegmentCollection>
<BezierSegment x:Name=“ber2“/>
<LineSegment Point=“{Binding ElementName=ber1,Path=Point3,Mode=OneWay}“/>

</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>

</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</GeometryGroup>
</Path.Data>
</Path>

<Border x:Name=“top“ Width=“{x:Bind Radius}“ Height=“{x:Bind Radius}“ ManipulationMode=“All“ ManipulationDelta=“top_ManipulationDelta“ VerticalAlignment=“Top“ HorizontalAlignment=“Left“ Background=“Red“ RenderTransformOrigin=“0.5,0.5“ ManipulationCompleted=“top_ManipulationCompleted“>

<TextBlock x:Name=“MsgCount“ Text=“1“ Foreground=“White“ VerticalAlignment=“Center“ HorizontalAlignment=“Center“/>
</Border>

</Canvas>
</Grid>
</UserControl>

CS:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace App1
{
public sealed partial class MyUserControl1 : UserControl
{
TranslateTransform translate;
CompositeTransform scale;
TranslateTransform pathtrans;
double center;
double r ;
private double distans;
/// <summary>
/// 已拖动距离
/// </summary>
public double Distans { get { return distans; } }
/// <summary>
/// 消除距离
/// </summary>
public double MaxDistans { set { r = value; } }
/// <summary>
/// 颜色
/// </summary>
public new Brush Background
{
get { return top.Background; }
set
{
top.Background = down.Background = path.Fill = value;
}
}
/// <summary>
/// 字体颜色
/// </summary>
public new Brush Foreground
{
get { return MsgCount.Foreground; }
set { MsgCount.Foreground = value; }
}
/// <summary>
/// 文字
/// </summary>
public string Text
{
get { return MsgCount.Text; }
set { MsgCount.Text = value; }
}
/// <summary>
/// 大小: 直径
/// </summary>
public double Radius
{
get { return top.Width; }
set
{
top.Width = top.Height = down.Width = down.Height = grid.Width = grid.Height = value;

}
}
/// <summary>
/// 圆角
/// </summary>
public CornerRadius CornerRadius
{
get { return top.CornerRadius; }
set { top.CornerRadius = down.CornerRadius = value; }
}

public MyUserControl1()
{
this.InitializeComponent();
translate = new TranslateTransform();
scale = new CompositeTransform();
pathtrans = new TranslateTransform();
path.RenderTransform = pathtrans;

top.RenderTransform = translate;
down.RenderTransform = scale;

}
private void top_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
center = Radius /2;
scale.CenterX = scale.CenterY = Radius / 2;
if (!e.IsInertial)
{
translate.X += e.Delta.Translation.X;
translate.Y += e.Delta.Translation.Y;

distans = Math.Sqrt(translate.X * translate.X + translate.Y * translate.Y);

if (distans < r)
{
scale.ScaleX = scale.ScaleY = (r - distans) / r;

TransformPath();
}
else
{
path.Fill = null;
down.Background = null;
}

}

}
async private void TransformPath()
{
distans += Math.Sqrt((1 - scale.ScaleY) * down.ActualHeight * (1 - scale.ScaleY) * down.ActualHeight * 2);

double cos = translate.Y / distans;
double sin = translate.X / distans;
double rdown = Radius / 2 * scale.ScaleY;
double rtop = Radius / 2;
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
{

start1.StartPoint = new Point(center - rdown * cos, center + rdown * sin);
ber1.Point3 = new Point(center + translate.X - (rtop) * cos, center + translate.Y + (rtop) * sin);
ber1.Point1 = new Point(translate.X * 1 / 3 + center - (rdown / 2) * cos, translate.Y * 1 / 3 + center + rdown / 2 * sin);
ber1.Point2 = new Point(translate.X * 2 / 3 + center - (rdown / 2) * cos, translate.Y * 2 / 3 + center + rdown / 2 * sin);

start2.StartPoint = new Point(center + translate.X + (rtop) * cos, center + translate.Y - (rtop) * sin);//

ber2.Point3 = new Point(center + rdown * cos, center - rdown * sin);

ber2.Point1 = new Point(translate.X * 2 / 3 + center + rdown / 2 * cos, translate.Y * 2 / 3 + center - rdown / 2 * sin);

ber2.Point2 = new Point(translate.X * 1 / 3 + center + rdown / 2 * cos, translate.Y * 1 / 3 + center - rdown / 2 * sin);
path.Fill = top.Background;
down.Background = top.Background;

});
}
private void top_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
translate.X = translate.Y = 0;
scale.ScaleX = scale.ScaleY = (r - distans) / r;
TransformPath();

}
}
}
做成一个Usercontorl,这样就可以在其他地方直接用啦!当然如果你还有更好的实现方法,请告诉我!
这里没有做动画,如果需要的话可以加上动画哦
<local:MyUserControl1 />
今天吹牛到此结束!
***图片停止解析***

敬告:
为防止不可控的内容风险,本站已关闭新用户注册,新贴的发表及评论;
你现在看到的内容只是互联网用户曾经发表的言论快照,仅用于老用户留存纪念,且仅与科技行业相关,全部内容不代表本站观点及立场;
本站重新开放前已针对包括用户隐私、版权保护、信息安全、国家政策在内的各种互联网法律法规要求,执行了隐患内容的自查、屏蔽和删除;
本站目前所属个人主体,未有任何盈利安排与计划,且与原WFUN.COM所属公司不存在任何关联关系;
如果本帖内容或者相关资源侵犯到您的合法权益,或者您认为存在问题,那么请您务必点此举报或投诉!
全部回复:
joe0902 UID.2838358
2016-08-10 使用 Lumia 950 XL 回复

看不懂的飘过,大神继续加油!

枝炙智 UID.1339091
2016-08-10 使用 Lumia 535 回复

@wpqq开发人员

俺****了 UID.2846918
2016-08-10 使用 Lumia 640 XL 回复

虽然看不懂,但必须赞一个,加油

ch****05 UID.900407
2016-08-10 使用 Lumia 930 回复

我还以为是老司机发车,原来是大神在飙车

木易巅峰 UID.1253182
2016-08-10 使用 Lumia 640 回复

不懂帮顶

seman638 UID.1186988
2016-08-10 使用 Lumia 640 XL 回复

支持你!

爱无厘头周生 UID.1350340
2016-08-10 使用 Lumia 830 回复

第一次见老司机大神飙车!

FirstRD UID.1114700
2016-08-10 使用 Lumia 640 回复

膜拜大神

wifi王道 UID.553
2016-08-12 回复

这个有点意思!

索****星 UID.2852233
2016-08-14 使用 Lumia 650 回复

拖动删除的话左右滑动界面怎么办

命苦****00 UID.335824
2016-08-14 回复

Quote索拉里斯星 发表于 2016-8-14 12:42
拖动删除的话左右滑动界面怎么办


这个可以判断焦点啊

fivefish2016 UID.2856140
2017-11-05 使用 Lumia 950 XL 回复

不发出来给大家用?

tmp00000 UID.995403
2017-11-07 回复

SwipeControl 被你忽视了 https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.swipecontrol

artfly08 UID.2900999
2018-06-26 使用 Lumia 950 XL 回复

真复杂,
支持开发者

artfly08 UID.2900999
2018-07-14 使用 Lumia 950 XL 回复

支持

本站使用Golang构建,点击此处申请开源鄂ICP备18029942号-4联系站长投诉/举报