葛爷带你上iOS王者——01

来源:互联网 发布:linux samba服务器搭建 编辑:程序博客网 时间:2024/06/09 18:45

git的安装及使用。

安装过程无脑,在此不多说,百度一大堆,不过有一个git的教学网站还不错,推荐下http://backlogtool.com/git-guide/cn/intro/intro2_1.html

项目原型的设计

在搭原型之前看业务就不多废话了,开始设计界面原型之前,一直以为不就是界面控件的拖拉吗?这还不简单。。呵呵,被自己蠢哭了。期间的问题让俺慢慢陈述。创建一个项目,名字取好,直奔storyboard(一下都使用sb),用navigation controller作为启动项直接把那个箭头的东西拉过来就ok了,在首页设计滚动的banner中想直接在sb上实现不去弄代码其实是不太可能,反正是没找到,然后用代码来实现可以循环滚动的imageview,

1.滚动的scrollview

代码如下:

<span style="font-size:14px;"><span style="font-size:12px;">@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;@property (nonatomic) NSMutableArray *imageArray;- (NSMutableArray*)imageArray{    if (!_imageArray)    {        _imageArray = [NSMutableArray array];    }        return _imageArray;}- (void)viewDidLoad {    [super viewDidLoad];        for (NSInteger i = 0; i < 5; i++) {        NSString *imageName = [NSString stringWithFormat:@"img%@.jpg",@(i)];        UIImage *image = [UIImage imageNamed:imageName];        [self.imageArray addObject:image];    }        self.scrollView.pagingEnabled = YES;    self.scrollView.delegate = self;    self.scrollView.showsVerticalScrollIndicator = NO;    self.scrollView.showsHorizontalScrollIndicator = NO;    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*self.imageArray.count, self.scrollView.frame.size.height);        for (NSInteger i = 0; i < self.imageArray.count; i++) {        UIImageView *imageView = [[UIImageView alloc] initWithImage:self.imageArray[i]];        imageView.frame = CGRectMake(i*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);        imageView.tag = i+1;        [self.scrollView addSubview:imageView];    }    }- (void)scrollViewDidScroll:(UIScrollView *)scrollView{    CGRect bounds = scrollView.bounds;    CGFloat maxX = CGRectGetMaxX(bounds);    CGFloat minX = CGRectGetMinX(bounds);        NSInteger firstIndex = minX;    NSInteger lastIndex = (NSInteger)floorf(maxX / bounds.size.width);        NSInteger left = (NSInteger)maxX % (NSInteger)bounds.size.width;    if (lastIndex == self.imageArray.count && left > 0)    {        for (NSInteger i = 0; i<self.imageArray.count-1; i++)        {            [self.imageArray exchangeObjectAtIndex:i withObjectAtIndex:self.imageArray.count-1];        }                        [self.scrollView setContentOffset:CGPointZero];        NSArray *subs = self.scrollView.subviews;        for (UIImageView *imageV in subs) {            imageV.image = self.imageArray[imageV.tag-1];        }            }        if (firstIndex < 0)    {        for (NSInteger i = self.imageArray.count-1; i>0; i--)        {            [self.imageArray exchangeObjectAtIndex:0 withObjectAtIndex:i];        }                [self.scrollView setContentOffset:CGPointMake((self.imageArray.count -1)*self.scrollView.frame.size.width, 0)];        NSArray *subs = self.scrollView.subviews;        for (UIImageView *imageV in subs) {            imageV.image = self.imageArray[imageV.tag-1];        }            }}</span></span>

2.图片的模糊处理

图片的模糊处理的方式比较多吧,在此用的是UI view的模糊处理方式,直接调用类方法代码如下:

GradientView * gView1 = [[GradientViewalloc]initWithFrame:CGRectMake(0,0,self.view1.frame.size.width,self.view1.frame.size.height)type:TRANSPARENT_GRADIENT_TYPE];

[self.view1addSubview:gView1];

