基本触摸动作的使用
文章信息
代码示例
文章
关键词: CCoeControl::HandlePointerEventL(), TPointerEvent::EButton1Up, TPointerEvent::EButton1Down, TPointerEvent::EDrag
翻译:
由 hoolee
最后由 hamishwillee
在 22 Dec 2011 编辑
Overview
S60第五版平台支持触摸屏,因此提供了更强大的应用程序界面特性。 如果一个界面过分复杂繁琐,有很多弹出菜单和工具栏按钮,那么对用户来说就十分讨厌和不便。甚至只是简单选择一个菜单或按一个工具栏按钮,都得十分小心的看着程序。
触摸界面可以帮助减少程序操作所需动作,例如将程序主要功能映射到触摸动作上。
基本的触摸动作包括如下:
上下/下上,左右/右左,右上到左下/左下到右上,以及右下到左上/左上到右下。当程序的主要功能都映射到触摸动作之后,用户就不需要盯着程序界面了。例如,当在媒体播放器中左右滑动就会切换到下一首,右左移动就会切换到上一首。
为了简化手势动作的类型,我们将基本的手势假设为他们的开始和结束位置。这样知道了他们的相对位置就可以推算手势类型了。此外,使用触摸手指也可以丰富用户体验(这样将菜单和按钮解放出来做其他事情)
八个基本的触摸手势
四个用来识别触摸类型的基本区域
前提
下列代码示例可以在触摸手机上运行,如果要检查是否支持笔触,可以调用knLayoutUtils::PenEnabled()方法。为了简化问题,这个程序假设手机可以支持触摸界面。
MMP文件
LIBRARY avkon.lib euser.lib
CAPABILITY could be self-signed
头文件
// Eight basic touch gestures
enum TGestureType
{
ENoneGesture = -1,
EUpDown,
EDownUp,
ERightLeft,
ELeftRight,
ELeftUpRightDown,
ELeftDownRightUp,
ERightUpLeftDown,
ERightDownLeftUp
};
// Four basic types of positions.
// The control rectangle can be devided into four main areas, for example A, B, C, and D .
enum TPositionType
{
ENonePosition = -1,
EAreaA,
EAreaB,
EAreaC,
EAreaD
};
/**
* Container class
*/
class CBasicGesturesExContainer : public CCoeControl
{
...
private:
/*
* From CCoeControl, HandleResourceChange
* This function gets called whenever a pointer event occurs.
* @param aEvent The pointer event.
*/
void HandlePointerEventL( const TPointerEvent& aEvent );
private:
/*
* Identifies the gesture type.
* @param aStartPoint The start point coordinates.
* @param aEndPoint The end point coordinates.
*/
TGestureType GetGestureType(const TPoint& aStartPoint, const TPoint& aEndPoint);
/*
* Handles the specified gesture.
* @param aGesture The gesture type.
*/
void HandleGesture(const TGestureType& aGesture);
private: // data
/*
* Stores the gesture start point coordinate
*/
TPoint iStartPoint;
/*
* Indicates whether there is any gesture.
* EFalse by default.
*/
TBool iGesture;
...
};
源文件
...
void CBasicGesturesExContainer::ConstructL(const TRect& aRect)
{
...
// Enables handling of drag events
EnableDragEvents();
...
}
void CBasicGesturesExContainer::HandlePointerEventL( const TPointerEvent& aEvent )
{
switch (aEvent.iType)
{
case TPointerEvent::EButton1Down:
// Save tap position (because it could be the beginning of the gesture)
iStartPoint = aEvent.iPosition;
break;
case TPointerEvent::EButton1Up:
if (iGesture)
{
// Idenditify type of gesture
TGestureType gesture = GetGestureType(iStartPoint, aEvent.iPosition);
// Perform necessary action depends on gesture type
HandleGesture(gesture);
}
// Reset gesture indicator
iGesture = EFalse;
break;
case TPointerEvent::EDrag:
iGesture = ETrue;
break;
default:
break;
}
}
TGestureType CBasicGesturesExContainer::GetGestureType(const TPoint& aStartPoint, const TPoint& aEndPoint)
{
// Default return value
TGestureType returnValue = EUpDown;
// Control's extent
TRect rect(Rect());
TInt rectHalfWidth = rect.Width()/2;
TInt rectHalfHeight = rect.Height()/2;
// Start point position type
TInt startPointLocation = EAreaA;
// End point position type
TInt endPointLocation = EAreaA;
// Determine in which logical part of the control is the start point
if (aStartPoint.iX <= rectHalfWidth)
{
if (aStartPoint.iY <= rectHalfHeight)
{
startPointLocation = EAreaA;
}
else if (aStartPoint.iY > rectHalfHeight)
{
startPointLocation = EAreaD;
}
}
else if (aStartPoint.iX > rectHalfWidth)
{
if (aStartPoint.iY <= rectHalfHeight)
{
startPointLocation = EAreaB;
}
else if (aStartPoint.iY > rectHalfHeight)
{
startPointLocation = EAreaC;
}
}
// Determine in which logical part of the control is the end point
if (aEndPoint.iX <= rectHalfWidth)
{
if (aEndPoint.iY <= rectHalfHeight)
{
endPointLocation = EAreaA;
}
else if (aEndPoint.iY > rectHalfHeight)
{
endPointLocation = EAreaD;
}
}
else if (aEndPoint.iX > rectHalfWidth)
{
if (aEndPoint.iY <= rectHalfHeight)
{
endPointLocation = EAreaB;
}
else if (aEndPoint.iY > rectHalfHeight)
{
endPointLocation = EAreaC;
}
}
// Get the actual type of the gesture
switch (startPointLocation)
{
case EAreaA:
switch (endPointLocation)
{
case EAreaA:
returnValue = ENoneGesture;
break;
case EAreaB:
returnValue = ELeftRight;
break;
case EAreaC:
returnValue = ELeftUpRightDown;
break;
case EAreaD:
returnValue = EUpDown;
break;
default:
returnValue = ENoneGesture;
break;
}
break;
case EAreaB:
switch (endPointLocation)
{
case EAreaA:
returnValue = ERightLeft;
break;
case EAreaB:
returnValue = ENoneGesture;
break;
case EAreaC:
returnValue = EUpDown;
break;
case EAreaD:
returnValue = ERightUpLeftDown;
break;
default:
returnValue = ENoneGesture;
break;
}
break;
case EAreaC:
switch (endPointLocation)
{
case EAreaA:
returnValue = ERightDownLeftUp;
break;
case EAreaB:
returnValue = EDownUp;
break;
case EAreaC:
returnValue = ENoneGesture;
break;
case EAreaD:
returnValue = ERightLeft;
break;
default:
returnValue = ENoneGesture;
break;
}
break;
case EAreaD:
switch (endPointLocation)
{
case EAreaA:
returnValue = EDownUp;
break;
case EAreaB:
returnValue = ELeftDownRightUp;
break;
case EAreaC:
returnValue = ELeftRight;
break;
case EAreaD:
returnValue = ENoneGesture;
break;
default:
returnValue = ENoneGesture;
break;
}
break;
default:
returnValue = ENoneGesture;
break;
}
return returnValue;
}
void CBasicGesturesExContainer::HandleGesture(const TGestureType& aGesture)
{
TBuf<64> messageText(KNullDesC);
// Handle gesture
switch(aGesture)
{
case EUpDown:
messageText = _L("Gesture: from Up to Down");
break;
case EDownUp:
messageText = _L("Gesture: from Down to Up");
break;
case ERightLeft:
messageText = _L("Gesture: from Right to Left");
break;
case ELeftRight:
messageText = _L("Gesture: from Left to Right");
break;
case ELeftUpRightDown:
messageText = _L("Gesture: from Left-Up to Right-Down");
break;
case ELeftDownRightUp:
messageText = _L("Gesture: from Left-Down to Right-Up");
break;
case ERightUpLeftDown:
messageText = _L("Gesture: from Right-Up to Left-Down");
break;
case ERightDownLeftUp:
messageText = _L("Gesture: from Right-Down to Left-Up");
break;
default:
// Gesture type's not identified,
// do nothing
return;
break;
}
CAknInformationNote* note = new ( ELeave ) CAknInformationNote;
// Show the information note with a previously defined text
note->ExecuteLD(messageText);
}
...
限制
触摸手势很容易识别,只有8个基本类型可以判断
后记
基本的手势动作都能映射到所需的程序功能上
相关资料




(no comments yet)