Trước tiên, bạn cần một con đường với các góc góc cạnh:
- (CGPathRef) makeAnglePathWithRect: (CGRect)rect withSize:(float) s {
CGPoint one = CGPointMake(rect.origin.x +s, rect.origin.y);
CGPoint two = CGPointMake(rect.origin.x + rect.size.width - s, rect.origin.y);
CGPoint three = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y +s);
CGPoint four = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height -s);
CGPoint five = CGPointMake(rect.origin.x + rect.size.width-s, rect.origin.y + rect.size.height);
CGPoint six = CGPointMake(rect.origin.x+s, rect.origin.y + rect.size.height);
CGPoint seven = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height-s);
CGPoint eight = CGPointMake(rect.origin.x, rect.origin.y + s);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,NULL,one.x, one.y);
CGPathAddLineToPoint(path,NULL,two.x, two.y);
CGPathAddLineToPoint(path,NULL,three.x, three.y);
CGPathAddLineToPoint(path,NULL,four.x, four.y);
CGPathAddLineToPoint(path,NULL,five.x, five.y);
CGPathAddLineToPoint(path,NULL,six.x, six.y);
CGPathAddLineToPoint(path,NULL,seven.x, seven.y);
CGPathAddLineToPoint(path,NULL,eight.x, eight.y);
CGPathAddLineToPoint(path,NULL,one.x, one.y);
return path;
}
Sau đó, bạn cần phải sử dụng đường dẫn để xác định một mặt nạ:
CAShapeLayer *maskLayer = [CAShapeLayer layer];
CGRect bounds = CGRectMake(0.0f, 0.0f, 100, 100); //figure out your bounds
[maskLayer setFrame:bounds];
CGPathRef p = [self makeAnglePathWithRect:bounds withSize:20.0];
maskLayer.path = p;
_myview.layer.mask = maskLayer;
Nếu bạn muốn loại bỏ các góc độ từ bất kỳ góc , fiddle với điểm một tám, loại bỏ giá trị "s". Bạn có thể thay đổi kích thước của tam giác cắt ra khỏi các góc với tham số kích thước.
IIRC CoreGraphics hiện công việc nếu bạn thực hiện pixel trong suốt. Bạn đã thử điều đó chưa? –
@ H2CO3 Tôi không thực sự chắc chắn ý bạn là gì. Bạn có ý định sử dụng hình ảnh với các góc bị cắt làm màu nền không? –
@Tahne Không, ý tôi là vẽ các pixel trong suốt theo lập trình bằng cách sử dụng các hàm 'CGContext *()'. –