Files
fanyin-ios/QXLive/Tabbar/TabbarCustomView/DrayView.m
2025-08-12 14:27:12 +08:00

206 lines
6.6 KiB
Objective-C
Executable File

//
// DrayView.m
// SoundRiver
//
// Created by apple on 2020/1/18.
//
#import "DrayView.h"
@interface DrayView ()
@property (nonatomic, strong) UIImageView *bgImgView;
@property (nonatomic, strong) UIButton *tapBtn;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UIView *lineView;
@end
@implementation DrayView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//添加拖拽手势-改变控件的位置
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changePostion:)];
[self addGestureRecognizer:pan];
[self setHidden:YES];
[self addSubview:self.bgImgView];
[self addSubview:self.animationView];
[self addSubview:self.tapBtn];
[self addSubview:self.closeBtn];
[self addSubview:self.lineView];
[self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
[self.animationView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(2);
make.centerY.equalTo(self);
make.width.height.mas_equalTo(46);
}];
[self.tapBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.animationView);
}];
[self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(70);
make.centerY.equalTo(self);
make.width.height.mas_equalTo(25);
}];
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.equalTo(self).inset(15);
make.left.mas_equalTo(60);
make.width.mas_offset(1);
}];
}
return self;
}
#pragma mark - Btn Click
- (void)onTapBtnClick:(UIButton *)btn {
if (self.onGotoRoomBtnCallBack) {
self.onGotoRoomBtnCallBack();
}
}
- (void)onLeaveRoomBtnClick:(UIButton *)btn {
if (self.onLeaveRoomBtnCallBack) {
self.onLeaveRoomBtnCallBack();
}
}
#pragma mark - Getter
- (UIImageView *)bgImgView {
if (!_bgImgView) {
_bgImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_live_room_play_bg_tip"]];
}
return _bgImgView;
}
- (FLImageView *)animationView {
if (!_animationView) {
_animationView = [[FLImageView alloc] init];
_animationView.contentMode = UIViewContentModeScaleAspectFill;
_animationView.layer.masksToBounds = YES;
_animationView.layer.cornerRadius = 23;
}
return _animationView;
}
- (UIButton *)tapBtn {
if (!_tapBtn) {
_tapBtn = [[UIButton alloc] init];
// _tapBtn.needEventInterval = 0.5;
// [_tapBtn hx_setEnlargeEdgeWithTop:5 right:5 bottom:5 left:5];
[_tapBtn addTarget:self action:@selector(onTapBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _tapBtn;
}
- (UIButton *)closeBtn {
if (!_closeBtn) {
_closeBtn = [[UIButton alloc] init];
[_closeBtn setImage:[UIImage imageNamed:@"tabbar_live_close_btn"] forState:UIControlStateNormal];
// _closeBtn.needEventInterval = 0.5;
// [_closeBtn hx_setEnlargeEdgeWithTop:5 right:5 bottom:5 left:5];
[_closeBtn addTarget:self action:@selector(onLeaveRoomBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _closeBtn;
}
- (UIView *)lineView {
if (!_lineView) {
_lineView = [[UIView alloc] init];
_lineView.backgroundColor = UIColor.whiteColor;
}
return _lineView;
}
#pragma mark - Other
// 靠右边
- (void)keepRightViewStatus {
self.bgImgView.transform = CGAffineTransformIdentity;
[self.animationView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(2);
make.centerY.equalTo(self);
make.width.height.mas_equalTo(46);
}];
[self.closeBtn mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(70);
}];
[self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(60);
}];
}
// 靠左边
- (void)keepLeftViewStatus {
self.bgImgView.transform = CGAffineTransformMakeRotation(M_PI);
[self.animationView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-2);
make.centerY.equalTo(self);
make.width.height.mas_equalTo(46);
}];
[self.closeBtn mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(5);
}];
[self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(35);
}];
}
#pragma mark - UIPanGestureRecognizer
- (void)changePostion:(UIPanGestureRecognizer *)pan{
CGPoint point = [pan translationInView:self];
CGRect originalFrame = self.frame;
originalFrame = [self changeXWithFrame:originalFrame point:point];
originalFrame = [self changeYWithFrame:originalFrame point:point];
self.frame = originalFrame;
[pan setTranslation:CGPointZero inView:self];
UIButton *button = (UIButton *)pan.view;
if (pan.state == UIGestureRecognizerStateBegan) {
button.enabled = NO;
}else if (pan.state == UIGestureRecognizerStateChanged){
} else {
CGRect frame = self.frame;
if (self.center.x <= SCREEN_WIDTH / 2.0){
frame.origin.x = 0;
[self keepLeftViewStatus];
}else {
frame.origin.x = SCREEN_WIDTH - frame.size.width;
[self keepRightViewStatus];
}
if (frame.origin.y < NavContentHeight) {
frame.origin.y = NavContentHeight;
} else if (frame.origin.y + frame.size.height > SCREEN_HEIGHT - NavContentHeight - TabbarHeight) {
frame.origin.y = SCREEN_WIDTH - NavContentHeight - TabbarHeight - frame.size.height;
}
[UIView animateWithDuration:0.3 animations:^{
self.frame = frame;
}];
button.enabled = YES;
}
}
//拖动改变控件的水平方向x值
- (CGRect)changeXWithFrame:(CGRect)originalFrame point:(CGPoint)point{
BOOL q1 = originalFrame.origin.x >= 0;
BOOL q2 = originalFrame.origin.x + originalFrame.size.width <= SCREEN_WIDTH;
if (q1 && q2) {
originalFrame.origin.x += point.x;
}
return originalFrame;
}
//拖动改变控件的竖直方向y值
- (CGRect)changeYWithFrame:(CGRect)originalFrame point:(CGPoint)point{
BOOL q1 = originalFrame.origin.y >= 20;
BOOL q2 = originalFrame.origin.y + originalFrame.size.height <= SCREEN_HEIGHT;
if (q1 && q2) {
originalFrame.origin.y += point.y;
}
return originalFrame;
}
@end