Adding gesture to label [duplicate] - ios

i have working in image and video overlay in that i put one image view, i assigned the tapGestureRecognizer to that image view its not working,For video i put one MPMoviePlayerController and i set the tapGestureRecognizer to it this one working fine but the image view only not working.Below is my code
- (void)viewDidLoad {
[super viewDidLoad];
videoimg=[[UIImageView alloc]init];//OvelayImage
videoimg.image=[UIImage imageNamed:#"fb.png"];
videoimg.userInteractionEnabled=YES;
videoimg.multipleTouchEnabled=YES;
[self.imgView bringSubviewToFront:videoimg];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imagePan:)];
tap.numberOfTapsRequired=1;
tap.numberOfTouchesRequired=1;
[videoimg addGestureRecognizer:tap];
[self.imgView addSubview:videoimg];//add the overlay image to Main imageView
[self.videoPlayerView.view addSubview:videoimg];//MPMoviePlayerController-add the overlay image to VideoView
}
//Gesture
-(void)imagePan:(UITapGestureRecognizer*)ges{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Image" message:#"FB Image Clicked..."
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}

videoimg=[[UIImageView alloc]init];
videoimg.image=[UIImage imageNamed:#"fb.png"];
videoimg.userInteractionEnabled=YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imagePan:)];
[videoimg setGestureRecognizers:[NSArray arrayWithObject:tap]];
tap.numberOfTapsRequired=1;
[self.videoPlayerView.view addSubview:videoimg];
use it may help you....

Related

Best way to take a UIGestureRecognizer event and get the root container view?

I have a UIViewController where I'm adding bunch of UIViews as subviews. Then I attach UIGestureRecognizer to each, so I can be notified when there's a tap. Here's the code inside the UIViewController:
- (void)attachSubview{
UIImageView *childView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"child"]] ;
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[childView addGestureRecognizer:singleFingerTap];
[self.view addSubView childView];
}
- (void)tapped:(UITapGestureRecognizer *)recognizer{
NSLog(#"Tapped view : %#", recognizer.view);
NSLog(#"The root view is : %#", self.view);
}
Now, here's the problem: I actually want to extract these two methods out into a separate class. In this case I can't use the self.view inside the tapped: method, since self wouldn't be the viewcontroller class anymore.
So I would like a simple and efficient way to just take the recognizer object and somehow get the root view to which the image view belongs. What is the best and future proof way to do this?
Could you try this please?
- (void)viewDidLoad {
[super viewDidLoad];
//example of its use. You can replace with [className attachSubview:self.view] for example
self.view = [self attachSubview:self.view];
}
- (UIView *)attachSubview:(UIView *)passedInView{
UIImageView *childView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"child"]] ;
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[childView addGestureRecognizer:singleFingerTap];
[passedInView addSubview:passedInView];
return passedInView;
}
- (void)tapped:(UITapGestureRecognizer *)recognizer{
NSLog(#"Tapped view : %#", recognizer.view);
NSLog(#"The root view is : %#", recognizer.view.superview);
}

Objective-C truncate tail and showing details when pressing the three dots [duplicate]

This question already has answers here:
uilabel tail truncation
(4 answers)
Closed 6 years ago.
I have a long text which is truncated to three dots, i want the long text to show a small pop-up that contains the full text when the user clicks on the three dots.
this is the label of the text
self.lblTitle.text = self.project.title;
You can do this on using tapgesture on the label and on taping the label a alertview will show and on that alertview you can show the full text what you want to show.
this is how we use tapgesture just example :-
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:tapGestureRecognizer];
myLabel.userInteractionEnabled = YES;
One of the possible solutions is add a tapGesture. I've just made this code, you can try use it:
- (void)tapGestureToLabel {
self.lblTitle.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(callAlert)];
tapGesture.numberOfTapsRequired = 1;
[tapGesture setDelegate:self];
[self.lblTitle addGestureRecognizer:tapGesture];
}
- (void)callAlert {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:self.lblTitle.text delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alert show];
}
You can call [self tapGestureToLabel]; right after:
self.lblTitle.text = self.project.title;
ps: Don't forget to add UIGestureRecognizerDelegate to your #interface

Tap gesture is not working in imageView but it working another view

