I am currently trying to select a UIImageView that I have created in the storyboard in my view controller.
Is there a method equivalent to android's findViewbyId in objective c?
Why not just make an IBOutlet for the imageview?
Then you can just access it by something like
self.imageView
Where imageView is what you named your outlet.
Place a UIButton over your UIImageView.
After creating an IBOutlet as Samantha suggested you will need to add a gesture recogniser to your UIImageView.
I would suggest adding the gesture recognizer in viewDidLoad:
-(void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *singlePress = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageViewPressed)];
[singleTap setNumberOfTapsRequired:1];
[_imageView addGestureRecognizer:singleTap];
}
- (void)imageViewPressed {
//Perform imageView action here
}
Use the viewWithTag
UIImageView *yourImage = (UIImageView*)[self.view viewWithTag:20];
Related
I want a pretty simple thing - in my top controller (which is navigation controller) to set up a tap gesture recognizer which will catch all the taps everywhere on the view. Currently when I tap on a button the system is not even thinking to bother my recognizer (except the gestureRecognizer:shouldReceiveTouch: delegate method, where I return YES). Instead, it just executes a button click. So I want to install "the strongest" recognizer on a view hierarchy no matter what.
You might try putting an empty UIView on top of all other views and add the UITapGestureRecognizer to it. We do something similar with help overlays. The biggest issue is figuring out how and when to ignore the touches so the underlying buttons get them when needed.
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *b = [UIButton buttonWithType:UIButtonTypeInfoDark];
b.frame = CGRectMake(50,50, b.bounds.size.width, b.bounds.size.height );
[self.view addSubview:b];
UIView *invisibleView = [[UIView alloc] initWithFrame:self.view.bounds];
invisibleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[invisibleView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapHit:)]];
[self.view addSubview:invisibleView];
}
-(void)tapHit:(UITapGestureRecognizer*)tap {
NSLog( #"tapHit" );
}
#end
When tapping a specific button in my app I want an image to show, I did this using an UIImageView. Then I want to hide that image by tapping it, but I don't understand how to do this?
I tried the following code, but it doesn't work.
#implementation ViewController
-(IBAction)pic {
UIImage *img = [UIImage imageNamed:#"test.png"];
[ImageView setImage:img];
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleTap:)];
tapRecognizer.delegate = self;
[imageView addGestureRecognizer:tapRecognizer];
}
- (void)handleTap:(UITapGestureRecognizer *)tapGestureRecognizer {
//handle tap
}
Its pretty simple.
Use an UIImageView instead and check that userInteractionEnabled is YES on the UIImageView. Then you can then add a gesture recognizer.
Your .h file should have atleast something like below:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UIGestureRecognizerDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *touchImageView;
#end
Dont forget to connect UIImageView from your storyboard to property declared above.
in your .m file:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.touchImageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleTap:)];
tapRecognizer.delegate = self;
[self.touchImageView addGestureRecognizer:tapRecognizer];
}
- (void)handleTap:(UITapGestureRecognizer *)tapGestureRecognizer {
//handle tap
self.touchImageView.alpha = 0.0f;
}
You could put an image on a button instead. I think using a UIImageView is the right decision though. You need to hook up a gesture to it programmatically. You can do this using something similar to below:
let singleFingerTap = UITapGestureRecognizer(target: self, action: "viewTapped:")
imageView.addGestureRecognizer(singleFingerTap)
You can add a tap gesture recognizer to the UIImageView that contains the image.
var tapGesture = UITapGestureRecognizer(target: <#AnyObject#>, action: <#Selector#>)
In the method you assign as the action, just set myImageView.alpha = 0. This should essentially "hide" your image view. You could also set the height of the image view to 0 if you wanted to hide it in that sense.
An alternative could be to import an open-sourced project, such as the AKImageViewer, to get posts to appear full screen (giving the user a better full view) and allowing them to swipe or cancel to get away from the image (similar to viewing images in the Twitter app.
I have a view controller that have a label, and I want to perform panning to the right on that label.
So what I did so far is:
*add a pan gesture to the label in the nib file
*created a didPan method:
- (IBAction)didPan:(UIScreenEdgePanGestureRecognizer *)sender;
and the implementation:
- (IBAction)didPan:(UIScreenEdgePanGestureRecognizer *)sender {
CGPoint newTranslation = [sender translationInView:self.homeLabel];
self.homeLabel.transform = CGAffineTransformMakeTranslation(newTranslation.x, 0);
}
and changed the screen edge pan gesture recogniser to right.
I thought it should pan now but its now, what am I doing wrong?
tnx
So I had some time to try it out myself.
Here's what the Interface Builder looks like with -didPan IBAction connected.
However that didn't work for me. I did some searching and found this also on StackOverflow.
Possible bug with UIScreenEdgePanGestureRecognizer in Interface Builder
So, I would suggest you try implementing it the old fashion way in code.
Try this:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScreenEdgePanGestureRecognizer *swipeFromLeft = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:#selector(didPan:)];
swipeFromLeft.edges = UIRectEdgeLeft;
[self.view addGestureRecognizer:swipeFromLeft];
UIScreenEdgePanGestureRecognizer *swipeFromRight = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:#selector(didPan:)];
swipeFromRight.edges = UIRectEdgeRight;
[self.view addGestureRecognizer:swipeFromRight];
}
I want to add a gesture recognizer to a UIImageView in a UITableViewCell.
I've subclassed this cell, and implemented the gesture code, but it does not seem to be working .
This is the code for my subclassed .m file:
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
[self.settingsView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didRecognizeTapOnSettings)];
[tap setNumberOfTouchesRequired:1];
[tap setNumberOfTapsRequired:1];
[self.settingsView addGestureRecognizer:tap];
}
return self;
}
I'm using initwithcoder since I'm using storyboards, and that seems to be working far better than initwithframe. For some odd reason, didRecognizeTapOnSettings does not get called. I've set the delegate of the UIGestureRecognizer in my .h file as well.
IBOutlets are not yet set inside an init method, so self.settingsView is going to be nil. Move the code you have in the if(self) block to awakeFromNib, and then it should work.
try using
[tap setMinimumPressDuration:0.1f];
then you said you are setting your delegate for the gesture then if you are planning to use your delegate methods for the tapGesture you should set the delegate to self.
tap.delegate = self;
What's the hierarchy in your views? Your UITableViewCell might be catching the tap event instead of your UIImageView. As an experiment try to disable the userInteractions in the UITableViewCell contentView I'm assuming that you added the UIImageView in the contentView of your cell.
Let me know if that worked
In Instagram in the header view we could see there is the profile image and the username right , so I am thinking how is this possible because the username is a UILabel how when it's clicked on it sent you the user homepage . Any idea ?
You can add a UITapGestureRecognizer to the label to make it clickable.
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tappedOnLabel:)];
[label setUserInteractionEnabled:YES];
[label addGestureRecognizer:gesture];
-(void)tappedOnLabel:(UIGestureRecognizer*)gestureRecognizer
{
// Perform your action
}
Yes it can, check "User Interaction Enabled" in the Attribute Inspector, and add an Action method with it. Make sure you connect the action method to the label
EDIT-
I am showing this in code as i cant post screenshots.
Create a Label #property(nonatomic, weak) IBOutlet UILabel *myLabel;
Connect the Label in Storyboard.
Create a method, that you want to do when user Taps the label.
-(void)showHello{
NSLog(#"Hello World");
}
Now declare a
UITapGestureRecognizer *tap;
I used tapGesture as I want the action to run when I TAP the label.Declare this Gesture as an Instance Variable
in viewDidLoad alloc and init the gesture and add it to the Label
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myLabel.userInteractionEnabled = YES;
tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(showHello)];
[self.myLabel addGestureRecognizer:tap];
}
Yup that should do it