I have a button that should move 2 pixels down when pressed down and jump back to the original location when the press ended. I've tried touchesBegan and touchesEnded but those don't work with a button. I've also tried exchanging the button with an IUView and calling the button's method in touchesEnded but that gave the button a weird delay before moving down. Any ideas?
Did you try using the Touch Down and Touch Up events? Try moving the button location on Touch Down and restoring it to its initial value on Touch Up (Inside and Outside).
EDIT: Here's a code example:
- (IBAction)moveButtonDown:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y + 2, btn.frame.size.width, btn.frame.size.height);
}
- (IBAction)moveButtonUp:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y - 2, btn.frame.size.width, btn.frame.size.height);
}
Assign moveButtonDown: to the buttons "Touch Down" event and moveButtonUp: to both its "Touch Up Inside" and "Touch Up Outside" events. Tested and working, here at least ;)
EDIT: Thanks fichek, here's a solution with CGRectOffset instead of CGRectMake:
- (IBAction)moveButtonDown:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectOffset(btn.frame, 0, 2);
}
- (IBAction)moveButtonUp:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectOffset(btn.frame, 0, -2);
}
Related
I have a UIButton that has only an UIImage set, when Clicked (long press) the images changes automatically with a shadow, What I want is that even if the button gets clicked but not with a long press the images should have the shadow. I already tried from IB to set the UIButton properties Reverse on Highlight and Shows Touch On Highlight but with no results
This is about UIControl Class. It's default to be with shadow. So if you wanna customize it, you should addTarget to your button to every action on UIButton like tapping,long press,touchUpInside and I think it s not about long press it's just you can't realize the difference on tapping.
in one of my project I customize my button actions like this:
buyButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width*3/4-2, 0, self.frame.size.width/4+4, self.frame.size.height)];
[buyButton addTarget:self action:#selector(buyButtonClicked:) forControlEvents:UIControlEventTouchDown];
[buyButton addTarget:self action:#selector(buyButtonNormal:) forControlEvents:UIControlEventTouchUpInside];
[buyButton addTarget:self action:#selector(buyButtonNormal:) forControlEvents:UIControlEventTouchUpOutside];
and methods of this like:
- (void) buyButtonClicked:(UIButton *) sender {
if ([self.backgroundColor isEqual:[Util UIColorForHexColor:#"fede32"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"fdca2e"];
}
else if([self.backgroundColor isEqual:[Util UIColorForHexColor:#"cfd3dc"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"c0c5d0"];
}
}
- (void) buyButtonNormal:(UIButton *) sender {
if ([self.backgroundColor isEqual:[Util UIColorForHexColor:#"fdca2e"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"fede32"];
}
else if([self.backgroundColor isEqual:[Util UIColorForHexColor:#"c0c5d0"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"cfd3dc"];
}
[delegate purchaseOffer:selectedOffer];
}
If you need more information about UIButton actions there are a lot of question on this site.
I have an UIScrollView page . On this UIscrollView I have different images inside buttons you can see it in below code, and I want to load this images in UIWebView when I press the buttons, My question is how can I know which button are click. Would you please give me some hint for implementing this?
I'm new to objective-C
Here is my action: I want to have one action for each images, rightnow I have just one action for all images, I one to have one for each image
- (IBAction)openAction:(id)sender {
NSLog(#"test");
}
The actual button is passed to the IBAction. Setting a tag on the button is one way of doing it. If you created outlets for all your buttons in Interface Builder, you can simply do something like this:
- (IBAction)openAction:(id)sender {
UIButton *b = (UIButton *)sender;
if ([b isEqual:self.outletButton1]) {
// Do something with button 1
}
else if ([b isEqual:self.outletButton2]) {
// Do something with button 2
}
}
Don't use Same Tag for Button. Use Different Tag for different button.
For ex [btn setTag:1] .
you can use [btn setTag:i];
Dynamic value
Give each button a unique tag, in your code or in Interface Builder.
Then in your action you can do:
- (IBAction)openAction:(id)sender {
UIButton *b = (UIButton *)sender;
NSLog(#"button %d is pressed", b.tag);
}
I see that you already give your imageviews tags. Those are not buttons! You should instead create UIButtons whose content are images. See UIButton's setImage:forState:
Edit: In response to your question below, here's an example:
NSMutableArray *bArray = [NSMutableArray arrayWithCapacity:kNumImages];
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btn setImage:imageName forState:UIControlStateNormal];
btn.tag = 100+i;
[bArray addObject:btn];
}
You don't have to add UIButton over UIImageView. Instead, you can set the image for UIButton like this.
[btn setImage:[UIImage imageNamed:#"image1"] forState:UIControlStateNormal];
You can access the image in the action like this:
- (IBAction)openAction:(id)sender {
if([[btn currentImage] isEqual:[UIImage imageNamed:#"image1"]]){
}
NSLog(#"test");
}
I have button created programmatically in the UIImageView. This view was also created programmatically. When image in this view is tapped the button is created, now I want to capture when this button is tapped but I'm unable to here is my relevant code :
- (void)imageTapped:(UIGestureRecognizer *)sender{
MyImageView *myView = (MyImageView *)sender.view;
NSLog(#"Image tapped is => %#", myView.currentImageName);
//add button to view
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(pressedPlay:)
forControlEvents:UIControlEventTouchDown];
//button image
UIImage *backgroundImage = [UIImage imageNamed:[NSString stringWithFormat:#"playbutton.png"]];
button.frame = CGRectMake(85.0, 175.0, backgroundImage.size.width, backgroundImage.size.height);
myView.userInteractionEnabled = YES;
button.userInteractionEnabled = YES;
[button setImage:backgroundImage forState:(UIControlStateNormal)];
[myView addSubview:button];
}
Now this code should handle the tap but for some reason it doesn't :
- (void)pressedPlay:(UIGestureRecognizer *)sender{
//MyImageView *senderView = (MyImageView *) sender.view;
NSLog(#"%#", #"I Presed button");
}
Any reason why this is not working?
Your code suggests that you are using a UIGestureRecognizer (most likely a UITapGestureRecognizer) to detect the initial tap. The problem is that UIGestureRecognizer is being greedy and stopping the UIButton from receiving the touch.
You will need to do some shifting to disable the gesture recognizer after the initial detection and then reenable it after the play has been tapped.
UIGestureRecognizer has the property
#property(nonatomic, getter=isEnabled) BOOL enabled
or you can choose to implement UIGestureRecognizerDelegate and provide an implementation of
gestureRecognizer:shouldReceiveTouch:
a very simple attempt could look like:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
{
return ![touch.view isKindOfClass:[UIButton class]];
}
and don't forget to set the delegate on the gesture recognizer
myGesture.delegate = self;
set the button frame from:
button.frame = CGRectMake(85.0, 175.0, backgroundImage.size.width, backgroundImage.size.height);
to:
button.frame = CGRectMake(0,0, backgroundImage.size.width, backgroundImage.size.height);
it looks like you are setting button frame outside the imageview's frame and hence the button is not actually getting any interaction.
The Button frame is likely setting the button outside the bounds of the UIImageView, frames are always relative to the superviews coordinate in this case the UIImageView.
[button setExclusiveTouch:YES];
I have placed a pause button in UI, where when the user clicks the button, the image of button from pause image it has to change to the start image. I have placed both the start and the pause images in the bundle, I am able to do this,but when i click on start again , it should show pause image button,How to do this, the following is the code i am using,
-(IBAction)btnClked:(id)sender
{
[pauseButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
UIButton *btn=(UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
}
You need a variable to keep track of that state change. A BOOL is fine.
But using your code, you can "abuse" the tag property of UIButton to tell if it's playing (tag == 1) or paused (tag == 0)
-(IBAction)btnClked:(id)sender {
UIButton *btn=(UIButton *)sender;
if (btn.tag == 1) {
btn.tag = 0;
[btn setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
} else {
btn.tag = 1;
[btn setImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
}
}
You can use UIButton's selected state to do the same. For example, while initiating the button you have to define 2 images. One for normal state and other for selected state. When touch event of the button fired, you have to make it selected. If it is already selected you need to deselect it. This is much better, than maintaining additional variables to check selected state of the button. Also you could improve the performance with this approach.
Code example
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *startButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
[startButton setBackgroundImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
[startButton setBackgroundImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
[startButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startButton];
}
- (IBAction)modeButtonClick:(id)sender {
UIButton *startButton = (UIButton *)sender;
[startButton setSelected:![startButton isSelected]];
}
If you are creating the button from story board or XIBs set the backgroud image for default state config and selected state config.
I have this action for the "Touch Up Inside" event of a UIButton that should create a new UIButton right below the sender:
-(IBAction) cloneMe: (id) sender{
if (!currentY) {
currentY = [sender frame].origin.y;
}
UIButton *clone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect cloneFrame = [sender frame];
cloneFrame.origin.y += currentY + cloneFrame.size.height + 30;
clone.frame = cloneFrame;
currentY = cloneFrame.origin.y + cloneFrame.size.height;
}
It doesn't work and the new button is never displayed. Anybody knows what's going on?
You are forgetting [sender.superview addSubview:clone];
You create button but don't add it to view hierarchy - you missing
[self.view addSubView: clone];
call or similar