Giter VIP home page Giter VIP logo

cxwebprogressandjs's Introduction

Error in user YAML: (<unknown>): did not find expected alphabetic or numeric character while scanning an alias at line 3 column 1
---
# CXWebProgressAndJS

* [BugWacko's Blog](https://bugwacko.github.io) 

这个实例是web加载动画和网页js信息回调功能集合,里面的关键代码在于动画的添加和js的初始化和回调。

## Web Progress Animation

其中使用的动画比较简单,自带API:

```
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
```
进度动画代码如下:

```
//public method
/*
 * _progressBarView 动画控件
 */
- (void)setProgress:(float)progress animated:(BOOL)animated
{
    BOOL isGrowing = progress > 0.0;
    [UIView animateWithDuration:(isGrowing && animated) ? _barAnimationDuration : 0.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        CGRect frame = _progressBarView.frame;
        frame.size.width = progress * self.bounds.size.width;
        _progressBarView.frame = frame;
    } completion:nil];
    
    if (progress >= 1.0) {
        [UIView animateWithDuration:animated ? _fadeAnimationDuration : 0.0 delay:_fadeOutDelay options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _progressBarView.alpha = 0.0;
        } completion:^(BOOL completed){
            CGRect frame = _progressBarView.frame;
            frame.size.width = 0;
            _progressBarView.frame = frame;
        }];
    }
    else {
        [UIView animateWithDuration:animated ? _fadeAnimationDuration : 0.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _progressBarView.alpha = 1.0;
        } completion:nil];
    }
}
```

## Web And JS

实例中我们已一个分享返回为例,通过点击指定web方法,实现js回调回传数据,其中包括分享连接、图片等数据,具体可因自己开发而定。

* 添加JS回调事件

```
/*
 * JSClass js对象,将获取的js实例化
 * xxxx 表示定义的js方法名
 */
-(void)addJs:(UIWebView *)webView {

    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    JSClass *jsClass = [JSClass new];
    context[@"xxxx"] = jsClass;
    
    __weak CXWebProgress * view = self;
    jsClass.shareBlock = ^(NSString * title, NSString * desc, NSString * link, NSString * imgUrl) {
        if (view.jsDelegate && [view.jsDelegate respondsToSelector:@selector(webViewShare:withDesc:withLink:withImgUrl:)]) {
            [view.jsDelegate webViewShare:title withDesc:desc withLink:link withImgUrl:imgUrl];
        }
    };
}
```

* JSClass

```
//JSClass.h

@protocol JSProtocol <JSExport>

-(void)onMenuShare:(NSString *)title :(NSString *)desc :(NSString *)link :(NSString *)imgUrl;

@end

typedef void(^JSShareBlock)(NSString * title, NSString * desc, NSString * link, NSString * imgUrl);
@interface JSClass : NSObject<JSProtocol>

@property(nonatomic, copy) JSShareBlock shareBlock;

@end
```

```
//JSClass.m

@implementation JSClass

-(void)onMenuShare:(NSString *)title :(NSString *)desc :(NSString *)link :(NSString *)imgUrl {
    if (self.shareBlock) {
        DLog(@"----- web webview_share js action is run -----")
        self.shareBlock(title, desc, link, imgUrl);
    }
}

@end
```

才疏学浅,希望可以帮到大家,之前一直将开源控件本地化,没怎么传,现在会慢慢捡回来,希望大家多多关注。

#### happy reading!!!

---

cxwebprogressandjs's People

Contributors

bugwacko avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.