TiOSTapGestureRecognizer und TiOSSwipeGestureRecognizer

//
// Programming by Andree Heyroth, Syncro-Concept
// programming language is Firemonkey / Delphi from Embarcadero
// http://www.syncro-concept.de
//
// Please add this unit to an existing package or create a new one.
// After compiling and installing the package the following methods will be available
// through the object inspector:
//
// OnSingleTap
// OnDoubleTap
//
// OnSwipeRight
// OnSwipeLeft
// OnSwipeUp
// OnSwipeDown
//
// The methods will be fired each time when the app-events with the same name are fired
//
// ---------------------------------------------------------------------------
//
unit Swiper;

{$IFDEF FPC}
{$mode delphi}
{$modeswitch objectivec1}
{$ENDIF}

interface

uses
SysUtils, Classes, FMX_Types
{$IFDEF FPC}
, FMX_Platform_iOS, iPhoneAll
{$ENDIF}
;

type
TTapEvent = procedure of object;

TiOSTapGestureRecognizer = class(TFmxObject)
private
{ Private declarations }
FOnSingleTap: TTapEvent;
FOnDoubleTap: TTapEvent;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property OnSingleTap: TTapEvent read FOnSingleTap write FOnSingleTap;
property OnDoubleTap: TTapEvent read FOnDoubleTap write FOnDoubleTap;
end;

type
TSwipeEvent = procedure of object;

TiOSSwipeGestureRecognizer = class(TFmxObject)
private
{ Private declarations }
FOnSwipeRight : TSwipeEvent;
FOnSwipeLeft : TSwipeEvent;
FOnSwipeUp : TSwipeEvent;
FOnSwipeDown : TSwipeEvent;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property OnSwipeRight: TSwipeEvent read FOnSwipeRight write FOnSwipeRight;
property OnSwipeLeft: TSwipeEvent read FOnSwipeLeft write FOnSwipeLeft;
property OnSwipeUp: TSwipeEvent read FOnSwipeUp write FOnSwipeUp;
property OnSwipeDown: TSwipeEvent read FOnSwipeDown write FOnSwipeDown;
end;

var
GlobalSGR : TiOSSwipeGestureRecognizer;
GlobalTGR : TiOSTapGestureRecognizer;

procedure Register;

implementation

{$IFDEF FPC}
type
TSwipeDelegate = objcclass(NSObject)
procedure swipe(gestureRecognizer: UIGestureRecognizer); message 'gestureRecognizer:';
end;

type
TTapDelegate = objcclass(NSObject)
procedure tap(gestureRecognizer: UIGestureRecognizer); message 'gestureRecognizer:';
end;

var
TapDelegate : TTapDelegate;
SwipeDelegate: TSwipeDelegate;
{$ENDIF}

{$IFDEF FPC}
procedure TTapDelegate.Tap(gestureRecognizer: UIGestureRecognizer);
var
TGR : UITapGestureRecognizer;
begin
TGR := UITapGestureRecognizer(gestureRecognizer);
if Assigned(GlobalTGR) then
case TGR.NumberOfTapsRequired of
1:
if Assigned(GlobalTGR.FOnSingleTap) then
GlobalTGR.FOnSingleTap;
2:
if Assigned(GlobalTGR.FOnDoubleTap) then
GlobalTGR.FOnDoubleTap;
end;
end;
{$ENDIF}

constructor TiOSTapGestureRecognizer.Create(AOwner: TComponent);
{$IFDEF FPC}
var
TGR : UITapGestureRecognizer;
window: UIWindow;
{$ENDIF}
begin
if Assigned(GlobalTGR) then
raise Exception.Create('I won''t let you have more than one of these things (TAP) ...');
inherited;