<span style="font-size:14px;">#import "GradientView.h"@implementation GradientView- (id)initWithFrame:(CGRect)frame type:(int) type{    self = [super initWithFrame:frame];    if (self) {        if (type == TRANSPARENT_GRADIENT_TYPE) {            [self insertTransparentGradient];        }else if (type == COLOR_GRADIENT_TYPE){            [self insertColorGradient];        }else {                }    }    return self;    }- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // Initialization code    }    return self;}//Transparent Gradient Layer- (void) insertTransparentGradient {    UIColor *colorOne = [UIColor colorWithRed:(33/255.0)  green:(33/255.0)  blue:(33/255.0)  alpha:0.0];    UIColor *colorTwo = [UIColor colorWithRed:(33/255.0)  green:(33/255.0)  blue:(33/255.0)  alpha:1.0];    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];    NSNumber *stopOne = [NSNumber numberWithFloat:0.0];    NSNumber *stopTwo = [NSNumber numberWithFloat:1.0];    NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil];        //crate gradient layer    CAGradientLayer *headerLayer = [CAGradientLayer layer];        headerLayer.colors = colors;    headerLayer.locations = locations;    headerLayer.frame = self.bounds;        [self.layer insertSublayer:headerLayer atIndex:0];}//color gradient layer- (void) insertColorGradient {        UIColor *colorOne = [UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1.0];    UIColor *colorTwo = [UIColor colorWithRed:(33/255.0)  green:(33/255.0)  blue:(33/255.0)  alpha:1.0];        NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];    NSNumber *stopOne = [NSNumber numberWithFloat:0.0];    NSNumber *stopTwo = [NSNumber numberWithFloat:1.0];        NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil];        CAGradientLayer *headerLayer = [CAGradientLayer layer];    headerLayer.colors = colors;    headerLayer.locations = locations;    headerLayer.frame = self.bounds;        [self.layer insertSublayer:headerLayer above:0];    }</span>

3.两个视图在同一页面的切换问题

之前差不过不少资料但是还是感觉没什么效果,所以直接用切换控制器来实现切换效果。看图:



[self.navigationControllerpushViewController:vcanimated:YES];

[self.navigationControllerpopToRootViewControllerAnimated:YES];

这两句点击地图按钮跳转到地图controller的代码,而list图标的按钮所对应的控制器跳回来有所改变:

[self.navigationControllerpopToViewController:[self.navigationController.viewControllersobjectAtIndex:1]animated:YES];

[self.navigationControllerpopToRootViewControllerAnimated:YES];

动画效果没有添加上,到时候补上。


4.如上图的图标使用的是svg格式的图片,所以需要转换成UIImage然后再赋值给对应的控件。

在这边用到的是UIImage-SVG第三方的库,链接:https://github.com/Resory/UIImage-SVG 

有些朋友点进去之后和你葛爷感觉一样我靠 第三方库 这个怎么用  不是说直接导入调用的吗怎么是用CocoaPods的什么鬼命令啊?其实也不一定,将该库下载下来导入对应的类就好了,不过他使用到了别的第三方库RaptureXML,这个我们不需要管,直接导入就OK。

#import <UIImage+SVG/UIImage+SVG.h>
UIImage* image = [UIImage imageWithSVGNamed:@"Happyface"                                 targetSize:CGSizeMake(200, 200)                                  fillColor:[UIColor blueColor]];
<span style="font-size:14px;"><span style="font-size:14px;">@property (weak, nonatomic) IBOutlet UIButton *btnImage;@property (weak, nonatomic) IBOutlet UIImageView *imageView;@property (weak, nonatomic) IBOutlet UIView *viewSVG;@property (weak, nonatomic) IBOutlet UIBarButtonItem *barBtn;    UIImage * image = [UIImage imageWithSVGNamed:@"地图模式-01" targetSize:CGSizeMake(50, 50) fillColor:[UIColor blueColor]];    [self.btnImage setBackgroundImage:image forState:UIControlStateNormal];    UIImage * image2 = [UIImage imageWithSVGNamed:@"地图模式-01" targetSize:CGSizeMake(150, 150) fillColor:[UIColor greenColor]];    self.imageView.image = image2;    UIImage * image3 = [UIImage imageWithSVGNamed:@"地图模式-01" targetSize:CGSizeMake(self.viewSVG.frame.size.width, self.viewSVG.frame.size.height) fillColor:[UIColor greenColor]];    self.viewSVG.backgroundColor = [UIColor colorWithPatternImage:image3];    UIImage * image4 = [UIImage imageWithSVGNamed:@"地图模式-01" targetSize:CGSizeMake(25, 25) fillColor:[UIColor blueColor]];    [self.barBtn setImage:image4];</span></span>

