CNLiveThreeLoginView.m 5.18 KB
//
//  CNLiveThreeLoginView.m
//  CNLiveLoginModule
//
//  Created by CNLive-zxw on 2019/9/6.
//  Copyright © 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveThreeLoginView.h"
#import "YYKit.h"
#import "CNLiveCategory.h"
#import "CNLiveShareImageManager.h"
#import "WXApi.h"
#import <AuthenticationServices/AuthenticationServices.h>

@interface CNLiveThreeLoginView()

@end
@implementation CNLiveThreeLoginView
static NSInteger const rowHeight = 60;
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews:frame];
    }
    return self;
    
}
- (void)createSubviews:(CGRect)frame{
    CGFloat width = frame.size.width;
    CGFloat nameWidth = width/3.0;
    CGFloat lineLeft = 15;
    CGFloat lineWidth = nameWidth - lineLeft*2;
    
    UIView *leftLine = [[UIView alloc] initWithFrame:CGRectMake(lineLeft, (rowHeight/3.0-1)/2.0, lineWidth, 1)];
    leftLine.backgroundColor = [UIColor colorWithRed:173/255.0 green:173/255.0 blue:173/255.0 alpha:1.0];
    [self addSubview:leftLine];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(nameWidth, 0, nameWidth, rowHeight/3.0)];
    name.textColor = [UIColor colorWithRed:173/255.0 green:173/255.0 blue:173/255.0 alpha:1.0];
    name.font = [UIFont systemFontOfSize:14];
    name.textAlignment = NSTextAlignmentCenter;
    name.text = @"第三方账号登录";
    [self addSubview:name];
    
    UILabel *rightLine = [[UILabel alloc] initWithFrame:CGRectMake(name.right+15, leftLine.top, leftLine.width, 1)];
    rightLine.backgroundColor = [UIColor colorWithRed:173/255.0 green:173/255.0 blue:173/255.0 alpha:1.0];
    [self addSubview:rightLine];
    
    NSMutableArray *pics = [NSMutableArray arrayWithArray:@[@"login_qq",@"login_weibo"]];
    if([WXApi isWXAppInstalled]||[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"wechat://"]]){
        [pics insertObject:@"login_wechat" atIndex:0];
    }
    NSUInteger number = pics.count;
    if (@available(iOS 13, *)) {
        [pics addObject:@"login_apple"];
        number = pics.count + 1;
    }
    CGFloat btnWidth = width/number*1.0;

    for(int i = 0; i < pics.count; i++){
        // 苹果登录在最后一个
        if (pics.count-1 == i && [pics[pics.count-1] isEqualToString:@"login_apple"]) {
            if (@available(iOS 13, *)) {
                ASAuthorizationAppleIDButton *btn = [ASAuthorizationAppleIDButton buttonWithType:ASAuthorizationAppleIDButtonTypeDefault style:ASAuthorizationAppleIDButtonStyleWhite];
                btn.tag = 10000+i;
                btn.layer.masksToBounds = YES;
                btn.layer.cornerRadius = 18.0;
                btn.layer.borderWidth = 1.0;
                btn.layer.borderColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0].CGColor;
                btn.frame = CGRectMake(btnWidth*i, name.bottom+12, 2*btnWidth, 36);
                [btn addTarget:self action:@selector(btnDidClicked:)  forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:btn];
             }
            
        }else {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = 10000+i;
            btn.frame = CGRectMake(btnWidth*i, name.bottom, btnWidth, rowHeight);
            UIImage *image = [self getImageWithImageName:pics[i] bundleName:@"CNLiveBLoginModule" targetClass:[self class]];
            [btn setImage:image forState:UIControlStateNormal];
            btn.adjustsImageWhenHighlighted = NO;
            [btn addTarget:self action:@selector(btnDidClicked:)  forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
        }
    }

}
- (void)btnDidClicked:(UIButton *)btn{
    if([WXApi isWXAppInstalled]||[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"wechat://"]]){
        if (self.delegate && [self.delegate respondsToSelector:@selector(clickedButton:type:)]) {
            if(btn.tag == 10000){
                [self.delegate clickedButton:self type:CNLiveSendAuthRespWX];
                
            }else if (btn.tag == 10001){
                [self.delegate clickedButton:self type:CNLiveSendAuthRespQQ];
                
            }else if(btn.tag == 10002){
                [self.delegate clickedButton:self type:CNLiveSendAuthRespWB];
                
            }else if(btn.tag == 10003){
                [self.delegate clickedButton:self type:CNLiveSendAuthRespApple];
                
            }
        }
    }else{
        if (self.delegate && [self.delegate respondsToSelector:@selector(clickedButton:type:)]) {
            if(btn.tag == 10000){
                [self.delegate clickedButton:self type:CNLiveSendAuthRespQQ];
                
            }else if(btn.tag == 10001){
                [self.delegate clickedButton:self type:CNLiveSendAuthRespWB];
                
            }else if(btn.tag == 10002){
                [self.delegate clickedButton:self type:CNLiveSendAuthRespApple];
                
            }
        }
    }
        
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end