iOS开发交流平台程序员iOS 开发

iOS常用一些小技巧

2016-05-27  本文已影响274人  轻斟浅醉17

一.取消tableView Cell的选中状态

取消之前

在cell数据(以下方法中)

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我们经常使用

     cell.selectionStyle = UITableViewCellSelectionStyleNone;

或者[cell setSelectionStyle:UITableViewCellSelectionStyleNone];这两种方法

    在-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 方法中使用

    [tableView deselectRowAtIndexPath:indexPath animated:NO];取消选中状态

取消之后

二.解决图片压缩问题

不写下面方法时图片会按比例对原图进行缩放,图片会失真;

    UIImageView *Photo = [[UIImageView alloc]init];

    [Photo setImage:[UIImage imageNamed:@"12"]];

    [self.view addSubView:Photo];

    [Photo setContentScaleFactor:[[UIScreen mainScreen] scale]];

    Photo.contentMode =  UIViewContentModeScaleAspectFill;

//如果在默认情况,图片的多出来的部分还是会显示屏幕上。如果不希望超过frame的区域显示在屏幕上要设置。clipsToBounds属性。

Photo.clipsToBounds  = YES;

三.取消tableView的滚动条

self.tableView.showsVerticalScrollIndicator = NO;

四 .图片设置圆角

    UIButton *littleIma = [[UIButton alloc]initWithFrame:CGRectMake(5, 3, 35, 35)];

    [Glass addSubview: littleIma];

    littleIma.layer.cornerRadius = 17.5;

    littleIma.layer.masksToBounds = YES;

五.毛玻璃效果设置

注意:本篇文章所讲的内容是基于iOS8以上系统的。

其实很多地方加入这个效果都会让你的app锦上添花,例如tableView的section header。在- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section这个代理方法中返回UIVisualEffectView就可以实现这个效果,举个例子:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:  (NSInteger)section{ UIVisualEffectView *view = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]]; view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 40); UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, CGRectGetWidth(self.view.frame) - 10, 45)]; label.text = @"轻斟浅醉17"; [view.contentView addSubview:label]; return view;}

还有一个典型的应用是全屏的遮罩。例如需要弹出提示窗的或是菜单的时候,可以将原来半透明的黑色背景换成毛玻璃效果

![146191-0e77be474aeb1f2f.png](http:https://img.haomeiwen.com/i1221840/d65d9bf7477d18a6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

UIVisualEffectView的使用也极其简单,初始化的时候通过initWithEffect:这个方法为他指定模糊效果即可。三种Blur效果大家可以自己尝试对比。

其实上面那张图片中的文字也是有特殊效果的,可以透出背景的颜色,实际上是将承载文字的Label放在了一个UIVisualEffectView中,而这个UIVisualEffectView的effect就是UIVibrancyEffect。拿上面图片的效果举例,具体实现是这样:

1、创建一个背景遮罩View

    UIVisualEffectView *vfv = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];

2、再创建另一个UIVisualEffectView用于承载Label实现文字的穿透效果

    UIVisualEffectView *tev = [[UIVisualEffectView alloc]initWithEffect:[UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)vfv.effect]];

3、创建一个label,将label添加到tev上面,将tev添加到vfv上面

    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(40, 100, 200, 200)];

    lbl.text = @"其实上面那张图片中的文字也是有特殊效果的";

    lbl.numberOfLines = 0;

    [tev.contentView addSubview:lbl];

    [vfv.contentView addSubview:tev];

And:

     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 30.0, self.view.frame.size.width, 200.0)];

      [self.view addSubview:view];

      UIVisualEffect *blurEffect;

      blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];

      UIVisualEffectView *visualEffectView;

       visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];

       [visualEffectView setFrame:view.bounds];

       [view addSubview:visualEffectView];

六.tag的常用方法

对于使用静态的tag的视图,推荐使用“-”的,并且tag的范围小一点儿比较好。setTag:0这个最好不要用,因为有的时候superView的tag的默认是0

因为对于整个程序来说,tag是全局的变量(类似),并不是在某个view,里面的viewOftag方法,而只调用本view里的相应的view,而是在内存中寻找只要live状态的,均会被调用。所以tag需要保证唯一性。

对相同多个对象进行区分

        for (int i = 0; i < 6; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        button.frame = CGRectMake(btuX , btuY , btuWidth, btuHeight);   

        [button setTitle:[NSString stringWithFormat:@"btuTitle %d",i] forState:UIControlStateNormal];

//图片显示方式 居中

    //        button.imageView.contentMode =  UIViewContentModeCenter;

    //        button.imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

        [button setImage:[UIImage imageNamed:[ImageArray objectAtIndex:i]]  forState:UIControlStateNormal];

    button.tag = i ;

        [button addTarget:self action:@selector(buttonClick:) f orControlEvents:UIControlEventTouchUpInside];

        [self.contentView addSubview:button];

希望对大家有所帮助,今后分享更好的东西给大家!

持续更新......

上一篇 下一篇

猜你喜欢

热点阅读