{$IFDEF FPC}
window := UIApplication.sharedApplication.Windows.objectAtIndex(0);
TapDelegate := TTapDelegate.alloc;
// TGR := UITapGestureRecognizer.alloc;
// TGR.initWithTarget_action(TapDelegate,objcselector('gestureRecognizer:'));
// TGR.setNumberOfTapsRequired (1);
// Window.addGestureRecognizer(TGR);
// TGR.release;
TGR := UITapGestureRecognizer.alloc;
TGR.initWithTarget_action(TapDelegate,objcselector('gestureRecognizer:'));
TGR.setNumberOfTapsRequired (2);
Window.addGestureRecognizer(TGR);
TGR.release;
{$ENDIF}

GlobalTGR := Self;
end;

destructor TiOSTapGestureRecognizer.Destroy;
begin
if GlobalTGR = Self then
GlobalTGR := nil;
{$IFDEF FPC}
TapDelegate.release;
{$ENDIF}
inherited;
end;

{$IFDEF FPC}
procedure TSwipeDelegate.swipe(gestureRecognizer: UIGestureRecognizer);
var
SGR : UISwipeGestureRecognizer;
begin
SGR := UISwipeGestureRecognizer(gestureRecognizer);
if Assigned(GlobalSGR) then
case SGR.direction of
UISwipeGestureRecognizerDirectionRight :
if Assigned(GlobalSGR.FOnSwipeRight) then
GlobalSGR.FOnSwipeRight;
UISwipeGestureRecognizerDirectionLeft :
if Assigned(GlobalSGR.FOnSwipeLeft) then
GlobalSGR.FOnSwipeLeft;
UISwipeGestureRecognizerDirectionUp :
if Assigned(GlobalSGR.FOnSwipeUp) then
GlobalSGR.FOnSwipeUp;
UISwipeGestureRecognizerDirectionDown :
if Assigned(GlobalSGR.FOnSwipeDown) then
GlobalSGR.FOnSwipeDown;
end;
end;
{$ENDIF}

constructor TiOSSwipeGestureRecognizer.Create(AOwner: TComponent);
{$IFDEF FPC}
var
SGR : UISwipeGestureRecognizer;
window: UIWindow;
{$ENDIF}
begin
if Assigned(GlobalSGR) then
raise Exception.Create('I won''t let you have more than one of these things (SWIPE) ...');
inherited;

{$IFDEF FPC}
window := UIApplication.sharedApplication.Windows.objectAtIndex(0);
SwipeDelegate := TSwipeDelegate.alloc;
SGR := UISwipeGestureRecognizer.alloc;
SGR.initWithTarget_action(SwipeDelegate,objcselector('gestureRecognizer:'));
SGR.setDirection(UISwipeGestureRecognizerDirectionRight);
Window.addGestureRecognizer(SGR);
SGR.release;
SGR := UISwipeGestureRecognizer.alloc;
SGR.initWithTarget_action(SwipeDelegate,objcselector('gestureRecognizer:'));
SGR.setDirection(UISwipeGestureRecognizerDirectionLeft);
Window.addGestureRecognizer(SGR);
SGR.release;
SGR := UISwipeGestureRecognizer.alloc;
SGR.initWithTarget_action(SwipeDelegate,objcselector('gestureRecognizer:'));
SGR.setDirection(UISwipeGestureRecognizerDirectionUp);
Window.addGestureRecognizer(SGR);
SGR.release;
SGR := UISwipeGestureRecognizer.alloc;
SGR.initWithTarget_action(SwipeDelegate,objcselector('gestureRecognizer:'));
SGR.setDirection(UISwipeGestureRecognizerDirectionDown);
Window.addGestureRecognizer(SGR);
SGR.release;
{$ENDIF}

GlobalSGR := Self;
end;

destructor TiOSSwipeGestureRecognizer.Destroy;
begin
if GlobalSGR = Self then
GlobalSGR := nil;
{$IFDEF FPC}
SwipeDelegate.release;
{$ENDIF}
inherited;
end;

procedure Register;
begin
RegisterComponents('iOS', [TiOSSwipeGestureRecognizer, TiOSTapGestureRecognizer]);
end;

end.