我试图让UIImageView的顶角为圆角 - 而且只是顶角。
这是我的代码,我基于this问题中较高投票的答案:
CouponViewController.m:
@synthesize cv;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self loadXibWithName:@"CouponView"];
cv = (CouponView *)self.view;
cv.delegate = self;
[self.navigationController.navigationBar setHidden:NO];
//[self displayLogoInNavBar];
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:cv.couponImage.bounds
byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
cornerRadii:CGSizeMake(15.0, 15.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = cv.bounds;
maskLayer.path = maskPath.CGPath;
cv.couponImage.layer.mask = maskLayer;
}
除了变量之外,唯一不同的是缺少[maskLayer release]
,导致ARC错误,并且无论如何都无法解决所述错误。
还有一个CouponView.m
,但其中包含IBActions
和awakeFromNib
,似乎没有做任何事情 - 我尝试进行以下测试代码那里:
- (void)awakeFromNib {
self.couponImage.layer.cornerRadius = 15.0f;
self.couponPerforated.image = [UIImage imageNamed:@"coupon_perforation_191"];
}
最后,这里是CouponView.xib
本身:
加载时,图像仍然是正方形。
我错过了什么吗?
答案 0 :(得分:1)
试试这段代码。经过长时间的搜索,它帮助了我。
-(void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self setMaskTo:viewDistance byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
}
- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(20.0, 20.0)];
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
shape.frame = self.view.bounds;
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}