iOS常用一些小技巧

一.取消tableView Cell的選中狀態(tài)

取消之前

在cell數(shù)據(jù)(以下方法中)

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

我們經(jīng)常使用

? ? ?cell.selectionStyle = UITableViewCellSelectionStyleNone;

或者[cell setSelectionStyle:UITableViewCellSelectionStyleNone];這兩種方法

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

? ? [tableView deselectRowAtIndexPath:indexPath animated:NO];取消選中狀態(tài)

取消之后

二.解決圖片壓縮問題

不寫下面方法時圖片會按比例對原圖進行縮放,圖片會失真;

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

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

? ? [self.view addSubView:Photo];

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

? ? Photo.contentMode =? UIViewContentModeScaleAspectFill;

//如果在默認情況,圖片的多出來的部分還是會顯示屏幕上。如果不希望超過frame的區(qū)域顯示在屏幕上要設置。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;

五.毛玻璃效果設置

注意:本篇文章所講的內(nèi)容是基于iOS8以上系統(tǒng)的。

其實很多地方加入這個效果都會讓你的app錦上添花,例如tableView的section header。在- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section這個代理方法中返回UIVisualEffectView就可以實現(xiàn)這個效果,舉個例子:

- (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://upload-images.jianshu.io/upload_images/1221840-d65d9bf7477d18a6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

UIVisualEffectView的使用也極其簡單,初始化的時候通過initWithEffect:這個方法為他指定模糊效果即可。三種Blur效果大家可以自己嘗試對比。

其實上面那張圖片中的文字也是有特殊效果的,可以透出背景的顏色,實際上是將承載文字的Label放在了一個UIVisualEffectView中,而這個UIVisualEffectView的effect就是UIVibrancyEffect。拿上面圖片的效果舉例,具體實現(xiàn)是這樣:

1、創(chuàng)建一個背景遮罩View

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

2、再創(chuàng)建另一個UIVisualEffectView用于承載Label實現(xiàn)文字的穿透效果

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

3、創(chuàng)建一個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的常用方法

對于使用靜態(tài)的tag的視圖,推薦使用“-”的,并且tag的范圍小一點兒比較好。setTag:0這個最好不要用,因為有的時候superView的tag的默認是0

因為對于整個程序來說,tag是全局的變量(類似),并不是在某個view,里面的viewOftag方法,而只調(diào)用本view里的相應的view,而是在內(nèi)存中尋找只要live狀態(tài)的,均會被調(diào)用。所以tag需要保證唯一性。

對相同多個對象進行區(qū)分

? ? ? ? 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];

希望對大家有所幫助,今后分享更好的東西給大家!

持續(xù)更新......

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

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容