GiGaMaskTime/GIGA/Modules/Mask/View/MaskTimeTopAnimationView.m

157 lines
5.5 KiB
Objective-C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MaskTimeTopAnimationView.m
// GIGA
//
// Created by lianxiang on 2018/9/26.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "MaskTimeTopAnimationView.h"
#define DEALY_WHEN_TITLE_IN_MIDDLE 5.0
#define DEALY_WHEN_TITLE_IN_BOTTOM 0.0
typedef NS_ENUM(NSUInteger, GYTitlePosition) {
GYTitlePositionTop = 1,
GYTitlePositionMiddle = 2,
GYTitlePositionBottom = 3
};
@interface MaskTimeTopAnimationView()
@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) NSArray *contentsAry;
@property (nonatomic, assign) CGPoint topPosition;
@property (nonatomic, assign) CGPoint middlePosition;
@property (nonatomic, assign) CGPoint bottomPosition;
/*
*1.控制延迟时间当文字在中间时延时时间长一些如5秒这样可以让用户浏览清楚内容
*2.当文字隐藏在底部的时候,不需要延迟,这样衔接才流畅;
*3.通过上面的宏定义去更改需要的值
*/
@property (nonatomic, assign) CGFloat needDealy;
@property (nonatomic, assign) NSInteger currentIndex; /*当前播放到那个标题了*/
@property (nonatomic, assign) BOOL shouldStop; /*是否停止*/
@end
@implementation MaskTimeTopAnimationView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//self.backgroundColor = [UIColor whiteColor];
self.topPosition = CGPointMake(self.frame.size.width/2 -22, -self.frame.size.height/2);
self.middlePosition = CGPointMake(self.frame.size.width/2 - 22, self.frame.size.height/2);
self.bottomPosition = CGPointMake(self.frame.size.width/2 -22 , self.frame.size.height/2*3);
self.shouldStop = NO;
_textLabel = [[UILabel alloc] init];
//_textLabel.backgroundColor = [UIColor blackColor];
_textLabel.layer.bounds = CGRectMake(0,0, CGRectGetWidth(frame), CGRectGetHeight(frame));
_textLabel.layer.position = self.middlePosition;
_textLabel.textAlignment = NSTextAlignmentCenter;
_textLabel.textColor = [UIColor whiteColor];
if (KMainW < 375) {
_textLabel.font = GIGA_TEXTFONTMEDIUM(10);
}else{
_textLabel.font = GIGA_TEXTFONTMEDIUM(12);
}
[self.layer addSublayer:_textLabel.layer];
self.clipsToBounds = NO; /*保证文字不跑出视图YES*/
self.needDealy = DEALY_WHEN_TITLE_IN_MIDDLE; /*控制第一次显示时间*/
self.currentIndex = 0;
}
return self;
}
- (void)animationWithTexts:(NSArray *)textAry {
self.contentsAry = textAry;
self.textLabel.text = [textAry objectAtIndex:0];
//[self.textLabel sizeToFit];
[self startAnimation];
}
- (void)startAnimation {
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:0.3 delay:self.needDealy options:UIViewAnimationOptionCurveEaseInOut animations:^{
if ([weakSelf currentTitlePosition] == GYTitlePositionMiddle) {
weakSelf.textLabel.layer.position = weakSelf.topPosition;
} else if ([weakSelf currentTitlePosition] == GYTitlePositionBottom) {
weakSelf.textLabel.layer.position = weakSelf.middlePosition;
}
} completion:^(BOOL finished) {
if ([weakSelf currentTitlePosition] == GYTitlePositionTop) {
weakSelf.textLabel.layer.position = weakSelf.bottomPosition;
weakSelf.needDealy = DEALY_WHEN_TITLE_IN_BOTTOM;
weakSelf.currentIndex ++;
NSString *tex = [weakSelf.contentsAry objectAtIndex:[weakSelf realCurrentIndex]];
if ([tex isEqualToString:@"面膜时间"]) {
weakSelf.textLabel.font = GIGA_TEXTFONTMEDIUM(18);
}else{
if (KMainW < 375) {
weakSelf.textLabel.font = GIGA_TEXTFONTMEDIUM(10);
}else{
weakSelf.textLabel.font = GIGA_TEXTFONTMEDIUM(12);
}
}
weakSelf.textLabel.text = tex;
} else {
weakSelf.needDealy = DEALY_WHEN_TITLE_IN_MIDDLE;
}
if (!weakSelf.shouldStop) {
[weakSelf startAnimation];
} else { //停止动画后要设置label位置和label显示内容
weakSelf.textLabel.layer.position = weakSelf.middlePosition;
NSString *tex = [weakSelf.contentsAry objectAtIndex:[weakSelf realCurrentIndex]];
if ([tex isEqualToString:@"面膜时间"]) {
weakSelf.textLabel.font = GIGA_TEXTFONTMEDIUM(18);
}else{
if (KMainW < 375) {
weakSelf.textLabel.font = GIGA_TEXTFONTMEDIUM(10);
}else{
weakSelf.textLabel.font = GIGA_TEXTFONTMEDIUM(12);
}
}
weakSelf.textLabel.text = tex;
}
}];
}
- (void)stopAnimation {
self.shouldStop = YES;
}
- (NSInteger)realCurrentIndex {
return self.currentIndex % [self.contentsAry count];
}
- (GYTitlePosition)currentTitlePosition {
if (self.textLabel.layer.position.y == self.topPosition.y) {
return GYTitlePositionTop;
} else if (self.textLabel.layer.position.y == self.middlePosition.y) {
return GYTitlePositionMiddle;
}
return GYTitlePositionBottom;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if ([self.delegate respondsToSelector:@selector(gyChangeTextView:didTapedAtIndex:)]) {
[self.delegate gyChangeTextView:self didTapedAtIndex:[self realCurrentIndex]];
}
}
//- (CGSize)intrinsicContentSize
//
//{
//
// return UILayoutFittingExpandedSize;
//
//}
@end