ifish/Ifish/controllers/IfishTabControllers/设备/QianDao/View/QianDaoGiftView.m

159 lines
6.2 KiB
Objective-C

//
// QianDaoGiftView.m
// Ifish
//
// Created by imac on 17/3/7.
// Copyright © 2017年 lianlian. All rights reserved.
//
#import "QianDaoGiftView.h"
#import "IfishUserObsever.h"
@interface QianDaoGiftView()
@property (nonatomic,strong) UIImageView *giftView;
@property (nonatomic,strong) UIButton *giftBtn;
@property (nonatomic,strong) UILabel *addGoldlabel;
@property (nonatomic,strong) UILabel *firstLabe;
@property (nonatomic) NSInteger goldValue;
@end
@implementation QianDaoGiftView
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self creatGiftImg];
UIView *holdView =[[UIView alloc] initWithFrame:frame];
holdView.backgroundColor = [UIColor blackColor];
holdView.alpha = 0.3;
[self addSubview:holdView];
[self bringSubviewToFront:self.giftBtn];
[self insertSubview:self.giftView belowSubview:self.giftBtn];
[self insertSubview:holdView belowSubview:self.giftView];
}
return self;
}
-(void)creatGiftImg{
self.giftView = [[UIImageView alloc] init ];
self.giftView.userInteractionEnabled = YES;
[self addSubview:self.giftView];
self.giftView.image = [UIImage imageNamed:@"signin_popup_egg"];
self.firstLabe = [[UILabel alloc] init];
self.firstLabe.textColor = [UIColor whiteColor];
self.firstLabe.text = @"恭喜您!";
self.firstLabe.textAlignment = NSTextAlignmentCenter;
self.firstLabe.font =[UIFont systemFontOfSize:13];
self.firstLabe.hidden = YES;
[self.giftView addSubview:self.firstLabe];
_goldValue = 0;
self.addGoldlabel = [[UILabel alloc] init];
self.addGoldlabel.textColor = [UIColor whiteColor];
self.addGoldlabel.textAlignment = NSTextAlignmentCenter;
self.addGoldlabel.attributedText = [self getAddGloldText:_goldValue];
self.addGoldlabel.hidden = YES;
[self.giftView addSubview:self.addGoldlabel];
self.giftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:self.giftBtn];
[self.giftBtn setBackgroundImage:[UIImage imageNamed:@"signin_popup_egg_button"] forState:UIControlStateNormal];
[self.giftBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
self.giftBtn.selected = NO;
[UIView animateWithDuration:0.36 delay:0 usingSpringWithDamping:25 initialSpringVelocity:20 options:UIViewAnimationOptionCurveEaseIn animations:^{
CGFloat imgW = self.frame.size.width - 53*2*KWidth_Scale;
self.giftView.frame = CGRectMake(0, 0, imgW, imgW * 1.04);
self.giftView.center = self.center;
self.firstLabe.frame = CGRectMake(0, 30*KWidth_Scale, imgW, 13);
self.addGoldlabel.frame = CGRectMake(0,CGRectGetMaxY(self.firstLabe.frame) +8*KWidth_Scale, imgW, 18);
CGFloat btnH = (imgW -51*2) * 0.21;
self.giftBtn.frame = CGRectMake(CGRectGetMinX(self.giftView.frame) + 51,CGRectGetMaxY(self.giftView.frame) - 19 - btnH,imgW -51*2, btnH);
} completion:^(BOOL finished){
}];
}
-(NSMutableAttributedString*)getAddGloldText:(NSInteger)goldValue{
NSString *gold =[NSString stringWithFormat:@"[%ld个金币]",(long)goldValue];
NSString * goldAllStr = [NSString stringWithFormat:@"获得了%@",gold];
NSRange range = [goldAllStr rangeOfString:gold];
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:goldAllStr];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255.0/255.0 green:218.0/255.0 blue:128.0/255.0 alpha:1] range:range];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255.0/255.0 green:147.0/255.0 blue:81.0/255.0 alpha:1] range:NSMakeRange(3, 1)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255.0/255.0 green:147.0/255.0 blue:81.0/255.0 alpha:1] range:NSMakeRange(goldAllStr.length -1,1)];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 3)];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:range];
return string;
}
-(void)btnAction:(UIButton *)btn{
if (btn.selected) {
[self removeFromSuperview];
}else{
[self hitGoldEgg:btn];
}
}
-(void)hitGoldEgg:(UIButton*)btn{
// self.giftView.image = [UIImage imageNamed:@"signin_popup_obtain"];
// [self.giftBtn setBackgroundImage:[UIImage imageNamed:@"signin_popup_obtain_button"] forState:UIControlStateNormal];
// self.firstLabe.hidden = NO;
// self.addGoldlabel.hidden = NO;
// return;
NSString *userId =[dataContorl dataControlGetUserIdInfo];
__weak typeof (self) weakself = self;
[AFHttpTool ifishHitGoldenEgg:userId success:^(id response) {
NSDictionary *reDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
if ([reDic[@"result"] isEqualToString:@"100"]){
NSDictionary *data=reDic[@"data"];
if (![data isKindOfClass:[NSNull class]]&&data) {
IfishUserAsset *userAsset = [[IfishUserAsset alloc] initWithDict:data];
[dataContorl resetUserAsset:userAsset];
//加金币
//if (weakself.getGiftBlock) {
// weakself.getGiftBlock(addGoldValue);
//}
weakself.giftView.image = [UIImage imageNamed:@"signin_popup_obtain"];
[weakself.giftBtn setBackgroundImage:[UIImage imageNamed:@"signin_popup_obtain_button"] forState:UIControlStateNormal];
weakself.giftBtn.selected = YES;
weakself.firstLabe.hidden = NO;
weakself.addGoldlabel.hidden = NO;
_goldValue =userAsset.addValue;
weakself.addGoldlabel.attributedText = [self getAddGloldText:_goldValue];
//加金币 tost
NSString *addvalue = [NSString stringWithFormat:@"%ld",(long)userAsset.addValue];
[[IfishUserObsever sharedInstance] showAddGoldWith:addvalue];
}
}
} failure:^(NSError *err) {
}];
}
@end