Files
yinmeng-ios/xplan-ios/Main/Home/View/SubViews/CycleVerticalView/XPCycleVerticalView.m
2023-03-16 14:34:53 +08:00

185 lines
5.3 KiB
Objective-C

//
// XPCycleVerticalView.m
// xplan-ios
//
// Created by XY on 2023/3/7.
//
#import "XPCycleVerticalView.h"
#import "XPCycleView.h"
#import "XPHomeGiftRecordModel.h"
@interface XPCycleVerticalView () {
CGRect _topRect; //上View
CGRect _middleRect;//中View
CGRect _btmRect; //下View
double _showTime; //timer循环时长
double _animationTime;//动画时长
UIButton *_button; //按钮
NSTimer *_timer; //计时器
NSInteger _indexNow; //计数器
}
@property (strong, nonatomic) XPCycleView *view1;
@property (strong, nonatomic) XPCycleView *view2;
@property (strong, nonatomic) XPCycleView *tmpTopView;
@property (strong, nonatomic) XPCycleView *tmpMiddleView;
@property (strong, nonatomic) XPCycleView *tmpBtmView;
@end
@implementation XPCycleVerticalView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_showTime = 3;
_animationTime = 0.3;
[self initUI];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
_showTime = 3;
_animationTime = 0.3;
[self initUI];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
_middleRect = self.bounds;
_topRect = CGRectMake(0, -CGRectGetHeight(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
_btmRect = CGRectMake(0, CGRectGetHeight(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
self.view1.frame = _middleRect;
self.view2.frame = self.direction == XPCycleVerticalViewScrollDirectionDown ? self->_topRect : self->_btmRect;
_button.frame = _middleRect;
}
- (void)initUI {
self.view1 = [[XPCycleView alloc]init];
// self.view1.backgroundColor = [UIColor clearColor];
self.view2 = [[XPCycleView alloc]init];
// self.view2.backgroundColor = [UIColor clearColor];
[self addSubview:self.view1];
[self addSubview:self.view2];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.backgroundColor = [UIColor clearColor];
[_button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button];
self.clipsToBounds = YES;
}
// 执行动画
- (void)executeAnimation {
[self setViewInfo];
[UIView animateWithDuration:_animationTime delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.tmpMiddleView.frame = self.direction == XPCycleVerticalViewScrollDirectionDown ? self->_btmRect : self->_topRect;
if (self->_direction == XPCycleVerticalViewScrollDirectionDown) {
self.tmpTopView.frame = self->_middleRect;
} else {
self.tmpBtmView.frame = self->_middleRect;
}
}completion:^(BOOL finished) {
self.tmpMiddleView.frame = self->_direction == XPCycleVerticalViewScrollDirectionDown ? self->_topRect : self->_btmRect;
if (self->_indexNow < self.dataArray.count - 1) {
self->_indexNow ++;
}else{
self->_indexNow = 0;
}
}];
}
#pragma mark - set
- (void)setDirection:(XPCycleVerticalViewScrollDirection)direction {
_direction = direction;
self.view2.frame = _direction == XPCycleVerticalViewScrollDirectionDown ? _topRect : _btmRect;
}
- (void)setDataArray:(NSArray<XPHomeGiftRecordModel*> *)dataArray {
if (dataArray.count == 0) {
XPHomeGiftRecordModel *model = [[XPHomeGiftRecordModel alloc] init];
model.isEmpty = YES;
_dataArray = @[model];
}else{
_dataArray = dataArray;
}
if (![_timer isValid]) {
_indexNow = 0;
[self startAnimation];
}
}
- (void)setViewInfo {
if (_direction == XPCycleVerticalViewScrollDirectionDown) {
if (self.view1.frame.origin.y == 0) {
_tmpMiddleView = self.view1;
_tmpTopView = self.view2;
} else {
_tmpMiddleView = self.view2;
_tmpTopView = self.view1;
}
} else {
if (self.view1.frame.origin.y == 0) {
_tmpMiddleView = self.view1;
_tmpBtmView = self.view2;
} else {
_tmpMiddleView = self.view2;
_tmpBtmView = self.view1;
}
}
_tmpMiddleView.infoModel = _dataArray[_indexNow%(_dataArray.count)];
if(_dataArray.count > 1){
if (_direction == XPCycleVerticalViewScrollDirectionDown) {
_tmpTopView.infoModel = _dataArray[(_indexNow+1)%(_dataArray.count)];
} else {
_tmpBtmView.infoModel = _dataArray[(_indexNow+1)%(_dataArray.count)];
}
}
}
- (void)startAnimation {
[self setViewInfo];
if (_dataArray.count > 1) {
[self stopTimer];
_timer = [NSTimer scheduledTimerWithTimeInterval:_showTime target:self selector:@selector(executeAnimation) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:UITrackingRunLoopMode];
}
}
- (void)stopAnimation {
[self stopTimer];
[self.layer removeAllAnimations];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
#pragma mark -
- (void)stopTimer {
if(_timer){
if([_timer isValid]){
[_timer invalidate];
}
_timer = nil;
}
}
- (void)btnClick {
if (self.block) {
self.block(_indexNow);
}
}
@end