我正在尝试使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,但其中包含IBActionsawakeFromNib,似乎没有任何作用-我尝试在其中放置以下测试代码:

- (void)awakeFromNib {
    self.couponImage.layer.cornerRadius = 15.0f;
    self.couponPerforated.image = [UIImage imageNamed:@"coupon_perforation_191"];
}


最后,这是CouponView.xib本身:



加载时,图像仍然是正方形。

我有什么想念的吗?

最佳答案

试试这个代码。经过长时间的搜索,它可以帮助我。

-(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;
}

关于ios - Xcode:左上角和右上角半径不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31521331/

10-13 03:59