Hide keyboard in ipad - iphone

How can I hide my keyboard when I click on my UIImageView?
I am trying to use this soution but it does not work with UIImageView:
- (IBAction)backgroundTap:(id)sender
{
logLang.hidden=YES;
pasComp.hidden=YES;
[login resignFirstResponder];
[password resignFirstResponder];
}

Make sure your image view user interaction enabled in xib? if you are adding your imageView pro grammatically then
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == yourImageView) {
logLang.hidden=YES;
pasComp.hidden=YES;
[login resignFirstResponder];
[password resignFirstResponder];
}
}
use the UIResponder methods touchesBegan, touchesMoved, touchesEnded etc to detect the touch on the image view

Related

Touch to hide tool bar

I want to hide and show tool bar when touch in The area CGRectMake(130, 0, 60, 480)
without using UIGestureRecognizer because its effect my other views
if touch once in the area , tool bar should hide and if tool bar is hidden , should show the tool bar
i have tried this
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (mainToolbar.hidden == YES) {
mainToolbar.hidden=NO;
}
else if(mainToolbar.hidden == NO){
[mainToolbar setHidden:YES];
}
}
but its hide tool bar when touch in to tool bar only
thanks......
Write your code in the view where you added tool bar. You will get location of touch using this code:
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: touch.view];
And then check if touch is inside your rect using CGRectContainsPoint: function.
Another approach is simply put an button on the required area. Hope this helps
The method gives you an NSSet of all touches on the screen. You can use this to customize your behavior for touches. For example:
//This will change the state of whether mainToolbar is hidden or not. In the case of multiple touches, it will change the property if any touch is in the CGRect area.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGRect testRect = CGRectMake(130, 0, 60, 480);
for (UITouch *touch in touches) {
if (CGRectContainsPoint(testRect, [touch locationInView:self.view])) {
mainToolbar.hidden = !mainToolbar.hidden;
}
}
}
Or if you only want the action to happen if one touch is made and ignore it if the user is touching with multiple fingers, you would check for that:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGRect testRect = CGRectMake(130, 0, 60, 480);
if ([touches count] == 1) {
//If there is only one touch, we check for that. Otherwise, we ignore it.
UITouch *touch = [touches anyObject];
if (CGRectContainsPoint(testRect, [touch locationInView:self.view])) {
mainToolbar.hidden = !mainToolbar.hidden;
}
}
}
You can do some really cool things with just the four functions touchesBegan: touchesMoved: touchesEnded: and touchesCanceled:.

removing a subview from another subview when tapped outside

i am trying to build my first iphone app, please do help me out of this issue!! i have my scrollview as a subview of my controller's view and some controls like label, button, textfield and tableview has been placed as subviews to this scrollview. now when i tap the button, my table view becomes visible but i could'nt dismiss this tableview when tapped outside the tableview(i mean when tapped on the scrollview).
below is my code snippet thru' which i tried to dismiss my tableview,
pls help me out!
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
if (aTouch.tapCount == 1)
{
CGPoint p = [aTouch locationInView:self.scrollview];
if (!CGRectContainsPoint(myTableView.frame, p))
{
myTableView.hidden = YES;
}
}
}
You put this code inside ViewController.m, so that it just active when you tap on self.view.Your scrollView overlay your self.view so that action not active.
Subclass your scrollView and select type in your IB, inside YourScrollView.m, add this code:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
if (aTouch.tapCount == 1)
{
CGPoint p = [aTouch locationInView:self];
for (UIView *aView in self.subviews) {
if (([aView isKindOfClass:[UITableView class]])&&(!CGRectContainsPoint(aView.frame, p)))
{
[aView setHidden:YES];
}
}
}
}

How to hide drop down list when clicked outside in iphone

Programmaticaly how can we close or hide drop down list when clicked outside in iphone?
-(void)RecommendDropDownSelect
{
dropDown=[[DropDownView alloc]initWithArrayData:arr_RecommendName cellHeight:30 heightTableView:100 paddingTop:-100 paddingLeft:10 paddingRight:20 refView:txt_Recommend animation:BLENDIN openAnimationDuration:0.5 closeAnimationDuration:0.5];
dropDown.delegate=self;
[scr_View addSubview:dropDown.view];
[dropDown openAnimation];
btn_RecommendDropDown.enabled=NO;
}
-(void)dropDownCellSelected:(NSInteger)returnIndex
{
btn_RecommendDropDown.enabled=YES;
txt_Recommend.text=[arr_RecommendName objectAtIndex:returnIndex];
}
Besides subclassing UIView and override touchesBegan, using UITapGestureRecognizer seems to be easier if you are using UIViewController
First, setup a tap gesture for your view:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideDropDown)];
[gestureRecognizer setCancelsTouchesInView:NO];
[self.view addGestureRecognizer:gestureRecognizer];
then implement the method for hiding the dropdown:
- (void)hideDropDown
{
if ((dropDown != nil) && (dropDown.view != nil))
{
[drdropDown.view removeFromSuperview];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view==self.view) {
[dropDown removeFromSuperView];
}
}
or
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view==self.view) {
dropDown.alpha=0;
}
}
Try this:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
if(dropDown)
{
[drdropDown.view removeFromSuperView];
}
}

