// // KBSignInViewController.m // YouHuiProject // // Created by 小花 on 2018/9/27. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBSignInViewController.h" #import "KBSignInView.h" #import "KBSignInModel.h" #import "KBSignTypeModel.h" #import "KBSignPopView.h" #import "KBSignSuccView.h" #import "CCAlertShowView.h" #import "KBSignInClickView.h" #import "KBRollingCollectionViewCell.h" #import "KBPopRouleView.h" #import #import #import @interface KBSignInViewController () { UIButton *signBtn; UILabel *tipLb; CGFloat collectionViewWidth; } @property (nonatomic ,strong) UICollectionView *userCollectionView; @property (nonatomic ,strong) NSMutableArray *userArray; @property (nonatomic ,strong) NSMutableArray *collectionCellWithArry; @property (nonatomic ,strong) NSTimer *usertimer; @property (nonatomic, strong) KBSignInView *signView; @property (nonatomic, strong) KBSignInModel *signInModel; //@property (nonatomic, strong) KBSignTypeModel *signTypeModel; @property (nonatomic, strong) KBSignInClickView *signTableView; @property (nonatomic, strong) UIImageView *contentImgView; @property (nonatomic, strong) UIImageView *topImgView; @property (nonatomic, strong) UIImageView *middleImgView; @end @implementation KBSignInViewController - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; [self getSignAction]; [self signUserRollingAction]; } - (void)viewDidLoad { [super viewDidLoad]; self.userArray = [NSMutableArray arrayWithCapacity:0]; self.collectionCellWithArry = [NSMutableArray arrayWithCapacity:0]; [self configNavigationBar]; [self initSubViews]; [self addObserverForApp]; } - (void)addObserverForApp { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSignAction) name:UIApplicationDidBecomeActiveNotification object:nil]; } - (void)initSubViews { UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)]; bgImgView.image = [UIImage imageNamed:@"signIn_bg"]; [self.view addSubview:bgImgView]; self.contentImgView = [[UIImageView alloc] init]; self.contentImgView.image = [UIImage imageNamed:@"signInContent_bg"]; [self.view addSubview:self.contentImgView]; [self.contentImgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(NavBarHeight+87); make.width.mas_equalTo(FITSIZE(323)); make.height.mas_equalTo(FITSIZE(472)); make.centerX.mas_equalTo(self.view); }]; self.topImgView = [[UIImageView alloc] init]; [self.view addSubview: self.topImgView]; [self.topImgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(NavBarHeight+52); make.width.mas_equalTo(FITSIZE(266)); make.height.mas_equalTo(FITSIZE(72)); make.centerX.mas_equalTo(self.view); }]; self.middleImgView = [[UIImageView alloc] init]; [self.view addSubview: self.middleImgView]; [self.middleImgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.topImgView.mas_bottom).offset(24); make.width.mas_equalTo(FITSIZE(314)); make.height.mas_equalTo(FITSIZE(38)); make.centerX.mas_equalTo(self.view); }]; self.signView = [[KBSignInView alloc] init]; self.signView.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.signView]; [self.signView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.middleImgView.mas_bottom).offset(23); make.width.mas_equalTo(self.contentImgView); make.height.mas_equalTo(20); make.centerX.mas_equalTo(self.view); }]; [self.view addSubview:self.signTableView]; [self.signTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.signView.mas_bottom).offset(10); make.width.mas_equalTo(self.contentImgView); make.height.mas_equalTo(FITSIZE(72*4)); make.left.mas_equalTo(self.contentImgView.mas_left); }]; [self.view addSubview:self.userCollectionView]; UIButton *ruleBtn=[UIButton buttonWithType:UIButtonTypeCustom]; [ruleBtn setTitle:@"红包规则" forState:UIControlStateNormal]; [ruleBtn setTitleColor:[UIColor YHColorWithHex:0x8B7117] forState:UIControlStateNormal]; ruleBtn.backgroundColor=[UIColor clearColor]; ruleBtn.titleLabel.font=[UIFont systemFontOfSize:12]; [self.view addSubview:ruleBtn]; [ruleBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.contentImgView.mas_bottom).offset(23); make.centerX.mas_equalTo(self.view); make.width.mas_equalTo(SCREEN_WIDTH-FITSIZE(320)); make.height.mas_equalTo(FITSIZE(22)); }]; [[ruleBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) { KBPopRouleView *popView = [[KBPopRouleView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(283), Fitsize(290)) text:self.signInModel.red_roule]; CCAlertShowView *showView = [CCAlertShowView alertViewWithView:popView]; [showView show]; }]; } - (void)getSignAction { NSString *url = [NSString stringWithFormat:@"%@/api/v2/users/getUserActiveNew",BaseURL]; [KBHttp get:url params:nil success:^(id json) { self.signInModel = [KBSignInModel yy_modelWithJSON:json[@"data"]]; if (self.signInModel.sign == 1) { [self refreshUI]; }else { [self refreshUI]; KBSignPopView *popView = [[KBSignPopView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(283), Fitsize(382)) text:self.signInModel.red_message]; popView.clickBtn.hidden = NO; CCAlertShowView *showView = [CCAlertShowView showAlertViewWithView:popView backgroundDismissEnable:NO]; [showView show]; [[popView.clickBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) { [self signAction]; [showView dismiss]; }]; } } failure:^(NSError *error) { }]; } - (void)signAction { NSString *url = [NSString stringWithFormat:@"%@/api/v2/users/complateActive",BaseURL]; [KBHttp get:url params:nil success:^(id json) { [self getSignAction]; } failure:^(NSError *error) { }]; } - (void)refreshUI { [self.topImgView sd_setImageWithURL:[NSURL URLWithString:self.signInModel.sign_img] placeholderImage:nil]; [self.middleImgView sd_setImageWithURL:[NSURL URLWithString:self.signInModel.describe_img] placeholderImage:nil]; [self.signView setSignModelArr:self.signInModel.days]; [self.signTableView refreshTableViewWithArry:self.signInModel.detail]; } - (void)configNavigationBar { [self.navigationBar setNavTitle:@"签到领红包"]; self.navigationBar.navTitleLabel.textColor = [UIColor blackColor]; self.navigationBar.backgroundColor = [UIColor whiteColor]; UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomLeftButtons:@[leftBtn]]; } //- (void)showSuccessView { // KBSignSuccView *succView = [[KBSignSuccView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(283), Fitsize(290)) text:self.signTypeModel.red_message]; // CCAlertShowView *showView = [CCAlertShowView alertViewWithView:succView]; // succView.sureClick = ^{ // [showView dismiss]; // }; // [showView show]; // //} - (void)clickViewIndexPath:(NSIndexPath *)index { [self changeBtnTypeWhenClick:index]; KBSignDetailModel *model = [KBSignDetailModel yy_modelWithJSON:self.signInModel.detail[index.row]]; NSString *tbUrl = model.ot_url; if (tbUrl.length > 0) { [self jumpToTaobaoWithUrl:tbUrl parentVc:self]; UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; pasteBoard.string = model.red_url==nil?@"":model.red_url; return; } NSURL *url = [NSURL URLWithString:@"taobao://"]; if ([[UIApplication sharedApplication] canOpenURL:url]) { KBSignDetailModel *model = [KBSignDetailModel yy_modelWithJSON:self.signInModel.detail[index.row]]; UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; pasteBoard.string = model.red_url==nil?@"":model.red_url; [[UIApplication sharedApplication] openURL:url]; }else{ [MBProgressHUD showMessage:self.signInModel.uninstall_taobao]; } } - (void)changeBtnTypeWhenClick:(NSIndexPath *)index { KBSignDetailModel *model = [KBSignDetailModel yy_modelWithJSON:self.signInModel.detail[index.row]]; NSString *url = [NSString stringWithFormat:@"%@/api/v2/users/ButtonChange",BaseURL]; NSString *nowDay = self.signInModel.now_days==nil?@"":self.signInModel.now_days; NSString *Id = model.btn==nil?@"":model.btn; NSDictionary *para = @{@"days":nowDay, @"red":Id }; [KBHttp get:url params:para success:^(id json) { } failure:^(NSError *error) { }]; } - (KBSignInClickView *)signTableView { if (!_signTableView) { _signTableView = [[KBSignInClickView alloc] init]; _signTableView.clickViewDelegate = self; } return _signTableView; } #pragma mark UICollectionView滚动 - (void)signUserRollingAction { NSString *url = [NSString stringWithFormat:@"%@/api/v2/users/getRollscreen",BaseURL]; [KBHttp get:url params:nil success:^(id json) { [self.userArray removeAllObjects]; [self.collectionCellWithArry removeAllObjects]; NSArray *jsonArry = [NSArray arrayWithArray:json[@"data"]]; for (NSDictionary *userDic in jsonArry) { [self.userArray addObject:[KBSignInRollingUser yy_modelWithJSON:userDic]]; } for (KBSignInRollingUser *userModel in self.userArray) { CGFloat cellWidth = 20+[self getWidhWithString:userModel.res withFontSize:12]+10; [self.collectionCellWithArry addObject:[NSNumber numberWithFloat:cellWidth]]; collectionViewWidth+=cellWidth; } [self makeTime]; [self.userCollectionView reloadData]; } failure:^(NSError *error) { }]; } - (void)makeTime { if (collectionViewWidth>SCREEN_WIDTH) { if(!self.usertimer){ self.usertimer=[NSTimer timerWithTimeInterval:0.05 target:self selector:@selector(clicknetxt) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:self.usertimer forMode:NSRunLoopCommonModes]; } } } -(void)clicknetxt { self.userCollectionView.contentOffset=CGPointMake(self.userCollectionView.contentOffset.x+1, self.userCollectionView.contentOffset.y); } - (UICollectionView *)userCollectionView { if (!_userCollectionView) { UICollectionViewFlowLayout *layout1=[[UICollectionViewFlowLayout alloc]init]; layout1.scrollDirection=UICollectionViewScrollDirectionHorizontal; _userCollectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0,NavBarHeight, SCREEN_WIDTH,27+9*2) collectionViewLayout:layout1]; _userCollectionView.indicatorStyle=UIScrollViewIndicatorStyleWhite; [_userCollectionView registerClass:[KBRollingCollectionViewCell class] forCellWithReuseIdentifier:@"KBRollingCollectionViewCell"]; _userCollectionView.delegate=self; _userCollectionView.dataSource=self; _userCollectionView.backgroundColor = [UIColor clearColor]; _userCollectionView.showsHorizontalScrollIndicator=NO; } return _userCollectionView; } #pragma mark collection代理 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.userArray.count; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBRollingCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"KBRollingCollectionViewCell" forIndexPath:indexPath]; KBSignInRollingUser *model=self.userArray[indexPath.item]; cell.model=model; return cell; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 10, 0, 0); } -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat cellWidth = [self.collectionCellWithArry[indexPath.row] floatValue]; return CGSizeMake(cellWidth, self.userCollectionView.frame.size.height); } - (CGFloat)getWidhWithString:(NSString *)string withFontSize:(CGFloat)fontSize { NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}; CGSize textSize = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size; return textSize.width; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if([scrollView isEqual:self.userCollectionView]) { if(scrollView.contentSize.width>SCREEN_WIDTH){ if(scrollView.contentOffset.x>=scrollView.contentSize.width){ [scrollView setContentOffset:CGPointMake(-SCREEN_WIDTH, 0)]; } } } } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if (self.usertimer) { [self.usertimer invalidate]; self.usertimer=nil; } } - (void)jumpToTaobaoWithUrl:(NSString *)url parentVc:(UIViewController *)parentVc{ id page = [AlibcTradePageFactory page:url]; //淘客信息 AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init]; taoKeParams.pid= ALTK_PID; //打开方式 AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init]; showParam.openType = AlibcOpenTypeNative; [[AlibcTradeSDK sharedInstance].tradeService show:parentVc page:page showParams:showParam taoKeParams:taoKeParams trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) { } tradeProcessFailedCallback:^(NSError * _Nullable error) { }]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end