iReddy iReddy - 3 months ago 54
Objective-C Question

How to create Diamond shape Button using Objective-C iOS

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);
}


enter image description here

Answer

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;

enter image description here

Comments