Creating non-square buttons with highlights in iPhone app - iphone

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.

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!

How to draw UIButton with shape as below?

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 :)

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.

iphone - how to extend a button touch area?

I have 5 round buttons in a row, each one, 40x40 pixels. Between each one, I have 20 pixels.
40x40 pixels is too small to touch, but as I have 20 pixels of space between each button I can extend the button touch area to 60x60 pixels, making it easy to touch. I could simply using the dirty solution of creating a square 60x60 pixels transparent image, put this over the button and make this touchable, but I know it is possible to extend a button touch area by creating a custom class and changing a parameter.
I know this is possible because I saw this done before (but I cannot find the URL). I know it is something related to hitTest.
How can this be done?
Thanks.
Eiko is absolutely right. Here's some simple code you can use that is independent of the button's location that somebody gave me for expanding the Info button frame.
CGRect newInfoButtonRect = CGRectMake(infoButton.frame.origin.x - 25,
infoButton.frame.origin.y - 25,
infoButton.frame.size.width + 50,
infoButton.frame.size.height + 50);
[infoButton setFrame:newInfoButtonRect];
You might want to watch out if you're using a background image as opposed to an image ( button setBackgroundImage: vs button setImage: forState:) because a background image will stretch with the frame while a normal image will not.
You can always make its frame bigger, i.e. yourButton.frame = CGMRectMake(0,0,60,60);
I usually do this when adding one of the info buttons.

iPhone Slider Sensitivity

I have custom slider that I use the following touch event to respond to:
[bSlider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
I find it very difficult to use, it does not respond to touches about 50% of the time. Compared to the volume slider with the music player that works great every time you touch it. Are there any secrets to creating a slider in code that will make it more responsive?
I had the same problem with my custom UISlider and solved it by using a larger "thumb" image with transparency around the outside.
I also had problems with my callback function running slowly and causing the slider to be very laggy and clumsy to use, which I fixed by adding a simple check at the top of the function:
int val = ceil(sliderView.value);
if (val == _lastSliderVal) return;
_lastSliderVal = val;
// .. code to update various display elements based on slider value
After changing both of these, the slider works beautifully.
The problem is the event you are tracking. UIControlEventTouchUpInside means that your -start method will only get called if you lift your finger up while it is still in the bounds of the control, something that's not easy to do on a slider.
What you probably want is UIControlEventTouchUpOutside and/or UIControlEventTouchDragExit, which will call -start either when you lift your finger outside of the control's bounds, or when your finger is dragged from within a control to outside its bounds, respectively.
See the UIControl reference for more info.
Well I tried several things including the suggestions above but never got my slider to work as well as the iPhone volume slider in the music app. The only thing that helped was to add a clear background around the button image to make the touch space larger. That created problems getting the button to slide all the way to the bottom and top of slider that I never completely resolved. Now with OS 3.0 is seems to be working much better.