Skip to content

Instantly share code, notes, and snippets.

@liuzhida33
Created July 31, 2018 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuzhida33/d07a62bbb3bbe0c56ec29d03ce2988f7 to your computer and use it in GitHub Desktop.
Save liuzhida33/d07a62bbb3bbe0c56ec29d03ce2988f7 to your computer and use it in GitHub Desktop.
hook mj_header 添加下拉刷新震动
@implementation UIScrollView (Swizz)

+ (void)load {
    
    SEL originalSelector = @selector(setMj_header:);
    SEL swizzledSelector = @selector(nr_setMj_header:);
    Method originalMethod = class_getInstanceMethod(self.class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(self.class, swizzledSelector);
    
    BOOL didAddMethod =
    class_addMethod(self.class,
                    originalSelector,
                    method_getImplementation(swizzledMethod),
                    method_getTypeEncoding(swizzledMethod));
    if (didAddMethod) {
        class_replaceMethod(self.class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    }else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

- (void)nr_setMj_header:(MJRefreshHeader *)mj_header {
    
    @weakify(mj_header)
    [RACObserve(mj_header, state) subscribeNext:^(id  _Nullable x) {
        @strongify(mj_header)
        if (mj_header.state == MJRefreshStatePulling) {
            AudioServicesPlaySystemSound(1519);
        }
    }];
    [self nr_setMj_header:mj_header];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment