iOS 淘票票截图反馈功能实现

iOS 淘票票截图反馈功能实现_第1张图片
效果图.jpeg

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(userDidTakeScreenshotNotification:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [NSNotificationCenter.defaultCenter removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

- (void)userDidTakeScreenshotNotification:(NSNotification *)sender {
    UIWindow *window = UIApplication.sharedApplication.keyWindow;
    UIImage *snapshotImage = [UIImage snapshotImageWithView:window];
    // TODO: 将screenshotImage进行分享,可以调用友盟SDK或自己集成第三方SDK实现,这里就不做演示了
    UIButton *btn = [window showFeedbackView:snapshotImage title:@"求助反馈"];
    [btn addActionHandler:^(UIControl * _Nonnull control) {
        DDLog(@"%@", control);

    } forControlEvents:UIControlEventTouchUpInside];
    
}

code:

import "UIWindow+Helper.h"

@implementation UIWindow (Helper)

- (UIButton *)showFeedbackView:(UIImage *)image title:(NSString *)title{
    UIWindow *window = self;
    UIView *view = [window viewWithTag:9999];
    if (view) {
        [window bringSubviewToFront:view];
        return [window viewWithTag:992];
    }
    
    UIView *containView = ({
        CGFloat width = window.frame.size.width;
        CGFloat height = window.frame.size.height;
        
        CGSize imgSize = CGSizeMake(width/5.0, height/5.0);
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(width - imgSize.width, (height - imgSize.height - 30)/2.0, imgSize.width, imgSize.height + 30)];
        view.tag = 9999;
        view.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.6];
        
        [view addGestureSwipe:^(UIGestureRecognizer * _Nonnull reco) {
            [UIView animateWithDuration:0.35 animations:^{
                reco.view.transform = CGAffineTransformMakeTranslation(reco.view.transform.tx + imgSize.width, reco.view.transform.ty);
            } completion:^(BOOL finished) {
                if (finished) {
                    [reco.view removeFromSuperview];
                }
            }];
        } forDirection:UISwipeGestureRecognizerDirectionRight];
        
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imgSize.width, imgSize.height)];
        view.tag = 991;
        imgView.image = image;
        [view addSubview:imgView];
        
        title = title ? : @"求助反馈";
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 992;
        btn.frame = CGRectMake(0, imgSize.height, imgSize.width, 30);
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [view addSubview:btn];
        
        view;
    });
    [window addSubview:containView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [containView removeFromSuperview];
    });
    
    return [window viewWithTag:992];
}

@end

源码

你可能感兴趣的:(iOS 淘票票截图反馈功能实现)