Currently i am working in iPhone application, Using two buttons, then i set the image both button1 and button2, then i tried to drag a image from button1 to button2, Is it possible to do this? please help me
Thanks in Advance
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"hearta.png"] forState:UIControlStateNormal];
button1.frame=CGRectMake(10, 60, 50, 50);
[button1 addTarget:self action:#selector(Button1Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:[UIImage imageNamed:#"club4.png"] forState:UIControlStateNormal];
button2.frame=CGRectMake(61, 60, 50, 50);
[button2 addTarget:self action:#selector(Button2Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2]
You can get reference from this demo project.
it's very nice.
Also another link is here
There are more than one ways of doing this ...
You can either create a UIImageView on the click event or action of button1 and use animation to drag that image till button2 where you make the imageview.image equal to button2's image , side by side making the original imageview as nil so that you can show it disappear into button2.
Also, instead of buttons you can use UIImageViews directly and make and animation effect on the click methods there too.
I have interstitial fullscreen ad in my iOS app. I do not have control on HTML code for ad. I do not wish to modify ads code provided by ad network.
Sometimes ad is not loading due to network problems.
There is no way to close interestitial. App is unusable after that.
I wish to place native close UIButton over UIWebView.
I do not use IB, I have to make such button programatically.
How to do that?
For example,
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(closeAd:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Close" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
Then put whatever code you like in the method
-(void)closeAd:(id)sender;
I have a problem. I've a normal viewcontroller where I add UITextViews in a scrollview. To these UITextviews I add a dynamic number of UIButtons, to which I want to add a target. The reason I add them to the UITextViews is that adding them to the viewcontroller adding the text view's origin will make them end up outside the screen and not scroll, of course. But when I do that, the buttons trigger the action.
My question is: how do I specify the viewcontroller as target? Using self or using the var created in the appdelegate as target does not trigger it. If "two superlevels up from the textview" will work I will use that, just don't know how to specify it correctly.
My code:
UIImage *img=[UIImage imageNamed:#"phonebutton40x30.png"];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:#selector(phoneemail:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:img forState:UIControlStateNormal];
btn.tag=700+i;
btn.frame=CGRectMake(xoffs+3, yoffs+19, 50, 38);
[tvMain addSubview:btn];
Just remove colon from your code
[btn addTarget:self action:#selector(phoneemail:) forControlEvents:UIControlEventTouchUpInside];
The corrected one is
[btn addTarget:self action:#selector(phoneemail) forControlEvents:UIControlEventTouchUpInside];
If I guess, this will probably help you.
Solved it back then by adding the buttons to the scrollview instead of the textview.
I've got a UIButton that I create at runtime. I put a title on it and have a few states set for it. I DO NOT have an image set to the background and want to avoid this. So the button should simply be a custom button with text in the middle.
When I click on it I see 0 feedback that I clicked on it. I can set the text to change states, but I don't get that grey highlighted color on top.
I've tried:
[button setShowsTouchWhenHighlighted:YES];
And get nothing. again I can set the title color to chagne based on the state but thats not really what I'm looking for. Any Ideas?
Thanks for any and all help.
change button type as:
UIButtonTypeRoundedRect
instead of UIButtonTypeCustom
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 200, 44); // position in the parent view and set the size of the button
[myButton setTitle:#"Click Me!" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[superView addSubview:myButton];
I thought to be clever and just put an transparent UIButton over an UIImageView with the exact frame size, so that I can wire it up easily with any event I like, for example TouchUpInside, and make it call a action method of an view controller when the user touches it. Well, it works until alpha is below 0.1f. If I do 0.01f, it will not work. So to get it work, when looking a long time on the screen, you'll see that 0.1f of alpha shining through. And that's totally disgusting ;)
It seems like iPhone OS trys to be clever and won't catch events on the button if it's visually not there. Any idea how to solve that?
Sure I could make a subclass of UIImageView and implement touchesBegan:... etc., but it doesn't feel really elegant. I mean...when I want to hyperlink an image on the web, I would never want create my own HTML element for that image, just to wire it up to an url when it's clicked. That just doesn't make a lot of sense to me.
You should be able to set the button's 'Type' to Custom in Interface Builder, and it will not display any text or graphical elements over the UIImageView. This way, you don't need to adjust the alpha. If the view is built from code, use:
button = [UIButton buttonWithType:UIButtonTypeCustom];
In addition to UIButtonTypeCustom, I set the button text colors to the following:
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
[button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
The best way of doing this is:
myButton.hidden = YES;
You can't use the buttonType property because it is a read only property. Only way to use it is when creating your button dynamically.
i also beleive you can assign an image to a button.
The image can take up the entire frame and can also have no other artifacts of the buttone if you set it up right.
check out the Property
UIButtonInstance.currentImage
That way you are not hogging your resources with elements that are essentially already there.
You can hide a button (or any object) and keep it active by adding a mask to its layer. The button will be invisible but will still catch events.
let layer = CAShapeLayer()
layer.frame = .zero
myButton.layer.mask = layer
Jasons answer above is nearly correct, but setting the button type is not possible. So to programmatically create an empty button, use this code:
UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame=frame;
[self.view addSubview:myButton];
This is what i did but with using a subclass of UIButton which i later found out should not be subclassed as per the net. My subclass was called Points
Points *mypoint=[Points buttonWithType:UIButtonTypeCustom];
then if you have an image you want to add to the button :
[mypoint setImage:imageNamed:#"myimage"] forstate: UIControlStateNormal];
if you dont add this image then the button will be invisible to the user but should respond to touch. Thats how i created a hotspot on my imageView inorder to have it respond to user interaction.
It's the only way I found...
[yourButton setImage:[UIImage imageNamed:#"AnEmptyButtonWithTheSameSize.png"] forState:UIControlStateNormal];
Take care of the image. It must be .png
Custom UIButtons respond to user interactions unless their alpha is set to 0. Put a custom UIButton on top of your imageView and connect to buttonPressed action. I have also set an additional highlighted image for my UIView, now it really behaves like a UIButton. First I have defined a duration for the UIView for staying highlighted:
#define HIGHLIGHTED_DURATION 0.1
Then set the image view highlighted if the button is pressed and start a timer to keep it highlighted for that duration. Do not forget to set the highlighted image for your imageview.
- (IBAction)buttonPressed:(id)sender {
[_yourImageView setHighlighted:YES];
_timer = [NSTimer scheduledTimerWithTimeInterval:HIGHLIGHTED_DURATION
target:self
selector:#selector(removeHighlighted)
userInfo:nil
repeats:NO];
}
And simply undo highlighting when the timer finishes:
-(void) removeHighlighted{
_yourImageView.highlighted = NO;
}
lazyImageView = [[UIImageView alloc] init];
button=[[UIButton alloc]init];
[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethodForVideo:)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"BackTransparent.png"] forState:UIControlStateHighlighted];
[button setTitle:#"" forState:UIControlStateNormal];
lazyImageView.frame=CGRectMake(x, y, w, h);
button.frame=CGRectMake(x, y, w, h);
Set frame of button and Image both have same frame .I use this code and working fine.Also set button background image forState:UIControlStateHighlighted so when you click on that when you see the click effect.
I managed to do it using the following code.
If the iPad is landscape the button will be located in the top right of the screen.
UIButton *adminButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
adminButton.frame = CGRectMake(974.0f, 0.0f, 50.0f, 50.0f);
[adminButton setBackgroundColor:[UIColor clearColor]];
[adminButton setTag:1];
[adminButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:adminButton];
Pressing the 'adminButton' will run the following function:
- (void)buttonPressed:(id)sender {
int buttonId = ((UIButton *)sender).tag;
switch(buttonId) {
case 1:
NSLog (#"Admin button was pressed");
break;
case 2:
//if there was a button with Tag 2 this will be called
break;
default:
NSLog(#"Key pressed: %i", buttonId);
break;
}
}