iOS:snapshot view add control logic

This commit is contained in:
pengfei.zhou 2021-07-14 15:23:33 +08:00 committed by osborn
parent 4c7eb36565
commit 49d3426d85
5 changed files with 143 additions and 7 deletions

View File

@ -121,7 +121,7 @@ - (void)onClick {
}
if ([DoricRegistry isEnableRenderSnapshot]) {
UIAlertAction *snapshot = [UIAlertAction actionWithTitle:@"Snapshot" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_) {
DoricSnapshotView *doricSnapshotView = [DoricSnapshotView new];
DoricSnapshotView *doricSnapshotView = [[DoricSnapshotView alloc] initWithDoricContext:self.doricContext];
doricSnapshotView.top = 50;
[self.doricContext.vc.view addSubview:doricSnapshotView];
[self.vc.navigationController popViewControllerAnimated:NO];

View File

@ -20,6 +20,8 @@
#import <Foundation/Foundation.h>
#import "DoricFloatView.h"
#import <DoricCore/Doric.h>
@interface DoricSnapshotView : DoricFloatView
- (instancetype)initWithDoricContext:(DoricContext *)context;
@end

View File

@ -18,19 +18,149 @@
//
#import "DoricSnapshotView.h"
#import <DoricCore/Doric.h>
#import <JavaScriptCore/JavaScriptCore.h>
@interface DoricSnapshotView ()
@property(nonatomic, weak) DoricContext *doricContext;
@property(nonatomic, strong) UIImageView *prevBtn;
@property(nonatomic, strong) UIImageView *nextBtn;
@property(nonatomic, strong) UIImageView *closeBtn;
@property(nonatomic, strong) UILabel *indexLabel;
@property(nonatomic, assign) NSUInteger snapSize;
@property(nonatomic, assign) NSUInteger snapIndex;
@end
@implementation DoricSnapshotView
- (instancetype)init {
- (instancetype)initWithDoricContext:(DoricContext *)context {
if (self = [super init]) {
_doricContext = context;
[self setupUI];
}
return self;
}
- (void)setupUI {
self.width = 20;
self.height = 20;
self.backgroundColor = [UIColor redColor];
self.width = 240;
self.height = 70;
self.backgroundColor = DoricColor(@(0xffecf0f1));
self.alpha = 0.8f;
UIImageView *moveBtn = [[UIImageView new] also:^(UIImageView *it) {
UIImage *image = [UIImage imageNamed:@"DoricDevkit.bundle/icon_doricdev_move"];
it.image = image;
it.width = 40;
it.height = 40;
it.contentMode = UIViewContentModeScaleToFill;
[self addSubview:it];
}];
self.prevBtn = [[UIImageView new] also:^(UIImageView *it) {
UIImage *image = [UIImage imageNamed:@"DoricDevkit.bundle/icon_doricdev_prev"];
it.image = image;
it.width = 40;
it.height = 40;
it.contentMode = UIViewContentModeScaleToFill;
[self addSubview:it];
it.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onPrev)];
[it addGestureRecognizer:tapGestureRecognizer];
}];
self.nextBtn = [[UIImageView new] also:^(UIImageView *it) {
UIImage *image = [UIImage imageNamed:@"DoricDevkit.bundle/icon_doricdev_next"];
it.image = image;
it.width = 40;
it.height = 40;
it.contentMode = UIViewContentModeScaleToFill;
[self addSubview:it];
it.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onNext)];
[it addGestureRecognizer:tapGestureRecognizer];
}];
self.closeBtn = [[UIImageView new] also:^(UIImageView *it) {
UIImage *image = [UIImage imageNamed:@"DoricDevkit.bundle/icon_doricdev_close"];
it.image = image;
it.width = 40;
it.height = 40;
it.contentMode = UIViewContentModeScaleToFill;
[self addSubview:it];
it.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClose)];
[it addGestureRecognizer:tapGestureRecognizer];
}];
self.indexLabel = [[UILabel new] also:^(UILabel *it) {
it.width = 40;
it.height = 40;
it.textAlignment = NSTextAlignmentCenter;
it.font = [UIFont systemFontOfSize:30];
[self addSubview:it];
}];
moveBtn.left = 0;
moveBtn.centerY = self.centerY;
self.prevBtn.left = moveBtn.right + 5;
self.prevBtn.centerY = self.centerY;
self.indexLabel.left = self.prevBtn.right + 10;
self.indexLabel.centerY = self.centerY;
self.nextBtn.left = self.indexLabel.right + 10;
self.nextBtn.centerY = self.centerY;
self.closeBtn.left = self.nextBtn.right + 10;
self.closeBtn.centerY = self.centerY;
[[self.doricContext callEntity:@"__renderSnapshotDepth__" withArgumentsArray:@[]] setResultCallback:^(JSValue *result) {
self.snapSize = [[result toNumber] unsignedIntegerValue];
dispatch_async(dispatch_get_main_queue(), ^{
self.snapIndex = self.snapSize;
[self updateUI];
});
}];
}
- (void)onPrev {
if (self.snapIndex <= 0) {
return;
}
self.snapIndex--;
[self updateUI];
}
- (void)onNext {
if (self.snapIndex >= self.snapSize) {
return;
}
self.snapIndex++;
[self updateUI];
}
- (void)onClose {
self.snapIndex = self.snapSize;
[self updateUI];
[self removeFromSuperview];
}
- (void)updateUI {
self.prevBtn.alpha = self.snapIndex <= 0 ? 0.5f : 1;
self.nextBtn.alpha = self.snapIndex >= self.snapSize ? 0.5f : 1;
self.indexLabel.text = [NSString stringWithFormat:@"%@", @(self.snapIndex)];
[[self.doricContext callEntity:@"__restoreRenderSnapshot__" withArgumentsArray:@[@(self.snapIndex)]] setResultCallback:^(JSValue *result) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.doricContext.rootNode.view.subviews forEach:^(__kindof UIView *obj) {
[obj removeFromSuperview];
}];
[self.doricContext.rootNode clearSubModel];
[[result toArray] forEach:^(NSDictionary *obj) {
NSString *viewId = obj[@"id"];
if (self.doricContext.rootNode.viewId == nil && [@"Root" isEqualToString:[obj optString:@"type"]]) {
self.doricContext.rootNode.viewId = viewId;
[self.doricContext.rootNode blend:[obj optObject:@"props"]];
[self.doricContext.rootNode requestLayout];
} else {
DoricViewNode *viewNode = [self.doricContext targetViewNode:viewId];
[viewNode blend:[obj optObject:@"props"]];
[viewNode requestLayout];
}
}];
});
}];
}
@end

View File

@ -47,6 +47,7 @@ - (void)viewDidLoad {
}]];
[DoricRegistry register:[DemoLibrary new]];
[DoricRegistry enablePerformance:YES];
[DoricRegistry enableRenderSnapshot:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

View File

@ -84,6 +84,9 @@ - (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray
}
- (void)init:(NSString *)initData {
if ([DoricRegistry isEnableRenderSnapshot]) {
[self callEntity:@"__enableSnapshot__" withArgumentsArray:@[]];
}
self.extra = initData;
if (initData) {
[self callEntity:DORIC_ENTITY_INIT withArgumentsArray:@[initData]];