嗯,就是这样,如上看到的能实用uiimage的控件都能够使用该库文件啦,比什么svgkit好用很多,个人感觉,简单实用,不过如上面所说CocoaPods是什么鬼,葛爷就慢慢跟你道来,其实安装这个东西不超过1小时,大部分时间都是在等待安置,下面的安装过程只适用于mac电脑。

5.Ruby、CocoaPods的安装及使用。

对于新入门的开发者,如何安装 Ruby和Ruby Gems 的运行环境可能会是个问题,主要介绍如何用一条靠谱的路子快速安装 Ruby 开发环境。
此安装方法同样适用于产品环境!要安装CocoaPods之前必须先配置ruby运行环境。

步骤1 - 安装系统需要的包

先安装xcode,到app store上免费下载,防止有一些木马或者病毒在程序里面,xcode会帮你安装好unix环境需要的开发包。

步骤2 - 安装 RVM

RVM 是干什么的这里就不解释了,后面你将会慢慢搞明白。

    $ curl -L https://get.rvm.io | bash -s stable

期间可能会问你sudo管理员密码,以及自动通过homebrew安装依赖包,等待一段时间后就可以成功安装好 RVM。然后,载入 RVM 环境(新开 Termal 就不用这么做了,会自动重新载入的)

    $ source ~/.rvm/scripts/rvm

检查一下是否安装正确

    $ rvm -v    rvm 1.22.17 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]

步骤3 - 用 RVM 安装 Ruby 环境  

    $ rvm install 2.0.0

同样继续等待漫长的下载,编译过程,完成以后,Ruby, Ruby Gems 就安装好了。

步骤4 - 设置 Ruby 版本

RVM 装好以后,需要执行下面的命令将指定版本的 Ruby 设置为系统默认版本

    $ rvm 2.0.0 --default

同样,也可以用其他版本号,前提是你有用 rvm install 安装过那个版本这个时候你可以测试是否正确

    $ ruby -v    ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]    $ gem -v    2.1.6

这有可能是因为Ruby的默认源使用的是cocoapods.org,国内访问这个网址有时候会有问题,网上的一种解决方案是将远替换成淘宝的,替换方式如下:
     $gem source -r https://rubygems.org/    $ gem source -a https://ruby.taobao.org

 要想验证是否替换成功了,可以执行:

    $ gem sources -l  

正常的输出结果:

      CURRENT SOURCES            

      http://ruby.taobao.org/            

到这里就已经把Ruby环境成功的安装到了Mac OS X上,接下来就是如何安装CocoaChina了。

安装CocoaPods

$sudo gem install cocoapods (需要输入管理员密码)
验证cocoapods是否安装成功
$pod

OK在此就安装好了,使用其实也很简单,首先打开Mac终端,cd切换到程序目录,如果嫌麻烦的可以配置下直接右键文件来打开终端所对应的文件目录。在下点将会提高如何配置。切到该工程目录之后,直接在终端中输入$vim Podfile后 在终端上就可以输入内容了(这不是在安装请别傻傻的等待),在Podfile文件中输入你想输入的库,到时候是可以修改的,如:

platform:ios,'7.0'
pod 'SDWwebImage',~>3.8'
输入完成,退出insert模式,按esc,然后输入:wq保存退出。

这时,项目目录会有一个podfile文件,文件内容即为刚输入的那两句话

 

在终端运行命令:$pod install (会安装Podfile里面记录的第三方库)

取决于网速的环境,安装时间可能会比较久. 注意最后一句话,from now on use ‘podsTest.xcworkspace’, 

