Files
yinmeng-ios/xplan-ios/Main/Mine/View/SubViews/XPMineNewUserRechargeView.m
2022-08-05 17:45:17 +08:00

170 lines
5.5 KiB
Objective-C

//
// XPMineNewUserRechargeView.m
// xplan-ios
//
// Created by GreenLand on 2022/7/28.
//
#import "XPMineNewUserRechargeView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "XPMacro.h"
#import "ThemeColor.h"
#import "Timestamp.h"
///View
@interface XPMineNewUserRechargeView ()
///底部的底图
@property (nonatomic,strong) UIImageView *backImageView;
///昵称
@property (nonatomic,strong) UILabel *nickLabel;
@end
@implementation XPMineNewUserRechargeView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initEvents];
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.backImageView];
[self.backImageView addSubview:self.nickLabel];
}
- (void)initSubViewConstraints {
[self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
[self.nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.right.mas_equalTo(-6);
make.bottom.mas_equalTo(-2);
make.height.mas_equalTo(14);
}];
}
- (void)initEvents {
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(roomMiniTag:)];
pan.delaysTouchesBegan = YES;
[self addGestureRecognizer:pan];
}
#pragma mark - events
- (void)roomMiniTag:(UIPanGestureRecognizer*)p {
UIWindow *appWindow = [UIApplication sharedApplication].delegate.window;
CGPoint panPoint = [p locationInView:appWindow];
if (p.state == UIGestureRecognizerStateBegan) {
self.alpha = 1;
} else if(p.state == UIGestureRecognizerStateChanged) {
self.center = CGPointMake(panPoint.x, panPoint.y);
} else if(p.state == UIGestureRecognizerStateEnded
|| p.state == UIGestureRecognizerStateCancelled) {
self.alpha = 1;
CGFloat touchWidth = self.frame.size.width;
CGFloat touchHeight = self.frame.size.height;
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
// fabs 是取绝对值的意思
CGFloat left = fabs(panPoint.x);
CGFloat right = fabs(screenWidth - left);
CGFloat top = fabs(panPoint.y);
CGFloat bottom = fabs(screenHeight - top);
CGFloat minSpace = 0;
minSpace = MIN(MIN(MIN(top, left), bottom), right);
CGPoint newCenter;
CGFloat targetY = 0;
//校正Y
if (panPoint.y < 15 + touchHeight / 2.0) {
targetY = 15 + touchHeight / 2.0;
}else if (panPoint.y > (screenHeight - touchHeight / 2.0 - 15)) {
targetY = screenHeight - touchHeight / 2.0 - 15;
}else{
targetY = panPoint.y;
}
if (minSpace == left) {
newCenter = CGPointMake(touchWidth/2.0, targetY);
}else if (minSpace == right) {
newCenter = CGPointMake(touchWidth/2.0, targetY);
}else if (minSpace == top) {
newCenter = CGPointMake(touchWidth/2.0, touchWidth / 3);
}else {
newCenter = CGPointMake(touchWidth/2.0, screenHeight - touchWidth / 3);
}
[UIView animateWithDuration:0.25 animations:^{
if (newCenter.y + self.frame.size.height / 2 > KScreenHeight - kSafeAreaBottomHeight - 44) {
self.center = CGPointMake(newCenter.x, KScreenHeight - self.frame.size.height / 2 - kSafeAreaBottomHeight - 44);
}else {
self.center = newCenter;
}
}];
}
}
#pragma mark - Getters And Setters
- (void)setEndTime:(long)endTime {
_endTime = endTime;
[Timestamp getInternetDateWithSuccess:^(NSTimeInterval timeInterval) {
timeInterval = timeInterval * 1000;
long aTime = (endTime - timeInterval) / 1000;
[self countTimeWithTime:aTime];
} failure:^(NSError * _Nonnull error) {
NSDate *datenow = [NSDate date];//现在时间
long time2 = (long)([datenow timeIntervalSince1970]*1000);
long aTime = (endTime - time2) / 1000;
[self countTimeWithTime:aTime];
}];
}
- (void)countTimeWithTime:(long)time {
NSInteger minute;
NSInteger hour;
NSInteger day = time / 24 / 3600;
time = time - day * 24 * 3600;
hour = time / 3600;
time = time - hour * 3600;
minute = time / 60;
if (day > 0) {
if (hour > 0) {
self.nickLabel.text = [NSString stringWithFormat:@"%zd天%zd小时%zd分", day, hour, minute];
} else {
self.nickLabel.text = [NSString stringWithFormat:@"%zd天%zd分", day, minute];
}
} else {
if (hour > 0) {
self.nickLabel.text = [NSString stringWithFormat:@"%zd小时%zd分", hour, minute];
} else {
self.nickLabel.text = [NSString stringWithFormat:@"%zd分", minute];
}
}
}
- (UIImageView *)backImageView {
if (!_backImageView) {
_backImageView = [[UIImageView alloc] init];
_backImageView.image = [UIImage imageNamed:@"NewUserRecharge_entrance_bg"];
}
return _backImageView;
}
- (UILabel *)nickLabel {
if (!_nickLabel) {
_nickLabel = [[UILabel alloc] init];
_nickLabel.font = [UIFont systemFontOfSize:8 weight:UIFontWeightMedium];
_nickLabel.textColor = [UIColor whiteColor];
_nickLabel.textAlignment = NSTextAlignmentCenter;
}
return _nickLabel;
}
@end