2015-02-24 44 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