Event on an Image in iOS - objective-c

I am pretty new to iOS development, so this may seem to be a stupid question.. In my xib file, i have taken a UIImageView that will show an image when the page is loaded. But when a user taps on an image, I want the next view to be loaded.. But I am not able to do that because we cannot have a delegate not a target-action mechanism for a UIImage view.. Many places, I found a workaround.. using a button and then placing an image on it, and then using it.. but I want the first way to work.. Please help..

use UITapGestureRecognizer ,and Don't forget to set imageView.userInteractionEnabled = YES; because by default image not having UserInteraction.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:singleTap];
-(void)handleSingleTap:(id)sender {
// push you view here
}
(OR) If you want
take button with BackGround image.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];

Instead of Using UIImageView, you can use UIButton.
UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[myBtn setBackgroundImage:[UIImage imageNamed:#"your_image_Name"] forState:UIControlStateNormal];
[myBtn setBackgroundImage:[UIImage #"your_image_Name"] forState:UIControlStateHighlighted];
[myBtn addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
also write button action:
-(void)buttonAction:(id)sender {
// Do whatever you want
}
In Case of XIB, Just use UIButton instead of UIImageView and set Background Image for both state Normal & Highlighted as you want. Rest you can manage button's action from XIB.

You can also add a fully transparent UIButton on top of your UIImageView... It should be preferred as each element has a clean semantics.

Related

How do I remove UIBarButtonItem glow programmatically?

Unfortunately there is no showsTouchWhenHighlighted for UIBarButtonItem and I can't edit my button from the toolbar...
The property responsible for this is accessible in the UIButton class:
myButton.showsTouchWhenHighlighted = NO;
You can access this (programmatically) in a UIBarButtonItem by assigning a UIButton to the bar button item's customView property, and configuring the button. You can do this in Interface Builder too: drag a UIButton onto a UIToolbar, and it will automatically embed it in a UIBarButtonItem for you - then look for the "Shows Touch On Highlight" checkbox under the button's settings.
Incidentally, I don't know how you're customising your buttons so feel free to ignore this, but if your button looks and behaves like a standard toolbar item then users will expect the glow effect.
Answer from here
EDIT:
Try this then:
UIImage* buttonImage = [UIImage imageNamed: #"header.navigation.back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width/2, 32);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(backToPriorView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];

Why are UIButton actions not being called when UIButton is inside a UIScrollView

I have created a springboard like panel containing buttons as follows:
-(void)configurePanel{
self.criteriaPanel.scrollEnabled=YES;
self.criteriaPanel=[[UIScrollView alloc] initWithFrame:CGRectMake(
0.0f,
0.0f,
[UIScreen mainScreen].bounds.size.width,
(BUTTON_HEIGHT+(3*BUTTON_Y_OFFSET))
)];
UIView *scrollViewContent=[[UIView alloc]init];
NSArray *criteriaTypeButtonTitles=[NSArray arrayWithObjects:
#"Option1",
#"Option2",
#"Option3",
#"Option4",
#"Option5",
#"Option6",
nil
];
for (int i=0; i<[criteriaTypeButtonTitles count]; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=i;
button.frame = CGRectMake(
(((BUTTON_X_OFFSET*i)+(BUTTON_WIDTH*i))+BUTTON_X_OFFSET)
, BUTTON_Y_OFFSET
, BUTTON_WIDTH
, BUTTON_HEIGHT
);
[button setTitle:[criteriaTypeButtonTitles objectAtIndex:i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
button.titleLabel.font=[UIFont systemFontOfSize:13.0f];
[button setBackgroundColor:[UIColor grayColor]];
[button.layer setCornerRadius:10.0f];
[button.layer setBorderWidth:1.0f];
[button addTarget:self action:#selector(sortByButtonTap:) forControlEvents:UIControlEventTouchDown];
[scrollViewContent addSubview:button];
}
//based upon # of buttons
self.criteriaPanel.contentSize=CGSizeMake(
(([criteriaTypeButtonTitles count]*BUTTON_WIDTH)+([criteriaTypeButtonTitles count]*BUTTON_X_OFFSET)),
(BUTTON_HEIGHT+(2*BUTTON_Y_OFFSET)));
[self.criteriaPanel addSubview:scrollViewContent];
[self.view addSubview:self.criteriaPanel];
}
The panel displays correctly and scrolls, but the tap events (sortByButtonTap:) for the buttons are never called. I suspect this is related to the buttons being contained in a view which is contained in the scrollview. After reading a number of other questions and the docs I still can't figure out what the solution should be.
EDIT:
I experimented w/ adding the buttons to the UIScrollView (self.criteriaPanel) directly and the button taps call sortByButtonTap: so it is something to do w/ the buttons being in the scrollViewContent.
You might need to set userInteractionEnabled on the containing view scrollViewContent for the button to receive touch events:
[scrollViewContent setUserInteractionEnabled:YES];
What if you try:
[button setExclusiveTouch:YES];
Buttons tend to work abnormally when they are inside a view that also handles touch events.
Follow this code for your query,
Add UIButton in Scrollview
scrollview.pagingEnabled = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake();
[button setTitle:#"X" forState:UIControlStateNormal];
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnCloseClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:#selector(btnCloseDraged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(btnCloseDragedEnd:withEvent:) forControlEvents:UIControlEventTouchDragExit];
[scrollview addSubview:button];
scrollview.userInteractionEnabled = YES;
you can put this three methods of button manually.
Let me know if you still have query.
instead of:
[button addTarget:self
action:#selector(sortByButtonTap:)
forControlEvents:UIControlEventTouchDown];
try:
[button addTarget:self
action:#selector(sortByButtonTap:)
forControlEvents:UIControlEventTouchUpInside];
I think your problem is just that you set the wrong UIControlEvent to the UIButton. Change from UIControlEventTouchDown to UIControlEventTouchUpInside. If you connect a UIButton inside the Interface Builder it will always set it to this UIControlEvent:
Another reason:
Otherwise your UIScrollView controls the interactions because it needs to know if you want to scroll it. If you place UIButtons inside your UIScrollView they will be accessible but always with a little delay. This is why you should maybe change the settings for the UIScrollView:
You can disable delays content touches and UIButtons will be accessible immediately

UIImage Buttons (Rounded Rect Button)

I want to be able to change the image of any of the buttons upon the user pressing the button and then when they release, the image that was there before is put back.
So basically when they press their finger on the button, the image changes and when they release, the original image I specified in IB is put back.
How can I do this? Here is the code I have so far.
- (IBAction)btnOne:(id)sender {
result.text = [self appendresult:#"1"];
//Changes the button image upon touch
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1.png"] forState:UIControlStateNormal];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateSelected];
btnOne.showsTouchWhenHighlighted = YES;
}
Put your code...
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1.png"] forState:UIControlStateNormal];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateSelected];
btnOne.showsTouchWhenHighlighted = YES;
in viewDidLoad instead of in the IBAction, then just make sure that your button in Interface Builder is properly linked to the IBOutlet you created for it.

UIButton IB vs programmatically

When I make a custom UIButton with a background in interface builder, it gives me this default functionality of when i touch up inside, it sort of blacks out the image.
If I create the UIButton programmatically, like this:
buttonPic = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 63)];
[[buttonPic imageView] setContentMode:UIViewContentModeScaleToFill];
[buttonPic setImage:[video pic] forState:UIControlStateNormal];
it doesn't behave like the one I created in IB at all. Nothing changes when I touch up inside.
Is there a settings in UIButton that I am missing?
Thanks
This is how to create a UIButton programatically.
//Specify the type of the button first. No need to use alloc-init for UIButton like other view objects.
UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// Give UIButtonTypeRoundedRect to get default Interface builder style button.
myBtn.frame = CGRectMake(0, 0, 100, 40);
[self.view addSubview:myBtn];
// Since the button type is custom, give an image to make it visible.
[myBtn setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:normal];
[myBtn addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
// These are some optional methods you may want to use.
myBtn.titleLabel.font = [UIFont fontWithName:#"Arial" size:15];
[myBtn setTitle:#"my button" forState:UIControlStateNormal];
[myBtn setTitleColor:[UIColor blackColor] forState:normal];
[myBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
I think you must enable one of this two options:
buttonPic.showsTouchWhenHighlighted = YES;
buttonPic.reversesTitleShadowWhenHighlighted = YES;

how to make button round with image background in IPhone?

I have a round button, but i will make it have a backgroud with image.
but when i do it, by setting the (what) property to an image, the button becomes rectangle because the image is a rectangle. How do I keep the button round?
Simply by doing this
#import <QuartzCore/QuartzCore.h>
myButton.layer.cornerRadius = 8;
Tested Code:
.h
-(void)vijayWithApple;
.m
-(void)vijayWithApple{
NSLog(#"vijayWithApple Called");
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Micky.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(vijayWithApple) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value
button.clipsToBounds = YES;
button.layer.cornerRadius = 20;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
[self.view addSubview:button];
Restut
Best solution I have found especially for UITableCell. I put the code in awakeFromNib and worked right off the bat.