標籤:libgdx android java robovm ios
1.在IOS中綁定類
@interface ClassName : ExtendedClassName<ImplementedInterfaceName>
那麼在java中就應該是:
@NativeClasspublic class GADBannerView extends UIView {}
這裡一般形式是:
@NativeClasspublic class GADBannerView extends NSObject {}
2.在IOS中Binder 方法
- (void)loadRequest:(GADRequest *)request;
在java中
@Method(selector = "loadRequest:")//必須和IOS裡面的方法名一樣 不然無法找到public native void loadRequest(GADRequest request);//這裡方法名可以任意寫 但是參數必須一樣
如果有多個參數的方法呢?
- (void)setLocationWithLatitude:(CGFloat)latitude longitude:(CGFloat)longitude accuracy:(CGFloat)accuracyInMeters;
在java寫法是這樣的:
@Method(selector = "setLocationWithLatitude:longitude:accuracy:")public native void setLocation(float latitude, float longitude, float accuracyInMeters);
3.在IOS中綁定構造方法
- (id)initWithAdSize:(GADAdSize)size;
那麼在java中實現:
@Method(selector = "initWithAdSize:")private native @Pointer long init(GADAdSize size);//構造方法需要值,因為GADAdSize實際上是long類型
這裡的方法寫成私人的,因為不是任何人都可以調用的,所以還得寫個java裡的構造方法
public GADBannerView(GADAdSize size) { super((SkipInit)null); initObject(init(size));}必須這樣寫,不然會被java調用兩次
4.在IOS中綁定成員變數
在IOS中有很多很簡便的寫法,可以讓開發人員少些很多代碼
@property(nonatomic, copy) NSString *adUnitID;
property這個意思是OC會自動實現getter和setter 不用手動實現,那麼我們知道他的意思後就很好綁定了,和Binder 方法一樣一樣的
@Property(selector = "adUnitID")public native String getAdUnitID ();@Property(selector = "setAdUnitID:")public native void setAdUnitID (String id);
注意:並不是所有屬性都有getter和setter的 有些只有getter比如:
@property(nonatomic, readonly) BOOL hasAutoRefreshed;
還有一種寫法在綁定的時候必須強引用,不然程式會報錯:
@property(nonatomic, assign) NSObject<GADBannerViewDelegate> *delegate
在綁定的時候必須這樣寫:
@Property(selector = "delegate")public native GADBannerViewDelegate getDelegate();@Property(selector = "setDelegate:", strongRef = true)public native void setDelegate(GADBannerViewDelegate delegate);
5.在IOS中綁定枚舉類型
typedef NS_ENUM(NSInteger, GADGender) { kGADGenderUnknown, ///< Unknown gender. kGADGenderMale, ///< Male gender. kGADGenderFemale ///< Female gender.};在java中這樣寫:
typedef enum { kGADGenderUnknown, ///< Unknown gender. kGADGenderMale, ///< Male gender. kGADGenderFemale ///< Female gender.} GADGender;
以上寫法精簡自:https://github.com/BlueRiverInteractive/robovm-ios-bindings