In my application – there are four buttons named as follows:
- Top – left
- Bottom – left
- Top – right
- Bottom – right
Above the buttons there is an image view (or a UIView).
Now, suppose a user taps on – top – left button. Above image / view should be rounded at that particular corner.
I am having some difficulty in applying rounded corners to the UIView.
Right now I am using the following code to apply the rounded corners to each view:
// imgVUserImg is a image view on IB.
imgVUserImg.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"any Url Here"];
CALayer *l = [imgVUserImg layer];
[l setMasksToBounds:YES];
[l setCornerRadius:5.0];
[l setBorderWidth:2.0];
[l setBorderColor:[[UIColor darkGrayColor] CGColor]];
Above code is applying the roundness to each of corners of supplied View. Instead I just wanted to apply roundness to selected corners like – top / top+left / bottom+right etc.
Is it possible? How?
I used the answer over at How do I create a round cornered UILabel on the iPhone? and the code from How is a rounded rect view with transparency done on iphone? to make this code.
Then I realized I’d answered the wrong question (gave a rounded UILabel instead of UIImage) so I used this code to change it:
http://discussions.apple.com/thread.jspa?threadID=1683876
Make an iPhone project with the View template. In the view controller, add this:
MyViewis just aUIImageViewsubclass:I’d never used graphics contexts before, but I managed to hobble together this code. It’s missing the code for two of the corners. If you read the code, you can see how I implemented this (by deleting some of the
CGContextAddArccalls, and deleting some of the radius values in the code. The code for all corners is there, so use that as a starting point and delete the parts that create corners you don’t need. Note that you can make rectangles with 2 or 3 rounded corners too if you want.The code’s not perfect, but I’m sure you can tidy it up a little bit.
alt text http://nevan.net/skitch/skitched-20100224-092237.png
Don’t forget that you’ll need to get the QuartzCore framework in there for this to work.