安装完的目录结构如下:

 

多了4个文件,Pods文件夹存放你下载的第三方库.以后打开工程不再是使用xcodeproj,而是使用xcworkspace…因为你打开workspace工作空间,你发现里面包含两个xcodeproj工程文件。一个是本身的工程,一个是Pods管理第三方库的工程。Podfile文件就是之前的Podfile文件,保存需要添加的第三方库。还有一个Podfile文件用来保存已经安装的第三方库的版本号。该文件对多人开发来说非常有用,团队中某用户check这份代码后,执行pod install命令得到的是与其它团队成员一致的第三方库版本。如果没有该文件,则会得到最新版本,与其他团队成员版本号不一致,这会为项目带来很大的困扰。

正确编译运行一个包含cocoapods类库的第三方库(就是你要用的第三方库又包含依赖其它的第三方库采用cocoapods管理)

进入项目所在目录,输入命令:$pod update  (将文件中的所有第三方库更新到最新版本)

这里就不再举例子了,掌握前面的,这个也就会了。

6.如何在MAC 指定文件夹打开终端(terminal)

进入‘系统偏好设置’->'键盘'->'快捷方式'->‘服务’
勾选 新建位于文件夹位置的终端标签 , 新建位于文件夹位置的终端窗口


然后右键文件夹就可以看到这两个选项了。


好啦,从此可以开心的玩耍了。

7.Storyboard中的UIScrollView使用自动布局

在使用storyboard和xib时,我们经常要用到ScrollView,还有自动布局AutoLayout,但是ScrollView和AutoLayout 结合使用,相对来说有点复杂。根据实践,我说一下我的理解,在故事板或xib中,ScrollView是根据其下面的一个View的大小来确定ContentSize的大小。

看一下效果


1. 创建一个项目,拖拽一个ScrollView到故事板中,如下图


2. 选中ScrollView,添加约束。


3. 拖拽一个View到ScrollView上, 然后添加上下左右四周约束。


4.添加完之后, 可能会报一个错, 如下图, 这个暂时别去管。 


5. 我们先确定一下, 我们是需要水平方向的滚动还是竖直方向的滚动,或者水平方向和竖直方向都需要滚动。

a.水平方向和竖直方向都需要滚动的话, 不用添加

b.水平方向滚动需要添加下面一个约束


c.竖直方向需要添加下面一个约束


6.我们以水平方向滚动为例,  我们需要确定我们想要的宽度, 添加一个固定的宽度的约束。


7.选中View, 更新一下Frame


8.如果是想要动态设置ScrollView的宽度,也就是设置View的宽度约束的值, 我们将其拉成属性, 然后修改其值。


9. 如果是确定的宽度, 可以在- (void)updateViewConstraints这个方法中修改,也可以在别处修改。


10.现在运行,就可以水平滚动了。 竖直方向的滚动和水平方向滚动的设置差不多。 我们来添加两个View, 先拖拽一个View(我设为灰色)到视图上, 然后添加约束, 如下图


11.再拖拽一个View, 背景颜色设为红色,设置好之后, 将frame设置到我们需要的, 我这边将X设置到600。


12.我们给第二个View添加约束,如下图


13.我们还需要设置一个约束, 就是第二个View距离SuperView的距离,就是第二个View的Leading约束


14.然后将这个约束Leading拉成属性,在- (void)updateViewConstraints设置他的值


如下图


这样子就OK了。 

自动布局需要自己去多多实践,原理还是很简单,理解后练练就会了。

8.将RGB的颜色转换成UIColor

