Reputation: 467
I need to design diamond shape buttons like below image. I have tried with two UIViews
and I'm able to draw diamond shape on top of the views by subclassing the UIView
.But the problem is these two views are overlapping..!
Here is the method i have used :
- (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);
}
Upvotes: 0
Views: 1174
Reputation: 111
this is what is look like in swift in case anyone wondering
var tr = CGAffineTransformIdentity
tr = CGAffineTransformScale(tr, 0.8, 1)
tr = CGAffineTransformRotate(tr, M_PI_4)
self.diamond.transform = tr
but the problem is that the text also effected by it
Upvotes: 0
Reputation: 718
You can easily transform a square to a diamond. Assuming that you have a sqaure button called diamond the transfomation below results in a diamond.
CGAffineTransform tr = CGAffineTransformIdentity;
tr = CGAffineTransformScale(tr, 0.8, 1);
tr = CGAffineTransformRotate(tr, M_PI_4);
self.diamond.transform = tr;
Upvotes: 2
Reputation: 534925
You can't avoid the overlapping of the views. Views are rectangular shaped!
What you can do is avoid the tappability of the area outside the diamond drawing. You would do that by munging the hit-testing for these buttons, so that button is "touched" only if the user taps inside the diamond.
Upvotes: 2