How do I access the accelerometer in iOS using Delphi XE2?
I 开发者_运维百科tried looking through the IDE but did not find a component.
Delphi XE2 supports deploying to OSX on a mac. Deploying to an iPhone is apparently pretty easy, but it still involves using XCode as a final deployment tool. Since Delphi doesn't "officially" support deploying to iPhone hardware, I doubt they have included any components that are specific to it.
Disclaimer: I just downloaded XE2 a few hours ago, and I've only deployed one mac application so far (and it worked beautifully). So I'm far from an expert.
Edit: Anders Ohlsson has apparently written an accelerometer component for XE2. I have not tried it, but here it is.
Joseph is correct. Philip Hess has been contributing to the Delphi iOS newsgroups, and other useful information, eg:
https://forums.embarcadero.com/message.jspa?messageID=390713&tstart=125
He also has this page, here:
http://web.me.com/macpgmr/ObjP/Xcode4/ObjP_Intro.html
Which may be helpful to you. I'm currently wading through it all in order to use the CoreLocation services (amongst others). It's definitely not for the faint of heart.
There's a component that reads the accelerometer on the Embarcadero blog:
http://edn.embarcadero.com/article/41748
Here's the download link for the component: http://cc.embarcadero.com/item/28609
And here's the source code by Anders Ohlsson:
(I used this to build a component for the GPS/Compass).
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.
精彩评论