How to Better Drag a UIButton? UIButton UIControlEventTouchUpInside Event Is Not Called - iphone

My draggable button is using these events to be dragged:
[button addTarget:self action:#selector(beganDragging:)
forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragOutside | UIControlEventTouchDragInside];
[button addTarget:self action:#selector(exitDragging:)
forControlEvents:UIControlEventTouchUpInside];
It's straight forward above. Method wasDragged adjusts the center of the UIButton to the UITouch location. However, there are cases when I end up with button being hung (normally it snaps to grid after finger release) because UIControlEventTouchUpInside is never called!
How? Let's say I start dragging a button and while holding it I make a screenshot (Power+Home). When I release the button no events are ever called (i.e. UIControlEventTouchUpInside). Therefore, my button is stuck and not snapping to grid which is in method exitDragging
Any light how to make sure exitDragging always runs?

Maybe it is easier using the UIView APIs with touchesBegan, touchesEnded etc.
It seems you are anyway not using the UIButton functionalities. I think that managing a drag with the target mechanism of UIButton is not what it was intended for.

Related

How do I make a section of my UIView a clickable area

I want to make a certain section of my UIView clickable. When clicked, it triggers an IBAction, as would an UIButton.
How do I do this?
Add Custom UIButton on your view, what ever position you need to clickable, Then perform
action on it.
UIButton *button = [UIButton buttonWithType:0];
[button setFrame:CGRectmake(0,0,0,0)]; //What ever clickable area, provide here
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
Using UITapGestureRecognizer and put transparent button over the area where you want clickable section available
Add UITapGestureRecognizer to your UIView. When you receive a tap, check your recognizer's locationInView:. Trigger the action if the tap happened in the area that you would like to make tap-sensitive; otherwise, ignore the tap event.

How to make image behind invisible button look like it was pressed down?

Hi I have a couple of images that are covered by an invisible button. Is there a way that I can make it look like the images were pressed down when I press the invisible button? Thanks!
You can put the image directly onto the button, you dont need a seperate UIImageView.
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateSelected];
The are a few more UIControlState constants.
Take a look at the docs:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIButton_Class/UIButton/UIButton.html
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/c/tdef/UIControlState

IOS Custom UIButton

I currently work on a small application in which I have several UIButtons. Each of them has an UIImageView beneath it. At the moment, I associate each UIButton with each UIImageView (all are named via an IBOutlet - UIButton and UIImageView). When I press a button, the underlying image changes. I would like to know if it's possible (and how) to create a button which contains an underlying UIImage (not a UIButton image or background image), a sort of new object.
This image is slightly larger than the associated UIButton, like the apple calculator basic apps, and this image changes when I press the button.
I've tried several solutions (certainly incomplete) and I'm beginning to despair.
My current solution is functional but I do find it not particularly elegant.
UIButton has both image and backgroundImage as its properties.
To achieve what you are looking for, I would actually recommend you to stick to just a UIButton, and then using contentInset to position the image over the backgroundImage so it creates the effect I think you are looking for. This should be quite easy in interface builder...
you can make an invisible button and behind it make the image..
This will give you what you want but should be avoided at cost...
Stick to UIButton..
I believe even in calculator app they are only UIButtons and nothing else..
they have been coded to been momentary selected ..so that's why you see them kind of changing highlight for a small fraction and each button have been coded to perform specific function(+ - / equal .)
Update : i might be in doubt but if you asked for a button to have an image view inside it..
then here is the answer.
Make UIbutton. Make an ImageView and then call [theButton.view addSubview:ImageView];
Since both UIbutton and UIImage view inherit from UIView.. you can add as many subviews to them..that is inside them
If I understood your question correct then you may try the following:
UIButton *button = [UIButton alloc]init];
[button setImage:buttonImage forState:UIControlStateNormal]; when button not pressed
[button setImage:buttonImage forState:UIControlStateSelected]; when button tapped
[button release];

limiting the touchable area in a UIButton in IPhone?

I have three custom buttons with non-rectangular images close to each other in my view. Then I have a problem with touchable area's of each button overlap with other buttons. So how can I limit the touchable area of each buttons to get the corresponding actions?
You can overwrite -pointInside:withEvent:, that internally will be used for hit testing.
A nice project using this technique is OBShapedButton, where transparent pixel will not trigger a hit.
Use two components. A UIImageView with a smaller UIButton on top.
You should create custom Type Buttons and add required images on each using this code:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[btn setFrame:frame];
Creating buttons with images this way will not result in overlapping images issues !!

How to highlight the button untill the next view is changed in iphone?

I have created five buttons in the view controller. If i clicked the button it goes to the corresponding view. Now i want to display the button in highlighted state when it is clicked. It should go back to the normal state only when i click the other button. I have set the another image for highigthting buttons when i clicked it, but it shows that highlighted state only one sec. Now i want to display the buttons highlighted till another button is clicked. Same like a Tabbar operations.(I have used buttons instead of tabbar for the requirements).
Now i have used the following code,
void didLoad
{
[btn1 setImage:[UIImage imageNamed:#"ContentColor.png"] forState:UIControlStateHighlighted];
[btn2 setImage:[UIImage imageNamed:#"bColor.png"] forState:UIControlStateHighlighted];
[btn3 setImage:[UIImage imageNamed:#"ShColor.png"] forState:UIControlStateHighlighted];
[btn4 setImage:[UIImage imageNamed:#"PicturesColor.png"] forState:UIControlStateHighlighted];
[btn5 setImage:[UIImage imageNamed:#"infoColor.png"] forState:UIControlStateHighlighted];
}
Please help me out.
Thanks.
You should be manipulating the image for selected state, not for highlighted state. Highlight will be unset on touch up or touch outside, as you have seen, whereas selected is persistent.
Simple solution would be...
on every click on any button change it's image (For normal state) to highlighted image for that particular button and set other 4 button's image to normal image...
It sounds like you should be using a UISegmentedControl with 5 segments instead of 5 buttons. It behaves this way already, a segment stays highlighted until you select a different one (unless you set the momentary property).