1、控件的局部圓角問題
你是不是也遇到過這樣的問題,一個(gè)button或者label,只要右邊的兩個(gè)角圓角,或者只要一個(gè)圓角。該怎么辦呢。這就需要圖層蒙版來幫助我們了
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的透明問題
如果僅僅把navigationBar的alpha設(shè)為0的話,那就相當(dāng)于把navigationBar給隱藏了,大家都知道,父視圖的alpha設(shè)置為0的話,那么子視圖全都會(huì)透明的。那么相應(yīng)的navigationBar的標(biāo)題和左右兩個(gè)按鈕都會(huì)消失。這樣顯然達(dá)不到我們要求的效果。
?。?)如果僅僅是想要navigationBar透明,按鈕和標(biāo)題都在可以使用以下方法:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];//給navigationBar
設(shè)置一個(gè)空的背景圖片即可實(shí)現(xiàn)透明,而且標(biāo)題按鈕都在

這就需要我們做進(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的偏移量來設(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:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:UIColorWithHexRGB(0xfe6d27)}];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName : UIColorWithHexRGB(0x666666)} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSiz
4、navigationBar隱藏顯示的過度
相信在使用中肯定遇到過,一個(gè)頁面隱藏navigationBar,另一個(gè)不隱藏。兩個(gè)頁面進(jìn)行push和pop的時(shí)候,尤其是有側(cè)滑手勢(shì)返回的時(shí)候,不做處理就會(huì)造成滑動(dòng)返回時(shí),navigationBar位置是空的,直接顯示一個(gè)黑色或者顯示下面一層視圖,很難看。這就需要我們加入過度動(dòng)畫來隱藏或顯示navigationBar:
在返回后將要出現(xiàn)的頁面實(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)用。但是在開發(fā)過程中在自定義了返回按鈕,或者某些webView,tableView等頁面,側(cè)滑返回手勢(shì)失效,這時(shí)候就需要我們來進(jìn)行設(shè)置一下了,可以在基類里面協(xié)商如下代碼:
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {//需要遵循一下手勢(shì)的代理 self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
問題:當(dāng)返回navigationController的最頂層的Controller的時(shí)候。再次側(cè)滑,這個(gè)時(shí)候你在點(diǎn)擊一個(gè)push頁面的操作,你會(huì)發(fā)現(xiàn)卡那了,半天才會(huì)有反應(yīng)。
這是由于,在最頂層Controller手勢(shì)依然有效,但是滑動(dòng)后,并找不到返回的頁面。造成軟件卡頓,假死所以就要在rootViewController中讓此手勢(shì)失效。把下面的設(shè)為NO
6、給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:[UIImage imageNamed:@"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的高度,使其可見
7、模態(tài)跳轉(zhuǎn)的動(dòng)畫設(shè)置
設(shè)置模態(tài)跳轉(zhuǎn)的動(dòng)畫,系統(tǒng)提供了四種可供選擇
DetailViewController *detailVC = [[DetailViewController alloc]init];
//UIModalTransitionStyleFlipHorizontal 翻轉(zhuǎn) //UIModalTransitionStyleCoverVertical 底部滑出 //UIModalTransitionStyleCrossDissolve 漸顯 //UIModalTransitionStylePartialCurl 翻頁 detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:detailVC animated:YES completion:nil];
8、圖片處理只拿到圖片的一部分
UIImage *image = [UIImage imageNamed:filename];
//這里的寬高是相對(duì)于圖片的真實(shí)大小//比如你的圖片是400x400的那么(0,0,400,400)就是圖片的全尺寸,想取哪一部分就設(shè)置相應(yīng)坐標(biāo)即可
CGImageRef imageRef = image.CGImage;CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);
CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);
UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];
9、給UIView設(shè)置圖片
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è)置顯示的圖片范圍//不過此處略有不同,這里的四個(gè)值均為0-1之間;對(duì)應(yīng)的依然是寫x,y,widt,height
10、給TableView或者CollectionView的cell添加簡(jiǎn)單動(dòng)畫,像這樣
image.png


只要在willDisplayCell方法中對(duì)將要顯示的cell做動(dòng)畫即可:
- (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放回原來的位置 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 *)cell forItemAtIndexPath:(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) {
}];
}