- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//开始初始化引擎
[JPEngine startEngine];
//读取js文件
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"];
NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
//运行js文件
[JPEngine evaluateScript:script];
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[ViewController alloc]init];
[self.window addSubview:[self genView]];
[self.window makeKeyAndVisible];
return YES;
}
- (UIView *)genView
{
UIView * view= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
view.backgroundColor = [UIColor redColor];
return view;
}
require('UIView, UIColor, UILabel')
//要替换函数的类
defineClass('AppDelegate', {
//替换函数
//要替换函数的名称
genView: function() {
var view = self.ORIGgenView();
view.setBackgroundColor(UIColor.greenColor())
var label = UILabel.alloc().initWithFrame(view.frame());
label.setText("JSPatch");
label.setTextAlignment(1);
view.addSubview(label);
return view;
}
});
- (void)viewDidLoad {
[super viewDidLoad];
// ‘\'符用于进行换行
[JPEngine evaluateScript:@"\
var alertView = require('UIAlertView').alloc().init();\
alertView.setTitle('Alert');\
alertView.setMessage('AlertView from js'); \
alertView.addButtonWithTitle('OK');\
alertView.show(); \
"];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[JPEngine startEngine];
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"];
NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
[JPEngine evaluateScript:script];
[self performSelectorOnMainThread:@selector(creatView) withObject:nil waitUntilDone:nil];
}
require('UIView, UIColor, UILabel')
defineClass('ViewController', {
// replace the -genView method
creatView: function() {
var view = UIView.alloc().initWithFrame({x:20, y:20, width:100, height:100});
view.setBackgroundColor(UIColor.greenColor());
var label = UILabel.alloc().initWithFrame({x:0, y:0, width:100, height:100});
label.setText("JSPatch");
label.setTextAlignment(1);
view.addSubview(label);
self.view().addSubview(view)
}
});
require('UIView')
require('UIView, UIColor, UILabel')
require('UIView').alloc().init()
UIColor.redColor()
view.addSubview(label)
label.setText("JSPatch")
/*
classDeclaration:要添加或者重写方法的类名 字符串 如果此类不存在 则会创建新的类
instanceMethods:要添加或者重写的实例方法 {}
classMethods:要添加或者重写的类方法 {}
*/
defineClass(classDeclaration, instanceMethods, classMethods)
defineClass('ViewController', {
// replace the -genView method
newFunc: function() {
//编写实例方法
self.view().setBackgroundColor(UIColor.redColor())
}
},{
myLoad:function(){
//编写类方法
}
}
)
defineClass('ViewController', {
// replace the -genView method
viewDidLoad: function() {
//编写实例方法
self.ORIGviewDidLoad()
}
}
)
defineClass('ViewController', {
// replace the -genView method
viewDidLoad: function() {
//编写实例方法
self.super().viewDidLoad()
}
}
)
defineClass('ViewController', {
// replace the -genView method
viewDidLoad: function() {
//编写实例方法
self.super().viewDidLoad()
self.setProp_forKey("JSPatch", "data")
},
touchesBegan_withEvent(id,touch){
self.getProp("data")
self.view().setBackgroundColor(UIColor.redColor())
}
}
)
defineClass("ViewController2: UIViewController <UIAlertViewDelegate>", {
viewDidAppear: function(animated) {
var alertView = require('UIAlertView')
.alloc()
.initWithTitle_message_delegate_cancelButtonTitle_otherButtonTitles(
"Alert",
"content",
self,
"OK",
null
)
alertView.show()
},
alertView_clickedButtonAtIndex:function(alertView, buttonIndex) {
console.log('clicked index ' + buttonIndex)
}
})
var view = require('UIView').alloc().init()
view.setFrame({x:100,y:100,width:100,height:100})
view.setCenter({x:200,y:200})
var size = {width:200,height:200}
view.setFrame({x:100,y:100,width:size.width,height:size.height})
var range = {location: 0, length: 1}
self.performSelector_withObject("func:", 1)
typedef void(^block)(NSString * str);
@interface ViewController ()
@end
@implementation ViewController
-(block)getBlock{
block block = ^(NSString * str){NSLog(@"%@",str);};
return block;
}
@end
defineClass("ViewController", {
viewDidAppear: function(animated) {
var func = self.getBlock()
func("123")
}
})
@interface ViewController ()
@end
@implementation ViewController
-(void)run:(void(^)(NSString * str))block{
block(@"123");
}
@end
defineClass("ViewController", {
viewDidAppear: function(animated) {
//run 方法中需要传入一个block
self.run(block("NSString*",function(str){console.log(str)}))
}
})
defineClass("ViewController", {
viewDidAppear: function(animated) {
//run 方法中需要传入一个block
var slf = self
self.run(block("NSString*",
function(str){
console.log(str)
slf.log(str)
}))
}
})
var slf = __weak(self) var stgSef = __strong(self)
//阻塞当前线程一定时间
dispatch_after(1.0, function(){
})
//为主线程添加异步任务
dispatch_async_main(function(){
})
//为主线程添加同步任务
dispatch_sync_main(function(){
})
//向全局队列中添加任务
dispatch_async_global_queue(function(){
})
JSPatch中不可以直接使用Objective-C中定义的枚举,但是可以用其枚举的真实值进行传递。例如:
//UIControlEventTouchUpInside的值是1<<6
btn.addTarget_action_forControlEvents(self, "handleBtn", 1<<6);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有