[英]How to create Diamond shape Button using Objective-C iOS
我需要設計如下圖所示的菱形按鈕。 我嘗試了兩個UIViews
並且可以通過子類化UIView
在視圖頂部繪制菱形。但是問題是這兩個視圖是重疊的..!
這是我使用的方法:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);
CGContextSetStrokeColorWithColor(context,
[UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 150);
CGContextAddLineToPoint(context, 100, 200);
CGContextAddLineToPoint(context, 50, 150);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context);
}
您無法避免視圖重疊。 視圖是矩形的!
您可以做的是避免在菱形圖之外的區域輕敲。 您可以通過對這些按鈕進行命中測試來做到這一點,以便僅當用戶在鑽石內部點擊時才“觸摸”該按鈕。
這是很快的樣子,以防有人懷疑
var tr = CGAffineTransformIdentity tr = CGAffineTransformScale(tr, 0.8, 1) tr = CGAffineTransformRotate(tr, M_PI_4) self.diamond.transform = tr
但問題在於文本也受其影響
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.