一、创建view
require("UIView, UIColor");
defineClass("XNWashHomeTopView", {
createScrollView: function() {
var view = UIView.alloc().initWithFrame({x:100, y:100, width:100, height:100});
view.setBackgroundColor(UIColor.redColor());
self.bgView().addSubview(view);
}
}, {});
注意:frame必须用{x:100, y:100, width:100, height:100}这种方式设置。同理,CGSizeMake需改为下面的方式:
//_scrollView.contentSize = CGSizeMake(150, 155);
_scrollView.contentSize = ({width: 750, height: 155});
二、宏的使用
Objective-C 里的宏同样不能直接在 JS 上使用。若定义的宏是一个值,可以在 JS 定义同样的全局变量代替,若定义的宏是程序,可以在JS展开宏。
require("UIScreen");
//不能使用,必须改为下面的方式获取
//#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.height
defineClass("XNWashHomeTopView", {
createScrollView: function() {
let screen_width = UIScreen.mainScreen().bounds().width;
console.log(screen_width);
}
}, {});
demo
转换前OC代码:
- (void)createScrollView {
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 105, self.width, 155)];
[self.bgView addSubview:_scrollView];
_scrollView.contentSize = CGSizeMake(_scrollView.width * 2, _scrollView.height);
_scrollView.backgroundColor = [UIColor clearColor];
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
for (int i = 0; i < 2; i ++) {
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(_scrollView.width * i, 0, _scrollView.width, _scrollView.height)];
[_scrollView addSubview:v];
v.backgroundColor = [UIColor clearColor];
UILabel *commissionLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, v.width, 20)];
[v addSubview:commissionLab];
commissionLab.textColor = [UIColor whiteColor];
commissionLab.font = [UIFont systemFontOfSize:25 weight:UIFontWeightSemibold];
commissionLab.textAlignment = NSTextAlignmentCenter;
commissionLab.text = @"0.00";
commissionLab.tag = 100 * (i + 1);
UILabel *commissionNoteLab = [[UILabel alloc] initWithFrame:CGRectMake(0, commissionLab.bottom + 10, v.width, 15)];
[v addSubview:commissionNoteLab];
commissionNoteLab.textColor = rgba(147, 188, 252, 1);
commissionNoteLab.font = SystemFont(11);
commissionNoteLab.textAlignment = NSTextAlignmentCenter;
if (_isBoss) {
commissionNoteLab.text = @"门店收入(元)";
NSArray *a1 = @[@"今日任务(台)",@"未开始(台)",@"施工中(台)",@"已完成(台)",@"非会老客户(台)",@"新客户(台)"];
CGFloat item_w = self.width / 3;
CGFloat top_offset = 0;
for (int j = 0; j < a1.count; j ++) {
if (j > 2) {
top_offset = 35 + 15;
}
UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(item_w * (j % 3), commissionNoteLab.bottom + 25 + top_offset, item_w, 35)];
[v addSubview:vv];
vv.backgroundColor = [UIColor clearColor];
UILabel *countLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, vv.width, 15)];
[vv addSubview:countLab];
countLab.text = @"0";
countLab.textColor = [UIColor whiteColor];
countLab.font = SystemFont(16);
countLab.textAlignment = NSTextAlignmentCenter;
countLab.tag = 1000 * (i + 1) + j;
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, countLab.bottom + 5, vv.width, 15)];
[vv addSubview:lab];
lab.text = a1[j];
if (i == 1 && j == 0) {
lab.text = @"本月任务(台)";
}
lab.textColor = rgba(147, 188, 252, 1);
lab.font = SystemFont(11);
lab.textAlignment = NSTextAlignmentCenter;
}
} else {
commissionNoteLab.text = @"今日提成(元)";
if (i == 1) {
commissionNoteLab.text = @"本月提成(元)";
}
NSArray *a1 = @[@"我的任务(项目)",@"未开始",@"施工中(台)",@"已完成(台)"];
CGFloat item_w = self.width / 2;
CGFloat top_offset = 0;
for (int j = 0; j < a1.count; j ++) {
if (j > 1) {
top_offset = 35 + 15;
}
UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(item_w * (j % 2), commissionNoteLab.bottom + 25 + top_offset, item_w, 35)];
[v addSubview:vv];
vv.backgroundColor = [UIColor clearColor];
UILabel *countLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, vv.width, 15)];
[vv addSubview:countLab];
countLab.text = @"0";
countLab.textColor = [UIColor whiteColor];
countLab.font = SystemFont(16);
countLab.textAlignment = NSTextAlignmentCenter;
countLab.tag = 1000 * (i + 1) + j;
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, countLab.bottom + 5, vv.width, 15)];
[vv addSubview:lab];
lab.text = a1[j];
lab.textColor = rgba(147, 188, 252, 1);
lab.font = SystemFont(11);
lab.textAlignment = NSTextAlignmentCenter;
}
}
}
}
转换后
require("UIScrollView, UIColor, UIView, UILabel, UIFont");
defineClass("XNWashHomeTopView", {
createScrollView: function() {
self.setScrollView(UIScrollView.alloc().initWithFrame({x:0, y:105, width:self.width(), height:155}));
self.bgView().addSubview(self.scrollView());
self.scrollView().setContentSize({width:self.scrollView().width() * 2, height:self.scrollView().height()});
self.scrollView().setBackgroundColor(UIColor.redColor());
self.scrollView().setShowsHorizontalScrollIndicator(NO);
self.scrollView().setPagingEnabled(YES);
self.scrollView().setDelegate(self);
for (var i = 0; i < 2; i++) {
var v = UIView.alloc().initWithFrame({x:self.scrollView().width() * i, y:0, width:self.scrollView().width(), height:self.scrollView().height()});
self.scrollView().addSubview(v);
v.setBackgroundColor(UIColor.clearColor());
var commissionLab = UILabel.alloc().initWithFrame({x:0, y:0, width:v.width(), height:20});
v.addSubview(commissionLab);
commissionLab.setTextColor(UIColor.whiteColor());
commissionLab.setFont(UIFont.boldSystemFontOfSize(25));
commissionLab.setTextAlignment(1);
commissionLab.setText("0.00");
commissionLab.setTag(100 * (i + 1));
var commissionNoteLab = UILabel.alloc().initWithFrame({x:0, y:commissionLab.bottom() + 10, width:v.width(), height:15});
v.addSubview(commissionNoteLab);
commissionNoteLab.setTextColor(UIColor.colorWithRed_green_blue_alpha(147 / 255, 188 / 255, 252 / 255, 1));
commissionNoteLab.setFont(UIFont.systemFontOfSize(11));
commissionNoteLab.setTextAlignment(1);
if (self.isBoss()) {
commissionNoteLab.setText("门店收入(元)");
var a1 = [ "今日任务(台)", "未开始(台)", "施工中(台)", "已完成(台)", "非会老客户(台)", "新客户(台)" ];
var item_w = self.width() / 3;
var top_offset = 0;
for (var j = 0; j < a1.length; j++) {
if (j > 2) {
top_offset = 35 + 15;
}
var vv = UIView.alloc().initWithFrame({x:item_w * (j % 3), y:commissionNoteLab.bottom() + 25 + top_offset, width:item_w, height:35});
v.addSubview(vv);
vv.setBackgroundColor(UIColor.clearColor());
var countLab = UILabel.alloc().initWithFrame({x:0, y:0, width:vv.width(), height:15});
vv.addSubview(countLab);
countLab.setText("0");
countLab.setTextColor(UIColor.whiteColor());
countLab.setFont(UIFont.systemFontOfSize(16));
countLab.setTextAlignment(1);
countLab.setTag(1e3 * (i + 1) + j);
var lab = UILabel.alloc().initWithFrame({x:0, y:countLab.bottom() + 5, width:vv.width(), height:15});
vv.addSubview(lab);
lab.setText(a1[j]);
if (i == 1 && j == 0) {
lab.setText("本月任务(台)");
}
lab.setTextColor(UIColor.colorWithRed_green_blue_alpha(147 / 255, 188 / 255, 252 / 255, 1));
lab.setFont(UIFont.systemFontOfSize(11));
lab.setTextAlignment(1);
}
} else {
commissionNoteLab.setText("今日提成(元)");
if (i == 1) {
commissionNoteLab.setText("本月提成(元)");
}
var a1 = [ "我的任务(项目)", "未开始", "施工中(台)", "已完成(台)" ];
var item_w = self.width() / 2;
var top_offset = 0;
for (var j = 0; j < a1.length; j++) {
if (j > 1) {
top_offset = 35 + 15;
}
var vv = UIView.alloc().initWithFrame({x:item_w * (j % 2), y:commissionNoteLab.bottom() + 25 + top_offset, width:item_w, height:35});
v.addSubview(vv);
vv.setBackgroundColor(UIColor.clearColor());
var countLab = UILabel.alloc().initWithFrame({x:0, y:0, width:vv.width(), height:15});
vv.addSubview(countLab);
countLab.setText("0");
countLab.setTextColor(UIColor.whiteColor());
countLab.setFont(UIFont.systemFontOfSize(16));
countLab.setTextAlignment(1);
countLab.setTag(1e3 * (i + 1) + j);
var lab = UILabel.alloc().initWithFrame({x:0, y:countLab.bottom() + 5, width:vv.width(), height:15});
vv.addSubview(lab);
lab.setText(a1[j]);
lab.setTextColor(UIColor.colorWithRed_green_blue_alpha(147 / 255, 188 / 255, 252 / 255, 1));
lab.setFont(UIFont.systemFontOfSize(11));
lab.setTextAlignment(1);
}
}
}
}
}, {});
网友评论