一个变化字体颜色的demo

原网址:http://www.jianshu.com/p/aeb56b51032d

自己根据作者,虚心的学习了一番。初出校园,能力还是很欠缺。跟着作者走了一遍,也学到了一些东西。

1.layer的几个小知识点:

    view.layer.shadowColor  = [UIColor blackColor].CGColor; // 阴影的颜色
    view.layer.shadowOffset = CGSizeMake(0.5, 0.5); // 阴影的偏移量
    view.layer.shadowOpacity= 1; // 阴影的不透明度
    view.layer.shadowRadius = 5; // 阴影的圆角
    view.layer.borderWidth = 1; // 层的 边界 宽度
    view.layer.borderColor = [UIColor blueColor].CGColor; // 层的 边界的颜色
一个变化字体颜色的demo_第1张图片
Simulator Screen Shot 2016年1月29日 下午3.03.51.png

2.初始化总实现步骤

  • 根据字符串数组,添加底层的label
  • 添加中间的拖动视图'midView'
  • 在midView上添加lableView,labelView中包含了一个全尺寸的label---和底层label一样大小
  • midView中调用来如下代码防止多余的label展示出来
    midView.clipsToBounds   = YES;
  • 最后一步就是在midView添加手势了,
代码如下
- (instancetype)initWithFrame:(CGRect)frame andTitleArr:(NSArray *)titleArr
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        _height = self.frame.size.height / titleArr.count;
        
        // 添加底层的label
        for (int i = 0; i < titleArr.count; i++) {
            [self addSubview:[self returnLabel:titleArr[i] andFrame:CGRectMake(0, i * _height, self.frame.size.width, _height) andColor:[UIColor blackColor]]];
        }
        _midView = ({
            UIView * midView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, _height)];
            midView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
            midView.clipsToBounds   = YES;
            midView.userInteractionEnabled  = YES;
            midView;
        });
        [self addSubview:_midView];

        // 未明白这种写法 到底是什么写法???
        _lableView = ({
            UIView * labelView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _midView.frame.size.width, _midView.frame.size.height)];
            for (int i = 0; i < titleArr.count; i++) {
                [labelView addSubview:[self returnLabel:titleArr[i] andFrame:CGRectMake(0, i * _height, self.frame.size.width, _height) andColor:[UIColor whiteColor]]];
            }
            labelView;
        });
        [_midView addSubview:_lableView];
        
        // 添加拖动手势
        UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
        [_midView addGestureRecognizer:pan];
    }
    return self;
}

3.手势的学习

作为一个小心手真的手势在我面前都是那么的新鲜,
- (void)pan:(UIPanGestureRecognizer*)sender
{
    CGPoint pt = [sender translationInView:self];
    
    CGPoint midCenter   = _midView.center;
    CGPoint labelCenter = _lableView.center;
    
    midCenter.y     += pt.y;
    labelCenter.y   -= pt.y;
    
    // 边界
    if (midCenter.y > self.frame.size.height - _height/2.0) {
        midCenter.y = self.frame.size.height - _height/2.0;
        labelCenter.y = -self.frame.size.height + _height * 1.5;
    }
    if (midCenter.y < _height/2.0) {
        midCenter.y     = _height/2.0;
        labelCenter.y   = _height/2.0;
    }
    
    _midView.center = midCenter;
    _lableView.center = labelCenter;
    
    [sender setTranslation:CGPointZero inView:self]; 
}

这边是label的代码

// 创建label
- (UILabel *)returnLabel:(NSString*)title andFrame:(CGRect)frame andColor:(UIColor*)color
{
    UILabel * label = [[UILabel alloc]initWithFrame:frame];
    label.font      = [UIFont systemFontOfSize:20.f];
    label.textColor = color;
    label.text      = title;
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

你可能感兴趣的:(一个变化字体颜色的demo)