卡片界面 可以用于比简单地制作酷炫的约会应用程序多得多的用途。让我们学习如何通过创建食物匹配器来构建它们。想象一下,如果一个孩子可以通过在西兰花上向左滑动并在 PBJ 上向右滑动来在早上从爸爸妈妈那里预订午餐。或者,在这种情况下,通过选择晚餐后你想要的确切甜点,这始终是一个关键的选择时刻。
首先,我使用 Angular 和 NativeScript 搭建了一个基本的应用程序。由于这是一个甜点匹配应用程序,所以在我的服务中,我创建了一个基本的 emoji 数组。每个 emoji 都放置在彩色卡片上。import { Injectable } from
"@angular/core"
;
import { Emoji } from
"./emoji"
;
@Injectable()
export class CardService {
private emoji =
new
Array<Emoji>(
{ code:
'🍮'
, color:
'b1'
},
{ code:
'🍡'
, color:
'b2'
},
{ code:
'🍨'
, color:
'b3'
},
{ code:
'🍩'
, color:
'b4'
},
{ code:
'🍪'
, color:
'b5'
},
{ code:
'🍰'
, color:
'b5'
},
{ code:
'🍬'
, color:
'b1'
},
{ code:
'🍭'
, color:
'b2'
},
{ code:
'🎂'
, color:
'b3'
},
{ code:
'🍧'
, color:
'b4'
},
{ code:
'🍫'
, color:
'b5'
},
{ code:
'🍦'
, color:
'b6'
}
);
getEmoji(): Emoji[] {
return
this
.emoji;
}
}
.b
1
{
background-color
:
#FFCCDA
;
}
.b
2
{
background-color
:
#FAEEC3
;
}
.b
3
{
background-color
:
#F67982
;
}
.b
4
{
background-color
:
#FFA1A1
;
}
.b
5
{
background-color
:
#FACBAA
;
}
.b
6
{
background-color
:
#F67982
;
}
<
StackLayout
>
<
AbsoluteLayout
horizontalAlignment
=
"center"
paddingTop
=
"30"
#absolutelayout>
<
GridLayout
width
=
"100%"
style
=
"z-index:1"
columns
=
"*,*"
horizontalAlignment
=
"center"
>
<
Label
#no
col
=
"0"
verticalAlignment
=
"center"
text
=
"不,谢谢!"
class
=
"no"
></
Label
>
<
Label
#yes
col
=
"1"
text
=
"是的,请!"
class
=
"yes"
></
Label
>
</
GridLayout
>
</
AbsoluteLayout
>
</
StackLayout
>
@ViewChild(
"absolutelayout"
) al: ElementRef;
@ViewChild(
"yes"
) yes: ElementRef;
@ViewChild(
"no"
) no: ElementRef;
...
ngOnInit() {
this
.emoji =
this
.cardService.getEmoji();
// 初始化卡片
this
.code =
this
.emoji[
this
.i].code;
// 为滑动做好准备!
for
(
var
key
in
this
.emoji) {
this
.handleSwipe(key);
}
}
handleSwipe(key: any) {
this
.i--;
let grid =
new
GridLayout();
let emoji =
new
Label();
let yes = <Label>
this
.yes.nativeElement;
let no = <Label>
this
.no.nativeElement;
let absolutelayout = <AbsoluteLayout>
this
.al.nativeElement;
let swipeleft = <Button>
this
.swipeleft.nativeElement;
let swiperight = <Button>
this
.swiperight.nativeElement;
// 在卡片上设置表情
emoji.text =
this
.emoji[key].code;
// 构建网格,即卡片
grid.cssClass =
'card '
+
this
.emoji[key].color;
grid.id =
'card'
+ Number(key);
grid.marginTop =
this
.i;
// 将表情添加到网格,并将网格添加到 AbsoluteLayout
grid.addChild(emoji);
absolutelayout.addChild(grid)
...
}
grid.on(GestureTypes.swipe,
function
(args: SwipeGestureEventData) {
if
(args.direction == 1) {
// 向右
yes.animate({ opacity: 0, duration: 100 })
.then(() => yes.animate({ opacity: 1, duration: 100 }))
.then(() => yes.animate({ opacity: 0, duration: 100 }))
.then(() =>
grid.animate({ translate: { x: 1000, y: 100 } })
.then(
function
() {
return
grid.animate({ translate: { x: 0, y: -2000 } }); })
.
catch
(
function
(e) {
console.log(e.message);
})
)
.
catch
((e) => {
console.log(e.message);
});
}
else
{
// 向左
no.animate({ opacity: 0, duration: 100 })
.then(() => no.animate({ opacity: 1, duration: 100 }))
.then(() => no.animate({ opacity: 0, duration: 100 }))
.then(() =>
grid.animate({ translate: { x: -1000, y: 100 } })
.then(
function
() {
return
grid.animate({ translate: { x: 0, y: -2000 } }); })
.
catch
(
function
(e) {
console.log(e.message);
})
)
.
catch
((e) => {
console.log(e.message);
});
}
});