UITableView 自定义的索引View点击放大动画

本文介绍了一个自定义的UIView类LGUIView,该类实现了基于触摸的动画效果,包括索引项滑动反馈及动画视图展示。文章详细解释了如何创建索引标签并为它们添加交互效果,同时提供了动画实现的具体代码。

//

//  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]

//索引labeltag

#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




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值