1、控件的局部圓角問(wèn)題
你是不是也遇到過(guò)這樣的問(wèn)題,一個(gè)button或者label玛迄,只要右邊的兩個(gè)角圓角,或者只要一個(gè)圓角棚亩。該怎么辦呢蓖议。這就需要圖層蒙版來(lái)幫助我們了
CGRect rect = CGRectMake(0, 0, 100, 50);
CGSize radio = CGSizeMake(5, 5);//圓角尺寸
UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;//這只圓角位置
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];
CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//創(chuàng)建shapelayer
masklayer.frame = button.bounds;
masklayer.path = path.CGPath;//設(shè)置路徑
button.layer.mask = masklayer;
舉例為button,其它繼承自UIView的控件都可以
2蔑舞、navigationBar的透明問(wèn)題
如果僅僅把navigationBar的alpha設(shè)為0的話拒担,那就相當(dāng)于把navigationBar給隱藏了,大家都知道攻询,父視圖的alpha設(shè)置為0的話从撼,那么子視圖全都會(huì)透明的。那么相應(yīng)的navigationBar的標(biāo)題和左右兩個(gè)按鈕都會(huì)消失。這樣顯然達(dá)不到我們要求的效果低零。
(1)如果僅僅是想要navigationBar透明婆翔,按鈕和標(biāo)題都在可以使用以下方法:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];//給navigationBar設(shè)置一個(gè)空的背景圖片即可實(shí)現(xiàn)透明,而且標(biāo)題按鈕都在
細(xì)心的你會(huì)發(fā)現(xiàn)上面有一條線如下圖:
這就需要我們做進(jìn)一步處理掏婶,把線去掉啃奴,如下方法即可:
self.navigationController.navigationBar.shadowImage = [UIImage new];
//其實(shí)這個(gè)線也是image控制的。設(shè)為空即可
2)如果你想在透明的基礎(chǔ)上實(shí)現(xiàn)根據(jù)下拉距離雄妥,由透明變得不透明的效果最蕾,那么上面那個(gè)就顯得力不從心了,這就需要我們采用另外一種方法了
//navigationBar是一個(gè)復(fù)合視圖老厌,它是有許多個(gè)控件組成的瘟则,那么我們就可以從他的內(nèi)部入手
[[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;//這里可以根據(jù)scrollView的偏移量來(lái)設(shè)置alpha就實(shí)現(xiàn)了漸變透明的效果
3、全局設(shè)置navigationBar標(biāo)題的樣式和barItem的標(biāo)題樣式
//UIColorWithHexRGB( )這個(gè)方法是自己定義的枝秤,這里只需要給個(gè)顏色就好了
[[UINavigationBar appearance] setBarTintColor:UIColorWithHexRGB(0xfefefe)];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFontboldSystemFontOfSize:18],NSForegroundColorAttributeName:UIColorWithHexRGB(0xfe6d27)}];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFontboldSystemFontOfSize:10],NSForegroundColorAttributeName : UIColorWithHexRGB(0x666666)}forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSiz
4醋拧、navigationBar隱藏顯示的過(guò)度
相信在使用中肯定遇到過(guò),一個(gè)頁(yè)面隱藏navigationBar淀弹,另一個(gè)不隱藏丹壕。兩個(gè)頁(yè)面進(jìn)行push和pop的時(shí)候,尤其是有側(cè)滑手勢(shì)返回的時(shí)候薇溃,不做處理就會(huì)造成滑動(dòng)返回時(shí)菌赖,navigationBar位置是空的,直接顯示一個(gè)黑色或者顯示下面一層視圖痊焊,很難看盏袄。這就需要我們加入過(guò)度動(dòng)畫(huà)來(lái)隱藏或顯示navigationBar:
在返回后將要出現(xiàn)的頁(yè)面實(shí)現(xiàn)viewWillAppear方法,需要隱藏就設(shè)為YES薄啥,需要顯示就設(shè)為NO
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
5辕羽、側(cè)滑手勢(shì)返回
iOS的側(cè)滑返回手勢(shì)有著很好的操作體驗(yàn),不支持側(cè)滑返回的應(yīng)用絕對(duì)不是好應(yīng)用垄惧。但是在開(kāi)發(fā)過(guò)程中在自定義了返回按鈕刁愿,或者某些webView,tableView等頁(yè)面,側(cè)滑返回手勢(shì)失效到逊,這時(shí)候就需要我們來(lái)進(jìn)行設(shè)置一下了,可以在基類里面協(xié)商如下代碼:
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//需要遵循一下手勢(shì)的代理 self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
問(wèn)題:當(dāng)返回navigationController的最頂層的Controller的時(shí)候铣口。再次側(cè)滑,這個(gè)時(shí)候你在點(diǎn)擊一個(gè)push頁(yè)面的操作觉壶,你會(huì)發(fā)現(xiàn)卡那了脑题,半天才會(huì)有反應(yīng)。
這是由于铜靶,在最頂層Controller手勢(shì)依然有效叔遂,但是滑動(dòng)后,并找不到返回的頁(yè)面。造成軟件卡頓已艰,假死所以就要在rootViewController中讓此手勢(shì)失效痊末。把下面的設(shè)為NO
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
當(dāng)然你也可以使用一個(gè)第三方庫(kù),寫(xiě)的相當(dāng)棒哩掺。他對(duì)系統(tǒng)的側(cè)滑返回手勢(shì)進(jìn)行拓展凿叠,不用從邊緣滑動(dòng),只要右滑即可返回嚼吞。最重要的是盒件,他只需要加入項(xiàng)目中即可,不需要一行代碼即可實(shí)現(xiàn)誊薄。附上github 網(wǎng)址:
https://github.com/forkingdog/FDFullscreenPopGesture
6履恩、導(dǎo)航控制器返回時(shí)陰影出現(xiàn)
這個(gè)問(wèn)題其實(shí)也很常見(jiàn),可以看到,在Push的時(shí)候,有顏色層級(jí)錯(cuò)開(kāi),非常影響體驗(yàn),解決方式也比較簡(jiǎn)單,只需要設(shè)置Window的顏色就可以了
[UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor];
7锰茉、給webView添加頭視圖
webView是一個(gè)復(fù)合視圖呢蔫,里面包含有一個(gè)scrollView,scrollView里面是一個(gè)UIWebBrowserView(負(fù)責(zé)顯示W(wǎng)ebView的內(nèi)容)
UIView *webBrowserView = self.webView.scrollView.subviews[0];//拿到webView的webBrowserView
self.backHeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenWidth*2/3.0)];
[_backHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImageimageNamed:@"placeholderImage"]];
[self.webView insertSubview:_backHeadImageView belowSubview:self.webView.scrollView];
//把backHeadImageView插入到webView的scrollView下面
CGRect frame = self.webBrowserView.frame;
frame.origin.y = CGRectGetMaxY(_backHeadImageView.frame);
self.webBrowserView.frame = frame;
//更改webBrowserView的frame向下移backHeadImageView的高度飒筑,使其可見(jiàn)
8片吊、模態(tài)跳轉(zhuǎn)的動(dòng)畫(huà)設(shè)置
設(shè)置模態(tài)跳轉(zhuǎn)的動(dòng)畫(huà),系統(tǒng)提供了四種可供選擇
DetailViewController *detailVC = [[DetailViewController alloc]init];
//UIModalTransitionStyleFlipHorizontal 翻轉(zhuǎn)
//UIModalTransitionStyleCoverVertical 底部滑出
//UIModalTransitionStyleCrossDissolve 漸顯
//UIModalTransitionStylePartialCurl 翻頁(yè)
detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:detailVC animated:YES completion:nil];
9协屡、圖片處理只拿到圖片的一部分
UIImage *image = [UIImage imageNamed:filename];
CGImageRef imageRef = image.CGImage;
CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);
//這里的寬高是相對(duì)于圖片的真實(shí)大小
//比如你的圖片是400x400的那么(0俏脊,0,400肤晓,400)就是圖片的全尺寸爷贫,想取哪一部分就設(shè)置相應(yīng)坐標(biāo)即可
CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);
UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];
10、給TableView或者CollectionView的cell添加簡(jiǎn)單動(dòng)畫(huà)
只要在willDisplayCell方法中對(duì)將要顯示的cell做動(dòng)畫(huà)即可:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
NSArray *array = tableView.indexPathsForVisibleRows;
NSIndexPath *firstIndexPath = array[0];
//設(shè)置anchorPoint
cell.layer.anchorPoint = CGPointMake(0, 0.5);
//為了防止cell視圖移動(dòng)补憾,重新把cell放回原來(lái)的位置
cell.layer.position = CGPointMake(0, cell.layer.position.y);
//設(shè)置cell 按照z軸旋轉(zhuǎn)90度漫萄,注意是弧度
if (firstIndexPath.row < indexPath.row) {
cell.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1.0);
}else{
cell.layer.transform = CATransform3DMakeRotation(- M_PI_2, 0, 0, 1.0);
}
cell.alpha = 0.0;
[UIView animateWithDuration:1 animations:^{
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1.0;
}];
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cellforItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row % 2 != 0) {
cell.transform = CGAffineTransformTranslate(cell.transform, kScreenWidth/2, 0);
}else{
cell.transform = CGAffineTransformTranslate(cell.transform, -kScreenWidth/2, 0);
}
cell.alpha = 0.0;
[UIView animateWithDuration:0.7 animations:^{
cell.transform = CGAffineTransformIdentity;
cell.alpha = 1.0;
} completion:^(BOOL finished) {
}];
}
11、給UIView設(shè)置圖片(性能優(yōu)化)
//注意:這種方式讀取圖片會(huì)占內(nèi)存
UIImage *image = [UIImage imageNamed:@"playing"];
_layerView.layer.contents = (__bridge id)image.CGImage;
_layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5);
//同樣可以設(shè)置顯示的圖片范圍
//不過(guò)此處略有不同盈匾,這里的四個(gè)值均為0-1之間腾务;對(duì)應(yīng)的依然是寫(xiě)x,y,widt,height