How to configure UIButton to use showsTouchWhenHighlighted? - iphone

I have a UIButton that's 90x90 in size and I would like it to glow when tapped. Using showsTouchWhenHighlighted will make it glow but it's glowing out of the center of the button and for a small radius only. This might work well for a small button like the info button, but for my button size, it's not. Is there anything I can do to make it glow from the outer perimeter of the button before I resort to designing a highlighted state?

I can think of 2 ways to do what you want:
Use different highlighted background image for your button's highlighted state,
Use CoreGraphics to draw the glow effect in your button's IBAction (touchDown is a decent place).
This is assuming you want some kind of custom glow effect of course - UIButtons already naturally have a highlighted effect when touched.

Related

Swift Animation - Circle behind button

I'm trying to add some animation in my app. I would like to show a circle when I tap a button.
I don't really know what could be the best way to do that.
Do I have to create on the storyboard a view with a background, and I add corner radius and use the isHidden property ?
Do I have to create the circle programmatically ? Maybe better because I have many buttons ?
BEST WAY:
DEMO
1. Assets Used
Set Default Image on Default State of UIButton
Set Highlighted Image on Highlighted State of UIButton

Custom UIButton image buttontype

If you can set a button's image even if it's button type is rounded rectangle, then what is the purpose of the button type to begin with? I usually use the custom button type, but does this in fact actually make a difference?
You can use UIButtonType to make a button of a standard type: a detail disclosure, an info, and so on. In these cases you would not need to set the image at all - the system will do it for you automatically.
When you pick rounded rectangle and set the image, the image would be clipped to the rounded rectangle's border; when you pick custom style, there will be no clipping.
Yes this definitely makes a difference. For example, if you have two buttons, One RoundRect and the other 2nd Custom.
Try to set a background color on both buttons. You will see the difference. The Custom Button has occupied the background color properly and the round rect has few issues.
Even with background Image, you can check both. The Custom button has no issues with setting images, as it has a transparent background and the round rect will have an issue on its corners.
UIButtonType definitely have difference of this.
I hope this is what you are looking for.

UIButton with changing images when pressed - but unattractive effect when changing them

I have a button that changes its image to different images depending on what modus we are in.
The images are set with:[modusBtn setImage:cx forState:UIControlStateNormal];
The images have round corners and the button is of Type "Custom".
Now when the button is pressed one can see another image in the upper and lower edge background.
Really weird - the buttons ALL have round corners - but for example when the blue modus is on and shows the blue image and one presses the blue button now - one can see red little edges only on the left two corners.
I already played around with all the button attribute settings in IB, but no luck.
Did anyone have a similar experience when changing button images?
So far all my button images where 100% rectangular and did not have round corners - therefore I never experienced this before.
Many thanks!
Use this UIButton's propriety for your button.
avatarButton.clipsToBounds = YES;
Whenever you set an image for custom button make sure to set
yourButton.backgroundColor=[UIColor clearColor];
The effect was happening as your button was using default background transitions during changing the state.

UIButton - unwanted background well

I'm creating UIButtons (type = UIButtonTypeCustom) with a custom background drawn by an artist; unfortunately UIButton is adding an unwanted 'well' effect around the backgroundImage (as specified via setBackgroundImage). Is there a way to disable the well? It's not a simple drop shadow, so messing with the CALayer properties doesn't seem to help. I realise I could use UIControl, but that's considerably more work, since I need to handle the label subview myself, and get the artist to produce highlighted versions of the artwork - UIButton is doing all that nicely, if I could only disable the well effect.
Put your image inside a UIImageView, and then position your UIButton on top of it. Use the "custom" style, which has no UI to it at all and is totally invisible.
If you want to change ("highlight") your button image when the button is hit, just change the image contained in the UIImageView in whatever method your UIButton targets.

iPhone button with non-rectangle shape?

How can I get a non-rectangular shaped button?
For example I do have a PNG image with alpha transparency.
How can I set the shape of a button to this image without the transparent color.
Like the others say you should have a reasonable surface for registering touch events, but I'm sure you know that and have your reasons so I'll just tell you how I did it:
I needed to do this not so long ago and what I did is what Sixten Otto just suggested. There are some hints in my original question ("UPDATE" section) for getting the alpha value for your image at a certain point:
How to create a transparent window with non-rectangular buttons?
Edit: I suggest subclassing UIControl in the example below but if you don't need any special behavior on your button apart from the "non-rectangleness" of it then just subclassing a borderless UIButton set up with your PNG will do the job and require less work. You have more control on the control's behavior by subclassing UIControl and doing it the "hard way" though.
I would suggest that you subclass UIControl and override the touch event methods, then check for alpha under the tapped point and not handle the event if alpha == 0. For drawing you override drawRect: and use the NSImage's drawInRect:fromRec:operation:fraction: method in there to draw your image in the control's frame.
First you need to load the image and get a bitmap representation of it:
buttonImage = [NSImage imageNamed:#"myButtonImage"];
buttonImageRep = [[buttonImage representations] objectAtIndex:0];
Then you can draw it to the control's view: (where stateImage is a pointer to the image that must be drawn depending on if the button is pressed or not)
- (void)drawRect:(NSRect)aRect {
[stateImage drawInRect:[self bounds] fromRect:NSMakeRect(0.0,0.0,[buttonImage size].width,[buttonImage size].height)
operation:NSCompositeSourceOver fraction:1.0];
}
At this point you have drawn your button with your png image. You can override the touch event methods and handle the event if the alpha is not 0:
NSColor *colorUnderMouse = [buttonImageRep colorAtX:mouse.x y:mouse.y];
float alpha = [colorUnderMouse alphaComponent];
That's what I did and it works wonderfully. Hope this helps!
N.B: My example is for the Mac but it should also work on the iPhone, maybe with some slight modifications.
It sounds like you are looking to have the clickable area of the button exactly match the PNG you are using.
If that is what you are looking for, I would firstly say to not do that. The iPhone is pressed using a finger, which generally doesn't have the accuracy to distinguish such a small region.
However if you are stuck on the idea, then the solution is to not use buttons at all, instead handle the click in a parent frame and manually interpret the X/Y value of the click to determine if it is in some bounding region (In the case of a rounded edge button, would likely consist of oring the result of checking 4 circles and 2 rectangles)
Edit:
Realizing part of your original question, I noticed you mentioned you wanted to handle the function automatically based on the Alpha channel. While I would recommend my method of bounding regions, you could in theory accomplish this by sampling the PNG to test the Alpha channel at a value offset by the origin of the button. Potentially even doing this in a normal button's click event.
Rectangular buttons are a button type, you can use the custom button type and assign an image to get rid of the rectangular edges of the button...heres a reference to button types http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/doc/c_ref/UIButtonType and to UIButton http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html
As Guvante says, it's really not a good idea to rely on precision touches. Apple, for instance, recommends that controls be at least 44px across.
I'd recommend using a UIButton of type UIButtonTypeCustom, set the button's image to your PNG, and register for touch events (see here for more on event handling). Then, in your action method, you can get the coordinates of the touch out of the event, and test those coordinates against the alpha of your image to see whether you should treat it as a "real" touch or not.
Another option could be to split your button up into multiple, rectangular parts.
#Form right about using 'drawRect' to implement custom shape for button.
But better method to exam touch inside or not inside button is using of
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
You can create your button transparent for touches in some parts.
If you can't use custom image for button (for example if you draw text on the button) you can use new method to draw subviews hierarchy into a image:
- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates;