第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在UIView中的兩個(gè)角

在UIView中的兩個(gè)角

iOS
富國滬深 2019-10-15 09:49:57
不久前,我發(fā)布了一個(gè)關(guān)于僅將視圖的兩個(gè)角取整的問題,并獲得了不錯(cuò)的答復(fù),但是在實(shí)現(xiàn)它時(shí)遇到了問題。這是我的drawRect:方法:- (void)drawRect:(CGRect)rect {    //[super drawRect:rect]; <------Should I uncomment this?    int radius = 5;    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextBeginPath(context);    CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI, M_PI / 2, 1);    CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);    CGContextClosePath(context);    CGContextClip(context);}該方法正在被調(diào)用,但似乎并不影響視圖的結(jié)果。有什么想法嗎?
查看完整描述

3 回答

?
當(dāng)年話下

TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊

iOS 11中引入的CACornerMask可幫助在視圖層中定義左上,右上,左下,右下。以下是使用示例。


在這里,我嘗試僅舍入兩個(gè)上角:


myView.clipsToBounds = true

myView.layer.cornerRadius = 10

myView.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]

提前致謝。


僅供參考: 

http://img1.sycdn.imooc.com//5da525dc0001f4ad03930180.jpg

查看完整回答
反對(duì) 回復(fù) 2019-10-15
?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

據(jù)我所知,如果還需要遮罩子視圖,則可以使用CALayer遮罩。有兩種方法可以做到這一點(diǎn)。第一個(gè)比較優(yōu)雅,第二個(gè)是一種解決方法:-),但它也很快。兩者都是基于CALayer遮罩的。去年,我在幾個(gè)項(xiàng)目中都使用了這兩種方法,然后希望您能找到有用的東西。


解決方案1

首先,我創(chuàng)建了此函數(shù)以動(dòng)態(tài)生成UIImage帶有所需圓角的圖像蒙版()。此函數(shù)本質(zhì)上需要5個(gè)參數(shù):圖像的邊界和4個(gè)角半徑(左上,右上,左下和右下)。



static inline UIImage* MTDContextCreateRoundedMask( CGRect rect, CGFloat radius_tl, CGFloat radius_tr, CGFloat radius_bl, CGFloat radius_br ) {  


    CGContextRef context;

    CGColorSpaceRef colorSpace;


    colorSpace = CGColorSpaceCreateDeviceRGB();


    // create a bitmap graphics context the size of the image

    context = CGBitmapContextCreate( NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast );


    // free the rgb colorspace

    CGColorSpaceRelease(colorSpace);    


    if ( context == NULL ) {

        return NULL;

    }


    // cerate mask


    CGFloat minx = CGRectGetMinX( rect ), midx = CGRectGetMidX( rect ), maxx = CGRectGetMaxX( rect );

    CGFloat miny = CGRectGetMinY( rect ), midy = CGRectGetMidY( rect ), maxy = CGRectGetMaxY( rect );


    CGContextBeginPath( context );

    CGContextSetGrayFillColor( context, 1.0, 0.0 );

    CGContextAddRect( context, rect );

    CGContextClosePath( context );

    CGContextDrawPath( context, kCGPathFill );


    CGContextSetGrayFillColor( context, 1.0, 1.0 );

    CGContextBeginPath( context );

    CGContextMoveToPoint( context, minx, midy );

    CGContextAddArcToPoint( context, minx, miny, midx, miny, radius_bl );

    CGContextAddArcToPoint( context, maxx, miny, maxx, midy, radius_br );

    CGContextAddArcToPoint( context, maxx, maxy, midx, maxy, radius_tr );

    CGContextAddArcToPoint( context, minx, maxy, minx, midy, radius_tl );

    CGContextClosePath( context );

    CGContextDrawPath( context, kCGPathFill );


    // Create CGImageRef of the main view bitmap content, and then

    // release that bitmap context

    CGImageRef bitmapContext = CGBitmapContextCreateImage( context );

    CGContextRelease( context );


    // convert the finished resized image to a UIImage 

    UIImage *theImage = [UIImage imageWithCGImage:bitmapContext];

    // image is retained by the property setting above, so we can 

    // release the original

    CGImageRelease(bitmapContext);


    // return the image

    return theImage;

}  

現(xiàn)在,您只需要幾行代碼。我把東西在我的viewController viewDidLoad的方法,因?yàn)樗乃俣雀欤憧梢栽谧远x也用它UIView與layoutSubviews實(shí)例方法。




- (void)viewDidLoad {


    // Create the mask image you need calling the previous function

    UIImage *mask = MTDContextCreateRoundedMask( self.view.bounds, 50.0, 50.0, 0.0, 0.0 );

    // Create a new layer that will work as a mask

    CALayer *layerMask = [CALayer layer];

    layerMask.frame = self.view.bounds;       

    // Put the mask image as content of the layer

    layerMask.contents = (id)mask.CGImage;       

    // set the mask layer as mask of the view layer

    self.view.layer.mask = layerMask;              


    // Add a backaground color just to check if it works

    self.view.backgroundColor = [UIColor redColor];

    // Add a test view to verify the correct mask clipping

    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake( 0.0, 0.0, 50.0, 50.0 )];

    testView.backgroundColor = [UIColor blueColor];

    [self.view addSubview:testView];

    [testView release];


    [super viewDidLoad];

}

解決方案2

這個(gè)解決方案有點(diǎn)“骯臟”。本質(zhì)上,您可以使用所需的圓角(所有角)創(chuàng)建遮罩層。然后,應(yīng)通過角半徑的值增加蒙版層的高度。這樣,底部的圓角被隱藏了,您只能看到上面的圓角。我把代碼只是在viewDidLoad方法,因?yàn)樗乃俣雀?,但你可以在自定義也用它UIView與layoutSubviews實(shí)例方法。


  


- (void)viewDidLoad {


    // set the radius

    CGFloat radius = 50.0;

    // set the mask frame, and increase the height by the 

    // corner radius to hide bottom corners

    CGRect maskFrame = self.view.bounds;

    maskFrame.size.height += radius;

    // create the mask layer

    CALayer *maskLayer = [CALayer layer];

    maskLayer.cornerRadius = radius;

    maskLayer.backgroundColor = [UIColor blackColor].CGColor;

    maskLayer.frame = maskFrame;


    // set the mask

    self.view.layer.mask = maskLayer;


    // Add a backaground color just to check if it works

    self.view.backgroundColor = [UIColor redColor];

    // Add a test view to verify the correct mask clipping

    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake( 0.0, 0.0, 50.0, 50.0 )];

    testView.backgroundColor = [UIColor blueColor];

    [self.view addSubview:testView];

    [testView release];


    [super viewDidLoad];

}

希望這可以幫助。再見!


查看完整回答
反對(duì) 回復(fù) 2019-10-15
  • 3 回答
  • 0 關(guān)注
  • 643 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)