is there any way to hide an UIButton until the UIImageView is pressed??
When the picture is pressed I need to show the back Button, like it works at the Photo App on the iPhone???
Here is the code of my UIButton:
- (void)viewDidLoad {
[super viewDidLoad];
[self ladeImage];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 10, 40, 40);
[btn addTarget:self action:#selector(goToViewA) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"<<" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
First step : btn.hidden = YES
Then you have to subclass the UIImageView to react to its touchesEnded: event and change the hidden property of your button there. For that, the proper way is to create a protocol (with a viewTouched method). Implement that protocol in the viewController containing your button and you ImageView. Add a delegate propery to the subclassed ImageView (i.e. id<MyCustomProtocol> _delagate;) and assign the view controller to this propery.
btn.hidden = YES;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image name"]];
imageView.userInteractionEnabled = YES; // here to enable touch event
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGestureRecongizer:)]; // handleTapGestureRecongizer is method will call when tap even fire
[imageView addGestureRecognizer:tap]; // Add Tap gesture recognizer to image view
[tap release], tap = nil;
[self.view addSubview:imageView];
[imageView release], imageView = nil;
Method handlerTapGestureRecognizer:
- (void)handleTapGestureRecongizer:(UITapGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
btn.hidden = NO;
}
}
have fun!
Related
I have an application in which i need to add activity indicators above the buttons whenever it has been clicked.I made all the custom buttons like this and add it to navigation bar.`
UIButton *btnNext1 = [UIButton buttonWithType:UIButtonTypeCustom];
btnNext1.frame = CGRectMake(100, 100,38, 38);
UIImage *image = [UIImage imageNamed:#"default.png"];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = image;
imageView.frame = CGRectMake(10,5,38,38);
[imageView.layer setCornerRadius:5.0];// position it to the middle
[btnNext1 setBackgroundImage:imageView.image forState:UIControlStateNormal];
[imageView release];
[btnNext1 addTarget:self action:#selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnNext =[[UIBarButtonItem alloc] initWithCustomView:btnNext1];
self.navigationItem.leftBarButtonItem = btnNext;
[btnNext release];
then in the sender methode i tried
UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
// Position the spinner
[myIndicator setCenter:CGPointMake(button.frame.size.width / 2,button.frame.size.height /2)];
// Add to button
[button addSubview:myIndicator];
// Start the animation
[myIndicator startAnimating];
But it is not showing up. Can anybody point me where I am going wrong?
Try this in button action
UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[myIndicator setCenter:button.center];
[button addSubview:myIndicator];
[button bringSubviewToFront:myIndicator];
[myIndicator startAnimating];
I tried with your code it working fine may be the image and indicator both are same color or you are calling different method ,otherwise it's working fine.
once check below images.
before and after button actions
Change your sender method to
UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
// Position the spinner
[myIndicator setCenter:CGPointMake(button.frame.size.width / 2,button.frame.size.height /2)];
// Add to button
[self addSubview:myIndicator];
// Start the animation
[myIndicator startAnimating];
You need to add indicator to uiview rather than button.
I'm using XCode 4.5.1 .
I added <UIGestureRecognizerDelegate> in .h file and in the start of .m file
#interface FirstViewController ()
#property (nonatomic, strong) UIImageView *img1;
#end
Then in viewDidLoad, creating UIImageView programmatically like
self.img1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, -30, 44, 44)];
[self.img1 setImage:[UIImage imageNamed:#"image1.png"]];
[self.img1 setAlpha:0.8];
[self.img1 setHidden:NO];
[self.img1 setUserInteractionEnabled:YES];
[self.img1 setTag:901];
And then
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(guessTapObject:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
tap.delegate = self;
[self.img1 addGestureRecognizer:tap];
[self.view addSubview:self.img1];
[self.view bringSubviewToFront:self.img1];
and at the end
- (void)guessTapObject:(UITapGestureRecognizer *) gesture
{
// guessing the image
UIImageView *tapImageView = (UIImageView*) gesture.view;
NSLog(#"Gesture Tag: %d", tapImageView.tag);
}
I have 4 different images and creating them programmatically as described above. And applying the animation to move them from top to bottom.
I want that when user tap on the image, it should animate and disappear(i know that code). But the code is not triggering the tap event. I put the breakpoint on the start of the guessTapObject function, but don't go there ever.
At UITapGestureRecognizer declaration, debugging shows that guessTapObject is attached to the recognizer to perform the tap action.
Please help why tap event is not triggering its selector method???
Thanks for your help in advance.
Edited:
i also tried with for loop to have gesture recognizer separately for every imageview
for (UIView * view in self.view.subviews) {
if (view.tag > 900){
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(guessTapObject:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
// tap.delegate = self;
[view addGestureRecognizer:tap];
NSLog(#"tapView: %#", tap.view);
NSLog(#"tap: %i", tap.enabled);
}
}
I was stuck in similar issue.. Make sure imageView is set true for userInteraction..
imageView.userInteractionEnabled = YES;
It worked for me!
Check this
Add following code to your View Controller
In viewController.h file
#property (nonatomic, strong) UIImageView *img1;
#property (nonatomic, strong) UIImageView *img2;
#property (nonatomic, strong) UIImageView *img3;
In viewController.m file
Inside viewDidLoad Method
// Init Image 1
self.img1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 30, 44, 44)];
[self.img1 setImage:[UIImage imageNamed:#"image1_name.jpg"]];
[self.img1 setAlpha:0.8];
[self.img1 setHidden:NO];
[self.img1 setUserInteractionEnabled:YES];
[self.img1 setTag:901];
// Init Image 2
self.img2 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 30, 44, 44)];
[self.img2 setImage:[UIImage imageNamed:#"image2_name.jpg"]];
[self.img2 setAlpha:0.8];
[self.img2 setHidden:NO];
[self.img2 setUserInteractionEnabled:YES];
[self.img2 setTag:902];
// Init Image 3
self.img3 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 130, 44, 44)];
[self.img3 setImage:[UIImage imageNamed:#"image3_name.jpg"]];
[self.img3 setAlpha:0.8];
[self.img3 setHidden:NO];
[self.img3 setUserInteractionEnabled:YES];
[self.img3 setTag:903];
// Add Images to view
[self.view addSubview:self.img1];
[self.view bringSubviewToFront:self.img1];
[self.view addSubview:self.img2];
[self.view bringSubviewToFront:self.img2];
[self.view addSubview:self.img3];
[self.view bringSubviewToFront:self.img3];
// Set Tap Gesture to each image of view
for (UIView * view in self.view.subviews) {
if (view.tag > 900){
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(guessTapObject:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[view addGestureRecognizer:tap];
NSLog(#"tapView: %#", tap.view);
NSLog(#"tap: %i", tap.enabled);
}
}
Make sure images should be added before for loop(adding TapGesture using array of subview)
i solved by applying gesture on view, instead of each image view separately.
[self.view addGestureRecognizer:tap];
and then in handler/selector guessTapObject function, i used this
CGPoint tapLocation = [gesture locationInView:self.view]; // gives the tap coordinates
for (UIView * view in self.view.subviews) { // gives view by iterating on each subview
CGRect dropRect = [[[view layer] presentationLayer] frame]; // specific way of getting the frame with respect to its layer
if(CGRectContainsPoint(dropRect, tapLocation)){ // checks for tap in the boundaries of view frame
if (view.tag > 900) // my specific view
// done what i want to do with that specific one
// as i removed it from my superview
}
}
}
i have 2 uibutton and 1 label and longpressgesture is bounded to these control. when longpress is taken place on any control then how to get the object on which longpress is taken place below is the code that i have written.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[btn addTarget:self action:#selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
//[self.view addSubview:btn];
btn.userInteractionEnabled = YES;
// add it
[self.view addSubview:btn];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[btn addGestureRecognizer:longPress];
below is function that is called on longpress
-(void)handleLongPress:(id)sender{
}
if i printing description of sender then i get
<UILongPressGestureRecognizer: 0x6aa4480; state = Began; view = <UIRoundedRectButton 0x6aa9570>; target= <(action=handleLongPress:, target=<ViewController 0x6a8cc60>)>>
from it how can i get the refrence of object on whcih longpress event takes place
i mean how do i know whether i preessed UiLabel or Uibutton?
Just check the UIGestureRecognizer's (the parent class) view property:
#property(nonatomic, readonly) UIView *view
The view the gesture recognizer is attached to. (read-only)
#property(nonatomic, readonly) UIView *view
Discussion
You attach (or add) a gesture recognizer to a UIView object using the addGestureRecognizer: method.
-(void)handleLongPress:(UILongPressGestureRecognizer *)sender{
if ([sender.view isKindOfClass:[UIButton class]]) {
UIButton *myButton = (UIButton *)sender.view; // here is your sender object or Tapped button
if (myButton.tag == 1) {
//sender is first Button. Because we assigned 1 as Button1 Tag when created.
}
else if (myButton.tag == 2){
//sender is second Button. Because we assigned 2 as Button2 Tag when created.
}
}
if ([sender.view isKindOfClass:[UILabel class]]) {
UILabel *myLabel = (UILabel *)sender.view; // here is your sender object or Tapped label.
}
}
I have implemented UITapGestureRecognizer on UIImageView, It is working on first tap. On First Tap, I am hiding that image and starting animation. Once the animations are completed, i am showing the image again. But After setting setHidden:FALSE, I am not getting the Tap event of that UIImageView.
Following is the code I am using :
- (void)viewDidLoad{
[super viewDidLoad];
defaultDogView= [[UIImageView alloc] initWithFrame:CGRectMake(3, 270, 110, 210)];
[defaultDogView setImage:[UIImage imageNamed:#"dog1.png"]];
defaultDogView.userInteractionEnabled = YES;
[self addGestureRecognizersToPiece:defaultDogView];
[self.view addSubview:defaultDogView];
}
- (void)addGestureRecognizersToPiece:(UIImageView *)piece
{
NSLog(#"in Gesture");
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapPiece:)];
[tapGesture setDelegate:self];
[piece addGestureRecognizer:tapGesture];
[tapGesture release];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressPiece:)];
[piece addGestureRecognizer:longPressGesture];
[longPressGesture release];
NSLog(#"%#", [piece gestureRecognizers]);
}
- (void)singleTapPiece:(UITapGestureRecognizer *)gestureRecognizer
{
NSLog(#"Image Tapped");
/** Hide the default Image and start the animation ***/
[defaultDogView setHidden:TRUE];
/***Animating the Dog***/
[dogArray addObject:[SpriteHelpers setupAnimatedDog:self.view numFrames:69 withFilePrefix:#"dog" withDuration:(12) ofType:#"png" withValue:0]];
dogView = [dogArray objectAtIndex:0];
//[self addGestureRecognizersToPiece:dogView];
[self performSelector:#selector(callBubbleUpdater) withObject:nil afterDelay:5.5];
}
-(void)showDogFrame{
NSLog(#"%#",[defaultDogView gestureRecognizers]);
[defaultDogView setHidden:FALSE];
defaultDogView.userInteractionEnabled = YES;
}
When view is hidden or its alpha component is zero that view won't receive any UIGestureRecognizers.
I can suggest to use next approach if you need to hide some view (let's name it touchableView) but want it to respond to gestures:
Create backgroundView with the same frame as touchableView:
UIView *backgroundView = [[UIView alloc] initWithFrame:touchableView.frame];
Set background color of backgroundView to clearColor:
backgroundView.backgroundColor = [UIColor clearColor];
Reset position of touchableView:
CGRect frame = touchableView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
Disable user interaction of touchableView:
touchableView.userInteractionEnabled = NO;
Add touchableView as subview to backgroundView:
[backgroundView addSubview:touchableView];
Add appropriate gesture recognizers to backgroundView.
Add backgroundView to view that you want.
Now you can hide touchableView but you will still receive gesture recognizers.
I don't test this but I think it should work.
sure
when UIImageView is hidden. it does not receive any touch events
set alpha zero for uiimageview
I'm dynamically adding image buttons to some scrollview. They all point at one longPressHandler. Now, how do I get which button was pressed? The [sender tag] gives me the tag of longGestureRecognizer that I added to button and I can't manually set that tag.
for (...) {
UIButton *button = [[UIButton alloc] init];
button.tag = w + h * 3;
[button addTarget:self action:#selector(imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *gest = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(imageButtonLongPress:)];
gest.minimumPressDuration = 1;
gest.delegate = self;
[button addGestureRecognizer:gest];
[gest release];
[scrollView addSubview:button];
[button release];
}
- (void) imageButtonLongPress:(id)sender {
// how to get button tag here?
}
There is a view property in the UIGestureRecognizer which returns the view that recognizer is attached to. I think that is your best bet.
- (void) imageButtonLongPress:(id)sender {
UIGestureRecognizer *recognizer = (UIGestureRecognizer*) sender;
int tag = recognizer.view.tag;
}
In your action you have to type cast your sender in gesture and then type cast its view to a button then get button's tag as -
UILongPressGestureRecognizer *gest = (UILongPressGestureRecognizer *)sender;
UIButton *button = (UIButton*)[gest view];
NSLog(#"%d",[button tag]);