Files
yinmeng-ios/xplan-ios/Main/Home/View/SubViews/CycleVerticalView/XPCycleVerticalView.m

178 lines
5.1 KiB
Mathematica
Raw Normal View History

2023-03-07 15:13:07 +08:00
//
2023-03-07 20:14:49 +08:00
// XPCycleVerticalView.m
// xplan-ios
2023-03-07 15:13:07 +08:00
//
2023-03-07 20:14:49 +08:00
// Created by XY on 2023/3/7.
2023-03-07 15:13:07 +08:00
//
2023-03-07 20:14:49 +08:00
#import "XPCycleVerticalView.h"
#import "XPCycleView.h"
@interface XPCycleVerticalView () {
CGRect _topRect; //View
CGRect _middleRect;//View
CGRect _btmRect; //View
double _showTime; //timer
double _animationTime;//
UIButton *_button; //
NSTimer *_timer; //
NSInteger _indexNow; //
2023-03-07 15:13:07 +08:00
}
2023-03-07 20:14:49 +08:00
@property (strong, nonatomic) XPCycleView *view1;
@property (strong, nonatomic) XPCycleView *view2;
2023-03-07 15:13:07 +08:00
2023-03-07 20:14:49 +08:00
@property (strong, nonatomic) XPCycleView *tmpTopView;
@property (strong, nonatomic) XPCycleView *tmpMiddleView;
@property (strong, nonatomic) XPCycleView *tmpBtmView;
2023-03-07 15:13:07 +08:00
@end
2023-03-07 20:14:49 +08:00
@implementation XPCycleVerticalView
- (instancetype)initWithFrame:(CGRect)frame {
2023-03-07 15:13:07 +08:00
self = [super initWithFrame:frame];
if (self) {
_showTime = 3;
_animationTime = 0.3;
[self initUI];
}
return self;
}
2023-03-07 20:14:49 +08:00
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
2023-03-07 15:13:07 +08:00
self = [super initWithCoder:aDecoder];
if (self) {
_showTime = 3;
_animationTime = 0.3;
[self initUI];
}
return self;
}
2023-03-07 20:14:49 +08:00
- (void)layoutSubviews {
2023-03-07 15:13:07 +08:00
[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;
2023-03-07 20:14:49 +08:00
self.view2.frame = self.direction == XPCycleVerticalViewScrollDirectionDown ? self->_topRect : self->_btmRect;
2023-03-07 15:13:07 +08:00
_button.frame = _middleRect;
}
2023-03-07 20:14:49 +08:00
- (void)initUI {
self.view1 = [[XPCycleView alloc]init];
// self.view1.backgroundColor = [UIColor clearColor];
self.view2 = [[XPCycleView alloc]init];
// self.view2.backgroundColor = [UIColor clearColor];
2023-03-07 15:13:07 +08:00
[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;
}
//
2023-03-07 20:14:49 +08:00
- (void)executeAnimation {
2023-03-07 15:13:07 +08:00
[self setViewInfo];
[UIView animateWithDuration:_animationTime delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
2023-03-07 20:14:49 +08:00
self.tmpMiddleView.frame = self.direction == XPCycleVerticalViewScrollDirectionDown ? self->_btmRect : self->_topRect;
if (self->_direction == XPCycleVerticalViewScrollDirectionDown) {
2023-03-07 15:13:07 +08:00
self.tmpTopView.frame = self->_middleRect;
} else {
self.tmpBtmView.frame = self->_middleRect;
}
}completion:^(BOOL finished) {
2023-03-07 20:14:49 +08:00
self.tmpMiddleView.frame = self->_direction == XPCycleVerticalViewScrollDirectionDown ? self->_topRect : self->_btmRect;
2023-03-07 15:13:07 +08:00
if (self->_indexNow < self.dataArray.count - 1) {
self->_indexNow ++;
}else{
self->_indexNow = 0;
}
}];
}
#pragma mark - set
2023-03-07 20:14:49 +08:00
- (void)setDirection:(XPCycleVerticalViewScrollDirection)direction {
2023-03-07 15:13:07 +08:00
_direction = direction;
2023-03-07 20:14:49 +08:00
self.view2.frame = _direction == XPCycleVerticalViewScrollDirectionDown ? _topRect : _btmRect;
2023-03-07 15:13:07 +08:00
}
2023-03-07 20:14:49 +08:00
- (void)setDataArray:(NSArray *)dataArray {
2023-03-07 15:13:07 +08:00
_dataArray = dataArray;
if (![_timer isValid]) {
_indexNow = 0;
[self startAnimation];
}
}
2023-03-07 20:14:49 +08:00
- (void)setViewInfo {
if (_direction == XPCycleVerticalViewScrollDirectionDown) {
2023-03-07 15:13:07 +08:00
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;
}
}
2023-03-07 20:14:49 +08:00
_tmpMiddleView.infoModel = _dataArray[_indexNow%(_dataArray.count)];
2023-03-07 15:13:07 +08:00
if(_dataArray.count > 1){
2023-03-07 20:14:49 +08:00
if (_direction == XPCycleVerticalViewScrollDirectionDown) {
_tmpTopView.infoModel = _dataArray[(_indexNow+1)%(_dataArray.count)];
2023-03-07 15:13:07 +08:00
} else {
2023-03-07 20:14:49 +08:00
_tmpBtmView.infoModel = _dataArray[(_indexNow+1)%(_dataArray.count)];
2023-03-07 15:13:07 +08:00
}
}
}
2023-03-07 20:14:49 +08:00
- (void)startAnimation {
2023-03-07 15:13:07 +08:00
[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];
}
}
2023-03-07 20:14:49 +08:00
- (void)stopAnimation {
2023-03-07 15:13:07 +08:00
[self stopTimer];
[self.layer removeAllAnimations];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
#pragma mark -
2023-03-07 20:14:49 +08:00
- (void)stopTimer {
2023-03-07 15:13:07 +08:00
if(_timer){
if([_timer isValid]){
[_timer invalidate];
}
_timer = nil;
}
}
2023-03-07 20:14:49 +08:00
- (void)btnClick {
2023-03-07 15:13:07 +08:00
if (self.block) {
self.block(_indexNow);
}
}
@end
2023-03-07 20:14:49 +08:00