i have working in image and video overlay in that i put one image view, i assigned the tapGestureRecognizer to that image view its not working,For video i put one MPMoviePlayerController and i set the tapGestureRecognizer to it this one working fine but the image view only not working.Below is my code
- (void)viewDidLoad {
[super viewDidLoad];
videoimg=[[UIImageView alloc]init];//OvelayImage
videoimg.image=[UIImage imageNamed:#"fb.png"];
videoimg.userInteractionEnabled=YES;
videoimg.multipleTouchEnabled=YES;
[self.imgView bringSubviewToFront:videoimg];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imagePan:)];
tap.numberOfTapsRequired=1;
tap.numberOfTouchesRequired=1;
[videoimg addGestureRecognizer:tap];
[self.imgView addSubview:videoimg];//add the overlay image to Main imageView
[self.videoPlayerView.view addSubview:videoimg];//MPMoviePlayerController-add the overlay image to VideoView
}
//Gesture
-(void)imagePan:(UITapGestureRecognizer*)ges{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Image" message:#"FB Image Clicked..."
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
videoimg=[[UIImageView alloc]init];
videoimg.image=[UIImage imageNamed:#"fb.png"];
videoimg.userInteractionEnabled=YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imagePan:)];
[videoimg setGestureRecognizers:[NSArray arrayWithObject:tap]];
tap.numberOfTapsRequired=1;
[self.videoPlayerView.view addSubview:videoimg];
use it may help you....

Tapping the Background image on iPhone