<span style="font-size:14px;">@implementation UIColor (SVGColor)-(UIColor *)initWithRGBColor:(NSString *)rgbColorString{    //删除字符串中的空格    NSString *cString = [[rgbColorString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];    // String should be 6 or 8 characters    if ([cString length] < 6)    {        return [UIColor clearColor];    }    // strip 0X if it appears    //如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾    if ([cString hasPrefix:@"0X"])    {        cString = [cString substringFromIndex:2];    }    //如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾    if ([cString hasPrefix:@"#"])    {        cString = [cString substringFromIndex:1];    }    if ([cString length] != 6)    {        return [UIColor clearColor];    }        // Separate into r, g, b substrings    NSRange range;    range.location = 0;    range.length = 2;    //r    NSString *rString = [cString substringWithRange:range];    //g    range.location = 2;    NSString *gString = [cString substringWithRange:range];    //b    range.location = 4;    NSString *bString = [cString substringWithRange:range];        // Scan values    unsigned int r, g, b;    [[NSScanner scannerWithString:rString] scanHexInt:&r];    [[NSScanner scannerWithString:gString] scanHexInt:&g];    [[NSScanner scannerWithString:bString] scanHexInt:&b];    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:1];}</span>

9.table及部分控件的一些使用技巧

<span style="font-size:14px;">-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return RowNumber;}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString * CellIdentifier = @"Cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];        if (indexPath.row == 0) {            [self firstCellForTable:cell];        }        else if(indexPath.row == RowNumber-1)        {            [self lastCellForTable:cell];        }        else        {            [self otherCellForTable:cell cellForRowAtIndexPath:indexPath];        }    }    return cell;}-(UITableViewCell *)lastCellForTable:(UITableViewCell *)cell{    UIColor * grdColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1];    UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(15, 10, 345, 122)];    textView.text = @"最近的 xcode ghost 事件让大家的安全意识提高了不少。本文介绍的方法可能存在一些安全隐患。当然,解决方案也是有的,就是可以把本地 cdn 加速的文件和官网发布的正式版本做 md5 检验,这样确保本地文件没有被修改。由于时间紧迫,目前还没有实现 md5 检验。请自己负责你的计算机安全。";    textView.textColor = grdColor;    [cell addSubview:textView];    return cell;}-(UITableViewCell *)otherCellForTable:(UITableViewCell *)cell cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UIColor * titleColor = [UIColor colorWithRed:50/255.0 green:50/255.0 blue:50/255.0 alpha:1];    UIColor * grdColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1];        UILabel * adrInfo = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 150, 25)];    adrInfo.textColor = titleColor;    adrInfo.text = @"Size";    adrInfo.font = [UIFont fontWithName:@"Helvetica" size:13];    [cell addSubview:adrInfo];        UILabel * detailAdr = [[UILabel alloc]initWithFrame:CGRectMake(165, 10, 200, 25)];    detailAdr.text = @"2062 Sq.Ft";    detailAdr.font = [UIFont fontWithName:@"Helvetica" size:13];    detailAdr.textColor = grdColor;    [cell addSubview:detailAdr];    return cell;}-(UITableViewCell *)firstCellForTable:(UITableViewCell *)cell{    UILabel * adrInfo = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 350, 25)];    UIColor * titleColor = [UIColor colorWithRed:50/255.0 green:50/255.0 blue:50/255.0 alpha:1];    adrInfo.textColor = titleColor;    adrInfo.text = @"600 W Las Olas Blvd APT 1607S";    [cell addSubview:adrInfo];        UIColor * grdColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1];        UIImage * mapIMG = [UIImage imageWithSVGNamed:@"地图模式-01" targetSize:CGSizeMake(11, 15) fillColor:grdColor];    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(15, 42, 11, 15)];    image.image = mapIMG;    [cell addSubview:image];        UILabel * detailAdr = [[UILabel alloc]initWithFrame:CGRectMake(28, 35, 300, 25)];    detailAdr.text = @"20 Pine St #2405  New York";    detailAdr.font = [UIFont fontWithName:@"Helvetica" size:15];    detailAdr.textColor = grdColor;    [cell addSubview:detailAdr];            UIColor * starColor = [UIColor colorWithRed:255/255.0 green:220/255.0 blue:38/255.0 alpha:1];    UIImage * starUpIMG = [UIImage imageWithSVGNamed:@"star01-01" targetSize:CGSizeMake(16, 15) fillColor:starColor];    UIImage * starDownIMG = [UIImage imageWithSVGNamed:@"star02" targetSize:CGSizeMake(16, 15) fillColor:grdColor];        UIImageView * star1 = [[UIImageView alloc]initWithFrame:CGRectMake(15, 66, 16, 15)];    star1.image = starUpIMG;    UIImageView * star2 = [[UIImageView alloc]initWithFrame:CGRectMake(32, 66, 16, 15)];    star2.image = starUpIMG;    UIImageView * star3 = [[UIImageView alloc]initWithFrame:CGRectMake(49, 66, 16, 15)];    star3.image = starUpIMG;    UIImageView * star4 = [[UIImageView alloc]initWithFrame:CGRectMake(66, 66, 16, 15)];    star4.image = starUpIMG;    UIImageView * star5 = [[UIImageView alloc]initWithFrame:CGRectMake(83, 66, 16, 15)];    star5.image = starDownIMG;    [cell addSubview:star1];    [cell addSubview:star2];    [cell addSubview:star3];    [cell addSubview:star4];    [cell addSubview:star5];        UIImageView * bedroom = [[UIImageView alloc]initWithFrame:CGRectMake(125, 66, 15, 15)];    UIImage * bedroomIMG = [UIImage imageWithSVGNamed:@"卧室数-01" targetSize:CGSizeMake(15, 15) fillColor:grdColor];    bedroom.image = bedroomIMG;    [cell addSubview:bedroom];        UIImageView * bathroom = [[UIImageView alloc]initWithFrame:CGRectMake(170, 66, 15, 15)];    UIImage * bathroomIMG = [UIImage imageWithSVGNamed:@"浴室数-01" targetSize:CGSizeMake(15, 15) fillColor:grdColor];    bathroom.image = bathroomIMG;    [cell addSubview:bathroom];        UIImageView * carbam = [[UIImageView alloc]initWithFrame:CGRectMake(215, 66, 15, 15)];    UIImage * carbamIMG = [UIImage imageWithSVGNamed:@"车库数" targetSize:CGSizeMake(15, 15) fillColor:grdColor];    carbam.image = carbamIMG;    [cell addSubview:carbam];        UILabel * bedroomCount = [[UILabel alloc]initWithFrame:CGRectMake(145, 66, 20, 15)];    bedroomCount.text = @"5";    bedroomCount.font = [UIFont fontWithName:@"Helvetica" size:12];    bedroomCount.textColor = titleColor;    bedroomCount.textAlignment = NSTextAlignmentCenter;    [cell addSubview:bedroomCount];        UILabel * bathroomCount = [[UILabel alloc]initWithFrame:CGRectMake(190, 66, 20, 15)];    bathroomCount.text = @"3";    bathroomCount.font = [UIFont fontWithName:@"Helvetica" size:12];    bathroomCount.textColor = titleColor;    bathroomCount.textAlignment = NSTextAlignmentCenter;    [cell addSubview:bathroomCount];        UILabel * carbamCount = [[UILabel alloc]initWithFrame:CGRectMake(235, 66, 20, 15)];    carbamCount.text = @"-";    carbamCount.font = [UIFont fontWithName:@"Helvetica" size:12];    carbamCount.textColor = titleColor;    carbamCount.textAlignment = NSTextAlignmentCenter;    [cell addSubview:carbamCount];        UIButton * calculateBtn = [[UIButton alloc]initWithFrame:CGRectMake(320, 60, 25, 27)];    UIColor * blueColor = [UIColor colorWithRed:00/255.0 green:174/255.0 blue:169/255.0 alpha:1];    UIImage * calculateIMG = [UIImage imageWithSVGNamed:@"计算器-01" targetSize:CGSizeMake(25, 27) fillColor:blueColor];    [calculateBtn setBackgroundImage:calculateIMG forState:UIControlStateNormal];    [cell addSubview:calculateBtn];    return cell;}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row == 0) {        return 94;    }    else if(indexPath.row == RowNumber-1){        return 122;    }    else    {        return 35;    }}-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{    return 40;}-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(tableView.frame.size.width/2 - 50, 5, 100, 30)];    [btn setTitle: @"Read More" forState: UIControlStateNormal];    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:13];        UIColor * blueColor = [UIColor colorWithRed:20/255.0 green:154/255.0 blue:223/255.0 alpha:1];    [btn setTitleColor:blueColor forState:UIControlStateNormal];        [view addSubview:btn];    return view;}</span>

