12

私は自分の見解の角を丸くしようとしていて、奇妙な問題をフェンシングしています。

次のコードを使用して、ビューの角を丸くします

bezierPath = [UIBezierPath bezierPathWithRoundedRect:btnView.bounds
                                       byRoundingCorners:UIRectCornerAllCorners
                                             cornerRadii:radius];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = btnView.bounds;
    maskLayer.path = bezierPath.CGPath;
    btnView.layer.mask = maskLayer;

問題は、下隅では機能しないことです。使用してUIRectCornerBottomLeftも何も起こりませんUIRectCornerAllCorners。上部の角だけを使用すると丸みを帯びます。

編集: 下の角を丸くしたいだけなので、layer.cornerRadius は使用しません。

UIViewAutoresizingFlexibleHeightビューから自動サイズ変更マスクを削除しても、問題はないことがわかりました。しかし、自動サイズ変更マスクを使用したいと思います。はいの場合、どのように可能ですか?

レイヤーのマスクを設定する前後に autoresizingMask を設定しようとしました。運が悪い!

4

9 に答える 9

12

解決するのに何時間もかかった同様の問題がありました-答えは非常に簡単でした:コードをviewDidLoadからviewDidLayoutSubviewsに移動します:これは、自動レイアウトが完了した後に呼び出される最初のメソッドです....だから、4つの丸い角があったと思いますしかし、どういうわけか、それらは画面の端から外れていました。

于 2014-06-25T19:52:32.517 に答える
1

解決:

-(void)roundCorners:(UIRectCorner)corner radius:(float)radius
{   
    _cornerRadius = radius;
    _corner = corner;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:_corner cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;

    self.layer.mask = maskLayer;
}
于 2015-01-28T23:06:13.867 に答える
1

使うべきだと思います

bezierPath.lineCapStyle = CGLineCap.Round
于 2016-07-12T02:21:39.717 に答える
0

最後に私は成功しました、そしてそれは本当にうまくいきます。それを試してみてください。

CGRect frame = self.accountBackgroundImage2.frame;
frame.size.height = self.view.bounds.size.height;
UIBezierPath *path=[UIBezierPath bezierPathWithRoundedRect:self.accountBackgroundImage2.bounds byRoundingCorners:(UIRectCornerBottomLeft |UIRectCornerBottomRight) cornerRadii:CGSizeMake(12.5, 12.5)];

CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
shapeLayer.frame = self.accountBackgroundImage2.bounds;
shapeLayer.path  = path.CGPath;
_accountBackgroundImage2.layer.mask = shapeLayer;
self.accountBackgroundImage2.frame = frame;
于 2015-05-21T10:56:07.507 に答える
0

別の方法

「 viewWillLayoutSubviews 」での実行

- (void)viewWillLayoutSubviews{
[self roundCornersOnView:self.buttonJoin onTopLeft:NO topRight:YES bottomLeft:YES bottomRight:NO radius:10.0];
}

関数

-(UIButton *)roundCornersOnView:(UIButton *)button 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;
    }

    UIButton *roundedView = button;
    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 button;
}

}

于 2015-08-04T20:56:50.040 に答える
0

ビューのコーナーを変更する必要がありました。自動サイズ変更により、viewDidLoad の下隅が機能しませんでした。コードを viewDidAppear に移動する必要がありました。

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];

CGRect bounds = self.view.bounds;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(5.0, 5.0)];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
self.view.layer.mask = maskLayer;

self.view.layer.masksToBounds = YES;
[self.view setNeedsDisplay];
}
于 2015-04-14T23:39:41.223 に答える