//
// UIView.h
//
#import <UIKit/UIKit.h>
//字体变化
#define FONT_RATE 1/8.000
//透明度变化
#define ALPHA_RATE 1/80.0000
//初始状态索引颜色
#define STR_COLOR [UIColor orangeColor]
//选中状态索引颜色
#define MARK_COLOR [UIColor blackColor]
//初始状态索引大小
#define FONT_SIZE [UIFont systemFontOfSize:10]
//索引label的tag值
#define TAG 233333
//圆的半径
#define ANIMATION_HEIGHT 80
typedef void (^MyBlock)(NSInteger);
@interface LGUIView : UIView
//动画视图(可自定义)
@property (nonatomic,strong) UILabel * animationLabel;
//索引数组
@property (nonatomic,strong) NSArray * indexArray;
//滑动回调block
@property (nonatomic,copy) MyBlock selectedBlock;
//初始数值(计算用到)
@property (nonatomic,unsafe_unretained) CGFloat number;
/**
* index滑动反馈
*/
-(void)selectIndexBlock:(MyBlock)block;
/**
* 初始化
*/
- (instancetype)initWithFrame:(CGRect)frame indexArray:(NSArray *)array;
@end
//
// UIView.m
//
#import "LGUIView.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@implementation LGUIView
// 初始化
- (instancetype)initWithFrame:(CGRect)frame indexArray:(NSArray *)array
{
self = [super initWithFrame:frame];
if (self)
{
self.indexArray = [NSArray arrayWithArray:array];
CGFloat hh = self.frame.size.height/self.indexArray.count;
for (int i = 0; i < array.count; i ++)
{
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, i * hh, self.frame.size.width, hh)];
label.text = self.indexArray[i];
label.tag = TAG + i;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = STR_COLOR;
label.font = FONT_SIZE;
[self addSubview:label];
_number = label.font.pointSize;
}
[self addSubview:self.animationLabel];
}
return self;
}
-(UILabel *)animationLabel
{
if (!_animationLabel)
{
_animationLabel = [[UILabel alloc]initWithFrame:CGRectMake(-WIDTH/2 + self.frame.size.width/2, 100, 60, 60)];
_animationLabel.layer.masksToBounds = YES;
_animationLabel.layer.cornerRadius = 5.0f;
_animationLabel.backgroundColor = [UIColor orangeColor];
_animationLabel.textColor = [UIColor whiteColor];
_animationLabel.alpha = 0;
_animationLabel.textAlignment = NSTextAlignmentCenter;
_animationLabel.font = [UIFont systemFontOfSize:18];
}
return _animationLabel;
}
-(void)panAnimationFinish
{
CGFloat hh = self.frame.size.height/self.indexArray.count;
for (int i = 0; i < self.indexArray.count; i ++)
{
UILabel * label = (UILabel *)[self viewWithTag:TAG + i];
[UIView animateWithDuration:0.2 animations:^{
label.center = CGPointMake(self.frame.size.width/2, i * hh + hh/2);
label.font = FONT_SIZE;
label.alpha = 1.0;
label.textColor = STR_COLOR;
}];
}
[UIView animateWithDuration:1 animations:^{
self.animationLabel.alpha = 0;
}];
}
-(void)panAnimationBeginWithToucher:(NSSet<UITouch *> *)touches
{
UITouch * touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGFloat hh = self.frame.size.height/self.indexArray.count;
for (int i = 0; i < self.indexArray.count; i ++)
{
UILabel * label = (UILabel *)[self viewWithTag:TAG + i];
NSLog(@"%f======%f------%f",label.center.y,point.y,fabs(label.center.y - point.y));
/* fabs(<#double#>) 取绝对值 */
/* 判断一开始点击的位置(一个范围内) */
if (fabs(label.center.y - point.y) <= ANIMATION_HEIGHT)
{
/* 在View动画的时间内改变lable的中心形成动画 */
[UIView animateWithDuration:0.2 animations:^{
/*这句是控制 索引 那一列是否有特效的*/
label.center = CGPointMake(label.bounds.size.width/2 - sqrt(fabs(ANIMATION_HEIGHT * ANIMATION_HEIGHT - fabs(label.center.y - point.y) * fabs(label.center.y - point.y))), label.center.y);
label.font = [UIFont systemFontOfSize:_number + (ANIMATION_HEIGHT - fabs(label.center.y - point.y)) * FONT_RATE];
/* 确认点击的是哪个 */
if (fabs(label.center.y - point.y) * ALPHA_RATE <= 0.08)
{
label.textColor = MARK_COLOR;
label.alpha = 1.0;
[self animationWithSection:i];
for (int j = 0; j < self.indexArray.count; j ++)
{
UILabel * label = (UILabel *)[self viewWithTag:TAG + j];
if (i != j)
{
label.textColor = STR_COLOR;
label.alpha = fabs(label.center.y - point.y) * ALPHA_RATE;
}
}
}
}];
}else
{
[UIView animateWithDuration:0.2 animations:^
{
label.center = CGPointMake(self.frame.size.width/2, i * hh + hh/2);
label.font = FONT_SIZE;
label.alpha = 1.0;
}];
}
}
}
-(void)animationWithSection:(NSInteger)section
{
self.selectedBlock(section);
_animationLabel.text = self.indexArray[section];
_animationLabel.alpha = 1.0;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self panAnimationBeginWithToucher:touches];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self panAnimationBeginWithToucher:touches];
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self panAnimationFinish];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self panAnimationFinish];
}
-(void)selectIndexBlock:(MyBlock)block
{
self.selectedBlock = block;
}
@end