10.在storyboard中设置控件边框属性

通常使用Category时,只能添加方法,不可添加属性。但是在使用Storyboard时我们可能会使用到keyPath,这里设置的key都需要是所设置视图的属性值,并且类型有所限制。

例如:我现在有一个按钮,我想设置成圆角,并且是紫色边框。并且我不想写代码,想通过storyboard直接设置。这样看起来很帅(其实就是一个强迫症吧,因为一直在用sb设置整个界面,但是突然出现几个样式,在sb中不可以直接设置,这种感觉很烦躁的,所以要解决掉它)。

为类目(类别)添加属性,,使用storyboard设置边框颜色


遇到的问题:这里key path 的type类型是固定的,这里我们不可以添加。这里只有一个UIColor类型的color,但是设置边框的颜色,应该是CGColor的,所以类型不匹配,我们需要为CALayer添加一个UIColor的属性。这里我们使用Category,不用疑问,对,是使用Category为CALayer添加一个UIColor的属性。

<span style="font-size:14px;">CALayer+Addition.h#import <QuartzCore/QuartzCore.h>#import <UIKit/UIKit.h>@interface CALayer (Additions)@property(nonatomic, strong) UIColor *borderColorFromUIColor;- (void)setBorderColorFromUIColor:(UIColor *)color;@endCALayer+Addition.m#import "CALayer+Additions.h"#import <objc/runtime.h>@implementation CALayer (Additions)- (UIColor *)borderColorFromUIColor {    return objc_getAssociatedObject(self, @selector(borderColorFromUIColor));}-(void)setBorderColorFromUIColor:(UIColor *)color{    objc_setAssociatedObject(self, @selector(borderColorFromUIColor), color, OBJC_ASSOCIATION_RETAIN_NONATOMIC);    [selfsetBorderColorFromUIborderColorFromUIColor];}- (void)setBorderColorFromUI:(UIColor *)color{    self.borderColor = color.CGColor;}</span>

