官方提供的demo截图
先分析一下iSpectrum官方demo的框架
1 Launcher是程序入口,类似于main.m,实际上代码解构也差不多
public void applicationDidFinishLaunching(UIApplication app)
{
UIWindow window = new UIWindow();
window.initWithFrame(UIScreen.mainScreen().bounds());
nav = new UINavigationController();
UIViewController ctr = new UIViewController();
ctr.init();
ctr.setTitle("Action sheet");
ctr.setView(new MainView(nav));
nav.initWithRootViewController(ctr);
window.addSubview(nav.view());
window.makeKeyAndVisible();
}
2 看看mainview,没有delloc确实很爽,不过由于没有nib,所以只能用initWithFrame很原始的添加组件了。
public class MainView extends UIView {
/**
* Fields
*/
private UIActionSheet actionSheet;
private UINavigationController nav;
/**
*
* @param n UINavigationController containing this view.
*/
public MainView(UINavigationController n){
super();
init();
nav = n;
/*
* ActionSheet
*/
actionSheet = new UIActionSheet();
actionSheet.init();
actionSheet.setActionSheetStyle(UIActionSheetStyle.UIActionSheetStyleBlackOpaque);
actionSheet.setDelegate(new ActionSheetDelegate(this));
actionSheet.setTitle("How many items do you want to display ?");
actionSheet.addButtonWithTitle("Cancel");
actionSheet.setCancelButtonIndex(0);
actionSheet.dismissWithClickedButtonIndexAnimated(0, true);
actionSheet.addButtonWithTitle("One item");
actionSheet.addButtonWithTitle("Two items");
actionSheet.addButtonWithTitle("Three items");
actionSheet.addButtonWithTitle("Four items");
/*
* Button
* Being used to open the UIActionSheet.
*/
UIButton button = new UIButton(){
//@Override
public void controlEvent() {
// Display the UIActionSheet
actionSheet.showInView(this.superview());
}
};
button.initWithFrame(CGRect.CGRectMake(85, 30, 150, 31));
button.setBackgroundImageForState(UIImage.imageNamed("buttonBackground.tiff"), 0);
button.setTitleForState("Create view", 0);
button.setTitleColorForState(UIColor.blackColor(), 0);
button.addTargetActionForControlEvents(UIControlEvents.UIControlEventTouchUpInside);
addSubview(button);
}
/**
*
* @return nav Current UINavigationController.
*/
public UINavigationController getNavigationController(){
return nav;
}
}
3 同样也有delegation类
public class ActionSheetDelegate extends UIActionSheetDelegate {
/**
* Field
*/
private MainView view;
/**
*
* @param v MainView that create this object.
*/
public ActionSheetDelegate(MainView v){
super();
init();
view = v;
}
/**
* Method called by system when user select a button on action sheet.
* @param actionSheet ActionSheet calling this method.
* @param buttonIndex Index of selected button.
*
*/
//@Override
public void actionSheetClickedButtonAtIndex(UIActionSheet actionSheet,
int buttonIndex) {
if(buttonIndex == 0){
// Click on "cancel" button
return;
}
else {
UIView newView = new UIView().init();
// Add to the new UIView as many subviews as user wants.
for(int i=1; i<=buttonIndex; i++){
UIImageView imageView = new UIImageView();
imageView.initWithFrame(CGRect.CGRectMake(i*40, 100, 30, 30));
imageView.setImage(UIImage.imageNamed("img"+i+".tiff"));
newView.addSubview(imageView);
}
UIViewController viewController = new UIViewController().init();
if(buttonIndex == 1)
viewController.setTitle(buttonIndex + " added item");
else
viewController.setTitle(buttonIndex + " added items");
viewController.setView(newView);
// Push this new view into UINavigationController stack.
view.getNavigationController().pushViewControllerAnimated(viewController, false);
}
}
}
从简单的demo中似乎看不到有用java的必要,因为完全无法用到java中很多功能和开源的代码,而且受到插件开发者的限制。