zoukankan      html  css  js  c++  java
  • iOS实现渐变颜色

    下面是我的两种实现:

    1.直接图片展示,注意图片的变形问题;

    2.用CAGradientLayer渐变颜色实现;

    代码如下:

    //
    //  ViewController.m
    //  ImageStrenchDemo
    //
    //  Created by 思 彭 on 17/3/14.
    //  Copyright © 2017年 思 彭. All rights reserved.
    //
    
    #import "ViewController.h"
    
    // rgb颜色转换(16进制->10进制)
    #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIImageView *headImgViewBg = [[UIImageView alloc] initWithFrame:self.view.bounds];
        [headImgViewBg setImage:[UIImage imageNamed:@"bj"]];
        [self.view addSubview:headImgViewBg];
        [self setupUI];
        [self setupImage];
        
    }
    
    // 图片实现渐变
    - (void)setupImage {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(100, 300, 300, 40);    
        UIImage *image1 = [UIImage imageNamed:@"2"];
        UIImage *resizeImage = [image1 resizableImageWithCapInsets:UIEdgeInsetsMake(image1.size.height*0.5, image1.size.width*0.3, image1.size.height*0.5, image1.size.width*0.5)resizingMode:UIImageResizingModeStretch];
        [button setBackgroundImage:resizeImage forState:UIControlStateNormal ];
        [button setTitle:@"dcevbvhvjrvrjvn" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.view addSubview:button];
    }
    
    // 颜色值实现渐变
    - (void)setupUI {
        
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(30, 100, 300, 40)];
        UIImageView *headImgViewBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
        bgView.layer.masksToBounds = YES;
        bgView.layer.cornerRadius = 20;
        [bgView addSubview:headImgViewBg];
        [self.view addSubview:bgView];
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = headImgViewBg.frame;
        gradient.colors = [NSArray arrayWithObjects:(id)UIColorFromRGB(0x752ccd).CGColor,
                           (id)UIColorFromRGB(0xcd1f87).CGColor,nil];
        gradient.startPoint = CGPointMake(0, 0.5);
        gradient.endPoint = CGPointMake(1, 0.5);
        [headImgViewBg.layer insertSublayer:gradient atIndex:0];
    }
    
    @end
  • 相关阅读:
    lunix下的redis数据库操作——set集合
    lunix下的redis数据库操作——hash(哈希)
    lunix下的redis数据库操作——list列表
    python操作mysql
    linux修改mysql表结构
    关于wordclou的一些简单操作
    SQL NOW() 函数:返回当前系统的日期和时间
    SQL ROUND() 函数:把数值字段四舍五入为指定的小数位数
    SQL LEN() 函数:返回文本字段中值的长度
    SQL MID() 函数:从文本字段中提取字符
  • 原文地址:https://www.cnblogs.com/pengsi/p/6548390.html
Copyright © 2011-2022 走看看