这样就成功为CALayer添加了borderColorFromUIColor属性,并且在key path里可能成功设置该属性,为按钮设置边框颜色了。

实现效果

为类目(类别)添加属性,使用storyboard设置边框颜色


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信运动跳转排行榜失败怎么办 小恩爱账号忘了怎么办 华为账号更换后游戏账号怎么办 注册游戏账号需要身份证怎么办 英雄联盟被裁决了怎么办 百度网盘密码重置不了怎么办 sap密码输入被锁怎么办 dnf二级密码错10怎么办 大网卡网速慢了怎么办 小米路由器无线速度慢怎么办 小米手机无线速度慢怎么办 电脑网卡驱动没了怎么办 电脑显示网卡驱动不正常怎么办 微信别人拒收消息怎么办 电脑无线网卡速度慢怎么办 网吧吃鸡更新慢怎么办 手机号注册不了微信怎么办 小米账号密码忘了怎么办 小米手机账号密码忘了怎么办 华为手机账号密码忘记了怎么办 老年机开不了机怎么办 天谕没有顺网登陆怎么办 苹果密保问题忘了怎么办 密保手机没用了怎么办 qq密保手机没用了怎么办 手机开机按钮坏了怎么办 改了账号游戏角色消失怎么办 华为开机键坏了怎么办 抖音账号已重置怎么办 抖音账号被重置怎么办 吃鸡账号密码忘了怎么办 微信只记得账号忘了手机号怎么办 红米3开机键失灵怎么办 晚自习教室有许多虫子怎么办 泰迪吃草又呕吐怎么办 手机不断收到验证码信息怎么办 樱桃吃多了上火怎么办 过年不想回婆婆家过怎么办 旅行箱提手坏了怎么办 影棚人物后面有影子怎么办 微信运动图标不见了怎么办