weex使用记录1

weex使用记录1_第1张图片
AFEC38C7-BD4C-47A2-BA4A-4E5CD03D9C7B

1.ios接入WeexSDK

利用cocoapod管理

pod 'WeexSDK'

然后在(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中初始化weex.

- (void)configWeex {
    //business configuration
    [WXAppConfiguration setAppGroup:@"AliApp"];
    [WXAppConfiguration setAppName:@"WeexDemo"];
    [WXAppConfiguration setAppVersion:@"1.0.0"];
    //init sdk environment
    [WXSDKEngine initSDKEnvironment];
    
    [WXSDKEngine registerHandler:[PCImageLoader new] withProtocol:@protocol(WXImgLoaderProtocol)];
    
    //set the log level
    [WXLog setLogLevel: WXLogLevelAll];
}

(这里的registerHandler我还没搞懂是什么意思)

2.在ios跑weex上的helloworld




这是官方的weex helloworld的代码.

我按照文档安装weex-toolkit后,直接在本地运行

weex list.we

然后就出问题了,界面没出来.

weex使用记录1_第2张图片
D4673C2E-3029-44DC-8230-39C56529BFD7

google搜了下说是toolkit的bug

17569CE0-BB90-4886-9C58-A7D4A49DE98B

有人说新版解决了

6294C1C8-6FC9-4C03-A4DB-0CD2299F35D0

我是今天才使用,已经是最新版了,但是还是有问题.

问了饿了吗的开发(因为他们是用native+weex的),他们建议我用chrome调试下

6FE8CDC4-4217-4E2E-A6AC-C34D8EE1169B

一番折腾后还是不行,不是很熟前端啊...

还好还有替代方案.

使用weex playground的在线编辑

weex使用记录1_第3张图片
9A92F716-5C96-425A-AB7D-013045515AD7

http://dotwe.org/vue

在线编辑这里可以生成对应的js

weex使用记录1_第4张图片
686605FE-004A-4EE6-8F15-A9D87BA336D2

把生成的js新建list.js文件然后放到xcode里

然后新建vc文件

#import 

@interface WeexTestViewController ()
@property (nonatomic, strong) WXSDKInstance *instance;
@property (nonatomic, strong) NSURL *url;
@property (weak, nonatomic) UIView *weexView;
@end
@implementation WeexTestViewController

- (void)dealloc {
    [self.instance destroyInstance];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWeexView];
}

#pragma mark - setters and getters -
- (void)initWeexView {
    _instance = [[WXSDKInstance alloc] init];
    _instance.viewController = self;
    _instance.frame = self.view.frame;
    [_instance renderWithURL:self.url
                     options:@{@"bundleUrl":[self.url absoluteString]}
                        data:nil];
    __weak typeof(self) weakSelf = self;
    _instance.onCreate = ^(UIView *view) {
        weakSelf.weexView = view;
        [weakSelf.weexView removeFromSuperview];
        [weakSelf.view addSubview:weakSelf.weexView];
    };
    _instance.onFailed = ^(NSError *error) {
        NSLog(@"处理失败:%@",error);
    };
    _instance.renderFinish = ^ (UIView *view) {
        NSLog(@"渲染完成");
    };
}

- (NSURL *)url {
    if (!_url) {
        _url =  [[NSBundle mainBundle] URLForResource:@"list"
                                        withExtension:@"js"];
    }
    return _url;
}

@end

这个也是根据文档写的demo.

这样运行,图片是显示不出来的,需要WXImgLoaderProtocol,WXImageOperationProtocol的方法重写

#import 
#import 

@interface PCImageLoader () 
@property (nonatomic, strong) AFHTTPSessionManager *sessionManager;
@property (nonatomic, strong) NSURLSessionDataTask *dataTask;
@end
@implementation PCImageLoader

- (id)downloadImageWithURL:(NSString *)url
                                          imageFrame:(CGRect)imageFrame
                                            userInfo:(NSDictionary *)options
                                           completed:(void(^)(UIImage *image,  NSError *error, BOOL finished))completedBlock {
    self.dataTask = [self.sessionManager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSData *imageData = responseObject;
        UIImage *image = [UIImage imageWithData:imageData];
        if (image&&!CGRectEqualToRect(imageFrame, CGRectZero)) {
            UIGraphicsBeginImageContext(imageFrame.size);
            [image drawInRect:imageFrame];
            image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
        completedBlock(image,nil,YES);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completedBlock(nil,error,YES);
    }];
    return self;
}

- (void)cancel {
    [self.dataTask cancel];
}

- (AFHTTPSessionManager *)sessionManager {
    if (!_sessionManager) {
        _sessionManager = [AFHTTPSessionManager manager];
        _sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    }
    return _sessionManager;
}

这个PCImageLoader类就是didFinishLaunchingWithOptions注册的组件

[WXSDKEngine registerHandler:[PCImageLoader new] withProtocol:@protocol(WXImgLoaderProtocol)];

终于在ios上显示出来了

weex使用记录1_第5张图片
96F52208-6112-4890-BC5D-482DF7B0AAD5

3.学习下weex的内建组件

  • a
  • slider
  • indicator
  • switch
  • text
  • textarea
  • video
  • web
  • div
  • image
  • waterfall
  • input
  • list
  • cell
  • loading
  • refresh
  • scroller

a




weex使用记录1_第6张图片
AB29D0E4-49EE-41A4-987B-230FACBF803C

这里为什么要一个template括起来,还没搞懂

slider






weex使用记录1_第7张图片
8AFEB43A-40D6-4125-9973-73C165BBE22E

这里为什么width不能设成100%的?写死像素,那怎么适配各种屏幕?

indicator






weex使用记录1_第8张图片
B62F4C17-9391-4167-AEBF-73E9E7713825

switch






weex使用记录1_第9张图片
403824B9-7B4B-442B-9BFA-89E287BB280F

text




weex使用记录1_第10张图片
B54F99B1-2B2D-41B4-98DE-38B7DBC8033A

textarea






weex使用记录1_第11张图片
E11522E6-647F-4F01-8870-9DA7E73B1693

video






weex使用记录1_第12张图片
C9C1FD6F-729C-445B-A833-FCF97F294290

这里发现一个神奇的问题,点击播放和停止经常没反应.

用weex playground扫就不会出现这个问题.

4.实践下

这样跟着文档一个个去熟悉组件效率太低了.

还不如直接在项目里把原生的页面用weex实现.

现在都是写好we代码然后转成js代码,放到项目里运行,这样开发效率好低,应该可以直接把js代码写到项目里让weexsdk解析的.

先记录到这里,下一篇继续...

你可能感兴趣的:(weex使用记录1)