How do I access the accelerometer in iOS using Delphi XE2?
I tried looking through the IDE but did not find a component.
How do I access the accelerometer in iOS using Delphi XE2?
I tried looking through the IDE but did not find a component.
约瑟夫是正确的。Philip Hess 一直为 Delphi iOS 新闻组和其他有用信息做出贡献,例如:
https://forums.embarcadero.com/message.jspa?messageID=390713&tstart=125
他也有这个页面,在这里:
http://web.me.com/macpgmr/ObjP/Xcode4/ObjP_Intro.html
这可能对您有帮助。为了使用 CoreLocation 服务(以及其他服务),我目前正在涉足这一切。这绝对不适合胆小的人。
Embarcadero 博客上有一个读取加速度计的组件:http:
//edn.embarcadero.com/article/41748
这是该组件的下载链接:http: //cc.embarcadero.com/item/28609
这是 Anders Ohlsson 的源代码:(
我用它来构建 GPS/指南针的组件)。
unit Accelerometer;
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$ENDIF}
interface
uses
SysUtils, Classes, FMX_Types
{$IFDEF FPC}
, iPhoneAll
{$ENDIF}
;
type
TAccelerateEvent = procedure(x,y : Double) of object;
TiOSAccelerometer = class(TFmxObject)
private
FOnAccelerate: TAccelerateEvent;
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
property OnAccelerate: TAccelerateEvent read FOnAccelerate write FOnAccelerate;
end;
{$IFDEF FPC}
const
kAccelerometerFrequency = 100.0; // Hz
{$ENDIF}
{$IFDEF FPC}
type
UIAcceleration = objcclass external (NSObject)
public
function timestamp: NSTimeInterval; message 'timestamp';
function x: UIAccelerationValue; message 'x';
function y: UIAccelerationValue; message 'y';
function z: UIAccelerationValue; message 'z';
end;
{$ENDIF}
{$IFDEF FPC}
type
AccDelegate = objcclass(NSObject)
procedure accelerometer_didAccelerate(accelerometer: UIAccelerometer; acceleration: UIAcceleration); message 'accelerometer:didAccelerate:';
end;
{$ENDIF}
{$IFDEF FPC}
var
AccDelegatVar: AccDelegate;
{$ENDIF}
var
MyAccelerometer: TiOSAccelerometer;
procedure Register;
implementation
{$IFDEF FPC}
procedure AccDelegate.accelerometer_didAccelerate(accelerometer: UIAccelerometer; acceleration: UIAcceleration);
begin
if Assigned(MyAccelerometer) then
if Assigned(MyAccelerometer.FOnAccelerate) then
MyAccelerometer.FOnAccelerate(acceleration.x,acceleration.y);
end;
{$ENDIF}
constructor TiOSAccelerometer.Create(AOwner: TComponent);
begin
inherited;
{$IFDEF FPC}
UIAccelerometer.sharedAccelerometer.setUpdateInterval(1.0 / kAccelerometerFrequency);
AccDelegatVar := AccDelegate.alloc;
UIAccelerometer.sharedAccelerometer.setDelegate(AccDelegatVar);
{$ENDIF}
end;
procedure Register;
begin
RegisterComponents('iOS', [TiOSAccelerometer]);
end;
end.