I want to do something when the background was tapped on iPhone.
How to enable the background tapping?
I put a sample background on my app using this code.
- void viewDidLoad{
[super viewDidLoad];
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"backgroundimage.jpeg"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
How to do background tapping?
For example I want to click the background and a message will pop up "Background was tapped!".
Do i need IBAction for that?
Need help.
What you could do is use an IBAction, which would be a lot simpler code.
In your header file of your view controller, do this:
- (IBAction)backgroundTap:(id)sender;
In your implementation file of your view controller, do this:
- (IBAction)backgroundTap:(id)sender
{
}
In the storyboard, assuming that you have linked up view controller GUI with your view controller class, click on the background of the view controller GUI and then show the Identity Inspector in the Utilities view, which should show up on the right. Then, under custom class, which should currently be blank, type in UIControl. Now go to the connections inspector and link at Touch Down to the background, selecting backgroundTap. Now, anything inside the backgroundTap method will be what will happen when you select the background.
UIButton is the simplest way.
- (void)backgroundButtonClicked:(id)sender
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:#"Background was tapped!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
/*
* Your other code here
*/
UIButton *backgroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
backgroundButton.backgroundColor = [UIColor clearColor];
backgroundButton.frame = self.view.bounds;
[backgroundButton addTarget:self action:#selector(backgroundButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backgroundButton];
[self.view sendSubviewToBack:backgroundButton];
}
BTW, there is no need to draw a background image because [UIImage imageNamed:#"imagename"] returns an image. If you want to present it, try putting the code in your -viewDidLoad:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"backgroundimage.jpeg"]];
imageView.frame = self.view.bounds;
[self.view insertSubview:imageView belowSubview:backgroundButton];
[imageView release];
Edit:
Thanks to #AlexMDC for reminding me of UITapGestureRecognizer. Here is the UITapGestureRecognizer version:
- (void)tapped:(UITapGestureRecognizer *)g
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:#"Background was tapped!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
/*
* Your other code here
*/
UITapGestureRecognizer*tap = [[UITapGestureRecognizer alloc] init];
[tap addTarget:self action:#selector(tapped:)];
[self.view addGestureRecognizer:tap];
[tap release];
}
Both versions meet the requirements. Admittedly, UITapGestureRecognizer is more powerful and more flexible. However, I'd prefer UIButton to do the trick this time. It's more lightweight than gesture recognizer. I needn't care about the state of the gesture recognizer, whether touch events have been blocked by it or how to implement the UIGestureRecognizerDelegate.
It's more likely the case that we want to add some other UIView or subclasses of UIView on the controller's view. At this point, the UITapGestureRecognizer version need to exclude all the non-background areas in – gestureRecognizerShouldBegin: delegate method.
If detecting double tap is the new requirement, it is still not too late to refactor the UIButton to the UITapGestureRecognizer.

iOS How to dismiss UIAlertView with one tap anywhere?

I want to dismiss UIAlertView anywhere outside it with one tap. I want to show a UIAlertView without any button.
I have standard UIAlertView codes here, but I need input how to dismiss it, if it is possible.
With UITouch? With UITapGestureRecognizer?
Thank you.
EDIT:
in viewDidLoad
alertview initialization here with name "alert"
if (alert)
{
emptyview = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];
emptyview.backgroundColor = [UIColor clearColor];
[self.view addSubview:emptyview];
[emptyview addSubview:alert];
NSLog (#"emptyview is there!");
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[emptyview addGestureRecognizer:singleTap];
[singleTap release];
}
But this emptyview doesnt not respond at all and it does not respond to handleSingleTap selector, which I rewrote a bit:
-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
[alert dismissWithClickedButtonIndex:0 animated:YES];
[emptyview removeFromSuperview];
}
I need this emptyview being upon alert when alert is shown then I can dismiss alert with one tap.
I tried:
if (alert)
{
emptyview = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];
emptyview.backgroundColor = [UIColor clearColor];
[self.view addSubview:emptyview];
[emptyview addSubview:alert];
NSLog (#"emptyview is there!");
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[alert addGestureRecognizer:singleTap];
[singleTap release];
}
Of course, alert did respond to handleSingleTap function. What did I wrong with emptyview?
SECOND EDIT:
What I want to achieve in this case is to show a small view with explanation after selecting a word, similar function in Kindle app, if you have one.
Maybe I should create a UIView instead of UIAlertView? But the small view in Kindle app is so nice with shadow below it, how is it possible?
It sounds like you are essentially trying to recreate a "Toast" on iOS. Good news, someone has already done that. See this project.
Edit: Don't want to use iToast. I like your style, less code it is. Here is what I come up with. It would seem obvious as others have said that the only way to overcome the modal nature of the UIAlertView is to add a superview to handle touch events. But you don't have to do that manually every time, consider subclassing UIAlertView. Try something like this:
Edit: #wagashi, Thanks for accepting my answer, and thanks for the heads up about setFrame: being a good place to adjust the size. Your code does make a very toast-like little alert, however when I tried it I found that if the message was to long the view seemed to fall apart. So I have modified setFrame: to simply reduce the size of the alert by about the size of one button, and to remain centered on the screen. So that the class accurately answers the question title "iOS How to dismiss UIAlertView with one tap anywhere?"
NoButtonAlertView.h
#import <UIKit/UIKit.h>
#interface _NoButtonAlertViewCover : UIView
#property (nonatomic,assign) UIAlertView *delegate;
#end
#interface NoButtonAlertView : UIAlertView
-(id)initWithTitle:(NSString *)title message:(NSString *)message;
#end
NoButtonAlertView.m
#import "NoButtonAlertView.h"
#implementation _NoButtonAlertViewCover
#synthesize delegate = _delegate;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self removeFromSuperview];
[_delegate dismissWithClickedButtonIndex:0 animated:YES];
}
#end
#implementation NoButtonAlertView
-(void)show{
[super show];
_NoButtonAlertViewCover *cover = [[_NoButtonAlertViewCover alloc] initWithFrame:[UIScreen mainScreen].bounds];
cover.userInteractionEnabled = YES;
cover.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.01];
cover.delegate = self;
[self.superview addSubview:cover];
}
-(id)initWithTitle:(NSString *)title message:(NSString *)message{
if ((self = [super initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil])){
}
return self;
}
- (void)setFrame:(CGRect)rect {
// Called multiple times, 4 of those times count, so to reduce height by 40
rect.size.height -= 10;
self.center = self.superview.center;
[super setFrame:rect];
}
#end
With this simple UIAlertView subclass and its UIView subclass for a cover, you can use it as simply as you would a standard UIAlertView. Like so:
NoButtonAlertView *alert = [[NoButtonAlertView alloc] initWithTitle:#"Hello" message:#"I am the very model of a modern major general; I'm information, vegitable, animal, and mineral."];
[alert show];
Will yield:
After showing UIAlertView add to your view controller (or even window) new empty UIView with full screen size. Attach to this wiew UITapGestureRecognizer
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[view addGestureRecognizer:singleTap];
[singleTap release];
Now in handleSingleTap method you can dismiss UIAlertView and remove this view from window
-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
[myAlert dismissWithClickedButtonIndex:0 animated:YES];
[view removeFromSuperView];
}

Resources