How to draw UIButton with shape as below? - iphone

I want to draw UIButton in following shape :
I have cut the image, so the edges are not straight. Please consider them straight. Also the bottom right corner is as shown. It is cut in quarter of circle.
I am not able to draw this shape for UIButton. How to do this?

Interesting question. I did a search and found two simple solutions that seem much simpler than the GB2. They override either hitTest:withEvent: or pointInside:withEvent: to take into account if the current pixel is transparent or not. So just take your image, set the lower right corner to transparent in a graphics editor, and use one of these classes.
http://iphonedevelopment.blogspot.com/2010/03/irregularly-shaped-uibuttons.html and
http://oleb.net/blog/2009/10/obshapedbutton-non-rectangular-buttons-on-the-iphone/

[self.myButton setBackgroundImage:[UIImage imageNamed:#"myOddlyShapedImage"] forState:UIControlStateNormal];

I got the same problem, I found a different UIBUtton libraries and solved this
search for:
GB2ShapeCache
and implement if you don't find it I could upload it and I give you the link.
You only put the library in your UIBUtton and it will work
I hope it helps :)

Related

Make fix size round button as image in iphone sdk

I am developing an application in which I need to round button as image (as I uploaded).
It Show two corner are straight and the rest is in round shape. I tried withmybutton.layer.cornerRadius but make round whole image. I also tried to just make custom button but it leave extra space in button around image i need to fix size button as image show.
Please help me. Thanks in advance. Happy Day.
Here is a one good tutorial on this:
http://iphonedevelopment.blogspot.in/2010/03/irregularly-shaped-uibuttons.html
you can download code from here
https://github.com/ole/OBShapedButton
Hope this helps
An alternate but not really a good one first add an imageView add this particular image over it than add an custom invisible button over the image by adjusting size of the button it will not cover the whole image though but still it will work fine...actually i did use this approach in one of my app :p
OBShapedButton might help. It's an open source UIButton subclass optimized for non-rectangular button shapes. If it still doesn't work, making your own buttons isn't that difficult either. Use a UIView and handle touches within the view. For eg, in touchesMoved:, set the active image and in touchesEnded:, handle the click event!

UIBarButtonItem clone

I have an UIButton subclass and I need its look
to be identical to an UIBarButtonItem when placed on the
classic blue tinted navigation bar.
The UIBarButtonItem has a border with a kind-of gradient,
being darker at the top and blue-ish at the bottom, which
I suspect it's done with some alpha trick. The bottom
looks recessed too.
There's also some overlay which makes the button a little bit darker
and even more when in the selected state.
Can anyone help?
The short answer is that you are going to have to spend some time learning how to draw gradients and shadows in Core Graphics.
The relevant documentation is called "Quartz 2D Programming Guide".
After you learn how to draw shadows and gradients, you are going to have to spend quite a bit of time zoomed in comparing what you are drawing vs what the button looks like.
You didn't ask, but this is what I would do:
Subclass UIBarButtonItem and add your custom functionality in there. Let UIBarButtonItem draw itself.
It might be easier for you just to use a photoshopped image. Use the image for your button. This way you don't have to worry about custom drawing code.
You can use Three20 library,
specially have a look at sample TTCatalog app.
Look at the source file "StyleTestController.m"
https://github.com/facebook/three20/blob/master/samples/TTCatalog/Classes/StyleTestController.m
I'm sure you'll find your answer here.
Cheers,

Touchable area of a UIButton

Is there any way to define which area of a UIButton is clickable? In my case I would like to have PNGs with an alpha channel overlapping. These PNGs should act as buttons - but only where alpha is > 0. Is there a way?
Best
Stefan
Don't do that. According to the Apple Human Interface Guidelines, all UI elements should behave in a consistent manner.
OBShapedButton is an awesome project that will give you a button that will only respond to taps that are in the images area. I've messed with Irregularly Shaped UIButtons and I think OBShapedButton is a little more accurate. Not trying to bash on the other one at all. I've used the other and it works just fine. I've just seen a more accurate hit area with this one.

Creating non-square buttons with highlights in iPhone app

I want to create a "play" button that has a play image (triangle) and highlights on touch.
I kind-of figured out how to do this, but it's not really right. The frame I create for the button is square, and the entire square button lights up when touched.
Instead, I want only the triangle to get highlighted, similar to how iPod buttons work. I don't know how to do this though.
My png is square with a transparent background, if it matters. Does that need to change at all?
Any suggestions on how I can accomplish this are much appreciated.
psychotik,
You can set different button images for different buttons states. This will allow you to use an image to give the appearance of highlighting around the edges of the play triangle instead of using the iPhone generic highlight feature.
Example code (fyi.. some people don't like to use imageNamed and don't trust it.. but use whatever you want to load some image in, this is just generic code):
[someButton setImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
[someButton setImage:[UIImage imageNamed:#"someImageHighlighted.png"] forState:UIControlStateHighlighted];
More information on these here:
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIControl_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIControlStateNormal
P.S. You can also do this in Interface Builder if you have your button created there.

UIButton: Need a circle hit area

Ok I have 6 custom UIButtons. Their normal state image are all circles images.
They are all spaced out equally but all the circles touch each other.
The problem with the custom UIbutton (which has a circle image on it), is that the hit area of that button is square, and the corners of this square overlaps the hitarea of the other custom button's hitarea.
How do i make the hit area of a UIbutton whos normal state has a circle image, be only clickable on that circle only, rather than the normal square hit area?!
I hope that someone can find a way for me to solve this problem that i currently am having!
Thanks in advance
Pavan
If the square area of a "circle" is clicked on, you should then check if the distance between the center of the circle and the coordinates of the click are less than the radius of the circle. If not, then the "hit" can be ignored.
Thanks for the input demi, I just basically put hidden buttons on top of the images. They weren't really circles, they were more complex shapes; hence the reason I didn't bother about the maths, so I just placed hidden buttons on top of the images themselves and make those hit areas and cover appropriately until I get the desired hit area coverage I need.
Although not very neat, it works well! :D
Thanks once again for the swift reply demi.
OBShapedButton is an awesome project that will give you a button that will only respond to taps that are in the images area. I've messed with a few projects that say they do the same thing, and this one is the best I've seen so far.