質問編集履歴

2

誤字

2015/12/01 07:19

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,50 +1,6 @@
1
1
  WPFをつかってiPhoneやAndroidのようなスクロールを実現したいです。
2
2
 
3
3
  タップで加速度がついて、次第に減速して止まるようなやつです。
4
-
5
- 現在はxamlにScrollViewerを設置したところです。
6
-
7
-
8
-
9
- これでスクロールバーは消せましたが、タッチパネルでタッチしても、写真は微動だにしないです。
10
-
11
- マウスのホイールを使うと、加速度や減速はないのですが、スクロールはできます。
12
-
13
- どのようにしたらよいか、ご示唆ください。
14
-
15
- よろしくお願いします。
16
-
17
-
18
-
19
- <Window x:Class="swipe.MainWindow"
20
-
21
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
-
23
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
24
-
25
- Title="MainWindow" Height="350" Width="525">
26
-
27
-
28
-
29
- <ScrollViewer Height="200"
30
-
31
- Margin="5"
32
-
33
- HorizontalScrollBarVisibility="Hidden"
34
-
35
- VerticalScrollBarVisibility="Hidden"
36
-
37
- PanningMode="Both">
38
-
39
- <Image Source="C:\01.jpg" Stretch="None" />
40
-
41
- </ScrollViewer>
42
-
43
- </Grid>
44
-
45
- </Window>
46
-
47
-
48
4
 
49
5
 
50
6
 

1

コード例を追加。

2015/12/01 07:19

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -43,3 +43,201 @@
43
43
  </Grid>
44
44
 
45
45
  </Window>
46
+
47
+
48
+
49
+
50
+
51
+ いま、こんな感じで、オブジェクトを移動はできるようになりました。
52
+
53
+ が、開始時のstartpointと、終了時のendpointに同じ値が入ってしまって、距離をとれずにいます。
54
+
55
+ アドバイスお願いします。
56
+
57
+
58
+
59
+ xmal
60
+
61
+
62
+
63
+ <Window x:Class="swipe.MainWindow"
64
+
65
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
66
+
67
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
68
+
69
+ Title="MainWindow" Height="450" Width="525">
70
+
71
+
72
+
73
+ <Canvas Name="canvas">
74
+
75
+ <Image Name ="image" Source="C:\01.jpg" Stretch="None"
76
+
77
+ MouseLeftButtonDown="image_MouseLeftButtonDown"
78
+
79
+ MouseLeftButtonUp="image_MouseLeftButtonUp"
80
+
81
+ MouseMove="image_MouseMove"
82
+
83
+
84
+
85
+ />
86
+
87
+ </Canvas>
88
+
89
+ </Window>
90
+
91
+
92
+
93
+ C#
94
+
95
+
96
+
97
+ using System;
98
+
99
+ using System.Windows;
100
+
101
+ using System.Windows.Controls;
102
+
103
+ using System.Windows.Input;
104
+
105
+
106
+
107
+ namespace swipe {
108
+
109
+ public partial class MainWindow : Window {
110
+
111
+ public MainWindow() {
112
+
113
+ InitializeComponent();
114
+
115
+ }
116
+
117
+ private bool isDrag = false;
118
+
119
+ private Point dragOffset;
120
+
121
+ private Point startpoint;
122
+
123
+ private Point endpoint;
124
+
125
+ //加速度を処理するときは移動量の他に移動時間も取る。
126
+
127
+ TimeSpan 移動時間;
128
+
129
+ DateTime 移動開始時刻;
130
+
131
+ DateTime 移動終了時刻;
132
+
133
+ double 速度 = 0;
134
+
135
+ double 距離 = 0;
136
+
137
+ //慣性移動量の計算をする。
138
+
139
+
140
+
141
+ /// <summary>
142
+
143
+ /// ドラッグ開始
144
+
145
+ /// </summary>
146
+
147
+ /// <param name="sender"></param>
148
+
149
+ /// <param name="e"></param>
150
+
151
+ private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
152
+
153
+ 移動開始時刻 = DateTime.Now;
154
+
155
+ UIElement element = sender as UIElement;
156
+
157
+ if (element != null) {
158
+
159
+ isDrag = true;
160
+
161
+ startpoint = dragOffset = e.GetPosition(element);
162
+
163
+ element.CaptureMouse();
164
+
165
+ }
166
+
167
+ }
168
+
169
+
170
+
171
+ /// <summary>
172
+
173
+ /// ドラッグ終了
174
+
175
+ /// </summary>
176
+
177
+ /// <param name="sender"></param>
178
+
179
+ /// <param name="e"></param>
180
+
181
+ private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
182
+
183
+ if (isDrag) {
184
+
185
+ UIElement element = sender as UIElement;
186
+
187
+ element.ReleaseMouseCapture();
188
+
189
+ endpoint = e.GetPosition(element);
190
+
191
+ 移動終了時刻 = DateTime.Now;
192
+
193
+ 移動時間 = 移動終了時刻 - 移動開始時刻;
194
+
195
+ double 移動時間の小数点以下 = double.Parse(移動時間.ToString("fffffff"));
196
+
197
+ double X = startpoint.X - endpoint.X;//ここらへん。
198
+
199
+ double Y = startpoint.Y - endpoint.Y;
200
+
201
+ double 縦横2乗 = Math.Pow(X, 2) + Math.Pow(Y, 2);
202
+
203
+ 距離 = Math.Sqrt(縦横2乗);
204
+
205
+ Title = "X=" + X + " Y=" + Y + " " +縦横2乗+ " " + 距離 + "/" + 移動時間の小数点以下 + "(pixcel/10000000second)";
206
+
207
+ isDrag = false;
208
+
209
+ }
210
+
211
+ }
212
+
213
+
214
+
215
+ /// <summary>
216
+
217
+ /// ドラック中
218
+
219
+ /// </summary>
220
+
221
+ /// <param name="sender"></param>
222
+
223
+ /// <param name="e"></param>
224
+
225
+ private void image_MouseMove(object sender, MouseEventArgs e) {
226
+
227
+ if (isDrag) {
228
+
229
+ Point point = Mouse.GetPosition(canvas);
230
+
231
+ UIElement element = sender as UIElement;
232
+
233
+ Canvas.SetLeft(element, point.X - dragOffset.X);
234
+
235
+ Canvas.SetTop(element, point.Y - dragOffset.Y);
236
+
237
+ }
238
+
239
+ }
240
+
241
+ }
242
+
243
+ }