2015-02-24 47 views
0

我正在Xcode版本6.1.1和iOS 8.1中開發我的應用程序,其中我需要根據設計在圖像視圖中圍繞左上角和右上角。圖像視圖中的選擇性圓角 - iOS

我以前用下面的代碼,它正常工作在Xcode以前的版本:

UIImageView *locationImage = (UIImageView *)[cell viewWithTag:101]; 

UIBezierPath *maskPath1; 

maskPath1 = [UIBezierPath bezierPathWithRoundedRect:locationImage.bounds 
            byRoundingCorners:(UIRectCornerTopRight | UIRectCornerTopLeft) 
             cornerRadii:CGSizeMake(5.0, 5.0)]; 
CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init]; 
maskLayer1.frame = locationImage.bounds; 
maskLayer1.path = maskPath1.CGPath; 
locationImage.layer.mask = maskLayer1; 

現在我得到的左上角圓,但沒有合適的一個。我知道代碼是正確的,因爲如果我將它應用於不受約束的圖像視圖,它運作良好,但我需要將項目約束到視圖。我使用自動佈局。

鏈接到圖片:https://www.dropbox.com/s/orisd8gzbdhsr4z/round-corners.tiff?dl=0

有件事我做錯了嗎?我怎樣才能正確地調整兩個角落?

在此先感謝

*對不起,我的英語

+0

可能重複(http://stackoverflow.com/questions/14866450/round-some -corners-of-uiview-and-round-the-view-s-layer-s-border) – 2015-02-24 10:33:44

+0

感謝您的建議,但是相關帖子使用相同的代碼來四捨五入,只需添加一個筆劃(其中I不需要),並以相同的方式工作:繞左上角。 – cmacera 2015-02-24 11:00:43

回答

0

最後我找到了解決辦法嵌套的UIImageView在UI在用戶定義運行時屬性中設置角半徑的視圖,並將此uiview的cliptobound設置爲yes。

如果有人需要進一步的解釋,我會給[回合的UIView的拐角處和輪視圖的層的邊界過於]高興

1

@jcmartinac,

您可以使用此解決方案 - how to set cornerRadius for only top-left and top-right corner of a UIView?

在你的代碼

應用此代碼,我已經測試,它的工作完美。

maskPath1 = (UIImageView *)[self roundCornersOnView: maskPath1 onTopLeft:YES topRight:YES bottomLeft:NO bottomRight:NO radius:20.0]; 

//使用下面的方法來設置角半徑圓..

-(UIView *)roundCornersOnView:(UIView *)view onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius { 

    if (tl || tr || bl || br) { 
     UIRectCorner corner = 0; //holds the corner 
     //Determine which corner(s) should be changed 
     if (tl) { 
      corner = corner | UIRectCornerTopLeft; 
     } 
     if (tr) { 
      corner = corner | UIRectCornerTopRight; 
     } 
     if (bl) { 
      corner = corner | UIRectCornerBottomLeft; 
     } 
     if (br) { 
      corner = corner | UIRectCornerBottomRight; 
     } 

     UIView *roundedView = view; 
     UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)]; 
     CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
     maskLayer.frame = roundedView.bounds; 
     maskLayer.path = maskPath.CGPath; 
     roundedView.layer.mask = maskLayer; 
     return roundedView; 
    } else { 
     return view; 
    } 

} 

/////////////對於泰伯維使用下面的代碼中的cellForRowAtIndexPath方法/// //

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.Img_thumb=(UIImageView *)[self roundCornersOnView: cell.Img_thumb onTopLeft:YES topRight:YES bottomLeft:NO bottomRight:NO radius:20.0]; 
} 

檢查屏幕截圖: - >Effect on Table

+0

感謝您的幫助!不幸的是,它的工作原理與之前一樣......測試代碼時,我意識到只能在左上角運行,如果我試圖繞過其他任何東西,它什麼都不會做。我完全失去了 – cmacera 2015-02-24 14:09:55

+0

好。保持編碼.. ;-) – Mehul 2015-02-25 06:22:42

+0

我已經看到你的代碼在uiviewcontroller中工作,但我使用的是uitableviewcontroller,當它不工作。你是否在uitableview的單元格內的uiimageview中嘗試了它?我認爲這個問題是在UITableView – cmacera 2015-02-25 11:42:21