touchesMoved reaching out of my view bounds

I have subclassed UIView and there initially my view will be in a default color and i need to fill some different color on touch (from x axis = 0 to user touched point),here the problem is touchesMoved even if i drag out of my self view bounds it is getting those points,how to restrict it to only for my self view bounds.
I googled & tried below snippets but of no luck
if([self pointInside:point withEvent:nil]){
[self fillColor];
}
My touchesMoved method is as below,
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
endPoint = point;
NSLog(#"moved x: %f,y: %f",point.x,point.y);
if(CGRectContainsPoint([self frame], endPoint)){ // this also not working
[self fillColor];
}
}
Any help is appreciated in advance.
just set tag in viewDidLoad: method and use bellow logic..
fillColorView.tag = 111;
and use bellow logic in touchesMoved: method like bellow..
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *tap = [touches anyObject];
CGPoint pointToMove = [tap locationInView:fillColorView];
if([tap.view isKindOfClass:[UIView class]])
{
UIView *tempView=(UIView *) tap.view;
if (tempView.tag == 111){
[self fillColor];
}
}
}
hope this help you...
In your touchesMoved method, CGPoint point = [touch locationInView:self]; replcae self by the view in which you wants the touch to be worked.
self will get the complete view, you should pass your drawingView at there, so that it will detetc touch only on that view.

Issues in handling touches on subviews(uiviews) in UIScrollView

I have craeted a UIScrollView using code and i have created subviews which i am creating dynamically from the DB that are added to the array of views and to the UIScrollviews.I am using touchesBegan and touchesMoved methods.The subview which i am selecting is not recognized,but the control comes into touchesBegan and touches moved method.I have posted the code below.Please anybody help me to overcome from this issue.
I have created the scrollview through code and i have subviews in an array named "arrayofviews".these subviews are the same views which are in the array.I am able to move these subviews which are from the DB on a normal view,but when i added these views to the scrollview its not working.
I have tried so many solutions which are in the net,but i couldn't get a proper solution.
.m file
- (void)viewDidAppear:(BOOL)animated
{
scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 374)];
scrollview.backgroundColor=[UIColor clearColor];
[scrollview setScrollEnabled:YES];
[scrollview setContentSize:CGSizeMake(300, 600)];
scrollview.showsVerticalScrollIndicator = YES;
scrollview.delaysContentTouches=NO;
.
.
.
.
//here i am retrieving the controls from the DB and adding into the "arrayofviews" array which is an NSMutableArray
//I have added subviews in this part to the scroll like
[scrollview addSubview:vw1];
[scrollview addSubview:vw2];
.
.
.
scrollview.userInteractionEnabled=NO;
scrollview.scrollEnabled=FALSE;
[self.view addSubview:scrollview];
[self.view bringSubviewToFront:scrollview];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
scrollview.userInteractionEnabled=YES;
[self.nextResponder touchesBegan:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation;
touchLocation.x=scrollview.contentOffset.x;
touchLocation.y=scrollview.contentOffset.y;
for(UIView *vw in arrayOfViews)
{
vw.userInteractionEnabled=YES;
if([touch view]==vw)
{
vw.center=touchLocation;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event //method to intiate the touch events
{
[self.nextResponder touchesMoved:touches withEvent:event];
UITouch *touch = [[event touchesForView:scrollview] anyObject];
CGPoint touchLocation1;
touchLocation1.x=scrollview.contentOffset.x;
touchLocation1.y=scrollview.contentOffset.y;
for(UIView *vw in arrayOfViews)
{
if([touch view]==vw)
{
vw.center=touchLocation1; //statement to move the control
}
}
}
change the line
scrollview.userInteractionEnabled=NO;
to
scrollview.userInteractionEnabled=YES;
reason
userInteractionEnabled tells the device, whether to accept touch events. As you disabled the touch on scrollView then how it could recived touches events...
cheers!!!