一個變化字體顏色的demo

原網址:http://www.itdecent.cn/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; // 層的 邊界的顏色
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的代碼

// 創(chuàng)建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;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,063評論 25 709
  • 發(fā)現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,383評論 4 61
  • 背西步豪邁,東望日升騰。 促喘因提速,驅寒乃汗蒸。 跑來樓宇下,登上最高層。 所愛涼州地,雄名早著稱。
    雪窗_武立之閱讀 188評論 6 1

友情鏈接更多精彩內容