removing a subview from another subview when tapped outside - iphone

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];
}
}
}
}

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:.

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.

Hide keyboard in ipad

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

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!!!

Different between UIView and UIControl when drag inside UIScrollView

I have a UIScrollView which contains some small UIView subclass. UIScrollView is scroll enabled, and I want each UIView can be dragged inside UIScrollView freely.
My UIView subclass has this method:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] != self) {
return;
}
CGPoint touchPoint = [touch locationInView:self.superview];
originalX = self.center.x;
originalY = self.center.y;
offsetX = originalX - touchPoint.x;
offsetY = originalY - touchPoint.y;
[self.superview bringSubviewToFront:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
CGPoint location = [touch locationInView:self.superview];
CGFloat x = location.x + offsetX;
CGFloat y = location.y + offsetY;
self.center = CGPointMake(x, y);
return;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
self.center = CGPointMake(originalX, originalY);
}
}
I found touchesCancelled:withEvent will be called each time I just drag UIView several pixels. But these codes will work correctly if it is subclass of UIControl.
Why?
Thanks in advance!
UIScrollView tries to determine what kind of interaction the user has in mind. If you tap a view inside a scroll view, that view gets the touch began. If the user then drags, the scroll view decides that the user wants to scroll, so it sends touchesCancelled to the view which first got the event. It then handles the dragging itself.
To enable your own dragging of subviews, you can subclass UIScrollView and override touchesShouldBegin:withEvent:inContentView: and touchesShouldCancelInContentView:.