adding extra image when the UIButton remain pressed - iphone

I want to add an extra image when my button remains pressed.
Is it possible in iphone?

I guess you mean when the user makes a long press on the cell (tap down and keep finger down)?
You can add a UILongPressGestureRecognizer element to your button and specify its target and action like this:
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[self.button addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];
This will call the -longPress: method when a user long presses on the button. The callback function could look like this:
- (void)longPress:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan){
// do something like add an image
}
}
Hope this helps!

Of course Yes,
First take a custom button and set any image to it.
Then bind the following method wih Touch Down event of the button.
-(IBAction)buttonTouchedImage:(id)sender
{
[yourButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateHighlighted];
}

Set button highlighted image and make it highlighted = YES when button pressed. This should work I guess.

Related

Set UIImageView background color upon user intervention

I am developing an application in which I have a horizontal scroller.
The scroller area comprise of different colors [which are nothing but UIImageView of colors].
Above the scroller area is a canvas [which is also a UIImageView].
Now, what I want is that when I select any color in the scroller area, it should set on the canvas.
It is pretty much like a color picker but I am already providing solid color options to be selected by the user.
How should I go about it ? Any sample Code to get me started or my mind kicking would be awesome ?
Thanx a ton
To implement it easily.
Use UIView not UIImageView
Set UIView backgroundColor and place it on the scroll
Set UITapGestureRecognizer on views,Refer here
when a purticular view is touched you have its instance of view which
is touched
get the background color of the view and set it in the scroller
Can you take UIButtons instead of UIImageViews inside your scrollView to show colours to be picked.
This will make the implementation easier. User will select any colour button and you will receive a callback in buttons action selector method. Then based on button tag or any property you can set the colour to your canvas.
In current implementation it will be tricky as you need to know the exact content offset where the tap is done to make out which UIImageView was pressed.
coding steps:
In your scroll view add UIButtons instead of UIImageView like:
UIButton* button = [[UIButton alloc] initWithFrame:someRect];
//put some tag to button
button.tag = someInt;
//add selector to each button to get callback
[view addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
In the btnSelected method put this code:
-(IBAction)btnSelected:(id)sender;
{
UIButton* button = (UIButton*) sender;
if (button.tag == someInt) {
//do something like
canvas.backgroundColor = button.backgroundColor;
}
}
Instead of using UIImageViews or UIViews as suggested, I would put custom UIButtons for picking colors.
UIButton * colorButton;
//You will probably do these inside a for loop
colorButton = [UIButton buttonWithType:UIButtonTypeCustom];
[colorButton setFrame:CGRectMake(X, Y, buttonSize, buttonSize)];
[colorButton setTitle:#"" forState:UIControlStateNormal];
[colorButton setBackgroundColor:SOME_COLOR];
//Border visualization would be good if you are putting buttons side-by-side
[colorButton.layer setBorderWidth:1.0];
[colorButton.layer setBorderColor:[[UIColor darkGrayColor] CGColor]];
//You can use this tag to identify different buttons later
[colorButton setTag:INDEX+SOME_OFFSET]; //use loop index with some offset
[colorButton addTarget:self action:#selector(colorButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(void) colorButtonPressed:(UIButton *) sender
{
UIColor *selectedColor = sender.backgroundColor; //Or identify with sender.tag
[yourCanvas doSmtOnCanvas:selectedColor];
}
Initialize UIImageView then add gesture recognizer to all imageViews by calling this method.
- (void) setupImageView : (UIImageView *) imageView{
[imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(changeUIImageViewBackgroundColor:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tapGestureRecognizer];
}
Now change color of canvasView within the selector by following way:
- (void) changeUIImageViewBackgroundColor : (UITapGestureRecognizer *) recognizer{
UIImageView *imageView = (UIImageView *)[recognizer view];
[canvasView setBackgroundColor:[imageView backgroundColor]];
}
You can put a tap gesture in your scrollview and whenever a tap is made in the scroll check whether the tap is on the imageview. (You will get whether the tap point in on imageview or not) and according you can change the image.
Second alternative is to take a custom button and handle on its click.
Hope it helps

UIButton touchUpInside event not firing correctly

I have a tableview that contains a row with a custom cell that contains a UIButton. However, the button doesn't always fire the action. Here's my code:
submitButton = [[UIButton alloc] init];
[[submitButton layer] setBorderColor:[[UIColor whiteColor] CGColor]];
[submitButton setClipsToBounds: YES];
submitButton.backgroundColor = [UIColor grayColor];
[submitButton setTitle:#"Send" forState:UIControlStateNormal];
[self.contentView addSubview:submitButton];
[submitButton addTarget:self action:#selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[submitButton release];
This is called in the custom cell's -(id)initWithStyle:
The buttonAction method looks like this:
-(void)buttonAction
{
NSLog(#"Button Clicked!");
}
It seems that the only way I can get the buttonAction to fire is if I press down on the button and release somewhere inside the cell's frame, but not inside the button itself. Why would that be?
*UPDATE*
Problem still exists, but I found that the more consistent way to get the button to fire is to click and drag to the left or right and then let go, as long as I let go within the bounds of the cell/row.
UPDATE #2
It looks like if I use iOS 6.0, it works as intended. But on 5.0 or 5.1 it does not.
Try assigning the same method call to TouchUpOutside as well - you should then see it work every time. TouchUpInside is only fired if you lift your finger while still within the bounds of the button.
Try [submitButton sizeToFit]. I'm wondering whether your button has any size (since I don't see you giving it any).
Also: Create your button with [UIButton buttonWithType: UIButtonTypeCustom] instead of alloc-init.

UIButton long press with finger stationary

In my project I have the need to use a UIButton (or another component) to handle events using long press.
Let me explain, I should make that mind I hold down the button a timer to count the seconds and release to pressure stop, I tried with the management of UILongPressGestureRecognizer but is not the case because I recall the event when the button is held down but only if I move my finger, but I wish the timer went away and counted all the time in which the button is held down (with your finger stationary) and stopped counting when the finger is released.
Does anyone know how to help me?
Thanks
Use these two methods for buttons events. touchDown is called when you press the button and touchUp will be called when you lift your finger from the button. Calculate the time difference between these two methods. Also you can start timer in touchDown and stop/restart it in touchUp.
//connect this action with Touch up inside
- (IBAction)touchUp:(id)sender {
NSLog(#"up");
}
//connect this to tocuh down
- (IBAction)touchDown:(id)sender{
NSLog(#"down");
}
Updated
In coding you can write like this
[btn addTarget:self action:#selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:#selector(touchDown:) forControlEvents:UIControlEventTouchDown];
and in xib
The same I have done...As u said about UILongPressGestureRecognizer, I can't understand it..but u can write ur code inside-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
}. I have done same by using this method and got successful result..:). You even don't need to add timer,instead u can use...
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0;
lpgr.delegate = self;
I think this works perfectly..

Small UIButton on top of larger UIButton

I have a smaller UIButton which is on top of a larger UIButton.
The problem right now is that if I tap the smaller UIButton, it also will trigger the larger UIButton. The code I'm using to determine if the buttons were tapped is:
if(CGRectContainsPoint(button1.frame, location)) {
}
Is there a property of the buttons or some automated way to make the smaller button not effect the larger button?
I know I could alter the code above to say if its within button1's frame and not within button2, but is there another way to do it?
UIControl (which is the superclass of UIButton) passes itself as the only parameter to its target using the action selector. Make use it, it's there for exactly these cases!
[smallButton addTarget:self action:#selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
[bigButton addTarget:self action:#selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
// ...
- (void) doStuff:(UIButton *)btn
{
if (btn == smallButton)
{
// smaller button was clicked
}
else
{
// bigger button was clicked
}
}

how can I get image tag from different image views on tapping on it

I want to used image view from nos. of image views in scroll view on single tapping any particular image view.
If you are insistent on using images instead of buttons you can use Gesture Recognizer. Create an imageView and enable its userInteraction
UIImageView *testImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"someImage"]];
testImageView.frame = CGRectMake(30.0,30.0,60.0,40.0);
testImageView.tag = 30;
testImageView.userInteractionEnabled = TRUE;
[tempPlotView addSubview: testImageView];
[testImageView release];
Now allocate a gesture Recognizer object and add it to your imageView...
UITapGestureRecognizer *testGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singletap:)];
[testGesture setNumberOfTapsRequired:1];
[testImageView addGestureRecognizer: testGesture];
[testGesture release];
Now in the selector "singleTap" you can do whatever your action is..
-(void)singleTap:(UIImageView*)sender{
if(sender.tag == 30){
//do your stuff here...
}
}
Hope this help...cheers....
Create a button and set image as background.while creating button you can set tag like
button.tag=yourtag;//your tag integer value
[button addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchDown];
[button setBackgroundImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
and implement this function in your clss
- (void)buttonTouched:(UIButton *)sender
{
NSLog(#"touched %i",[sender tag]);
}
while tapping the particular button this function will get called.
Rather than use image views, you could use UIButtons with image views as their content.
That way, you'll get a callback when a given image is tapped with a reference to the button. From there you should be able to get the tag of that which has been tapped!
I hope this helps,
You can do according to what Nick has suggested.
Or else, you can create a subclass of the imageview. Set frame and tag to it.
In touchesEnded method of this custom class, you can find which imageview it is based on its tag.