Objective C: Stop Scrolling on Touch - objective-c

I am trying to temporarily stop the scrolling of my view when the user starts to touch the screen and move.
I have coded this
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (touch){
[scrollView setScrollEnabled:FALSE];
}
}
it is not working.

If you have a ScrollView and the User touches it its Defaults beheviour that it stops to scroll.
If this is not whatyou ment please be more specific what you want ;)

try this:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[scrollView setScrollEnabled:FALSE];
}
now i just wonder where you enable it again...

Related

Detect user interaction without delegates

Is it possible to detect user interaction (like swiping a scrollview or start editing a textfield) without using delegates or notifications? Just using the most basic touch events possible.
I've been trying to use touchesBegan and touchesEnded but these are not always called.
Yes you can just connect an IBAction to your UITextField Sent Events - Editing Changed, so every time the text field changes your IBAction will be called. You can do the same for Editing Did Begin and Editing Did End.
https://stackoverflow.com/a/29045440/2303865
You can identify the touched object and perform actions you required.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
if ([touch.view isKindOfClass: UITextField.class])
{
if (![(UITextField *)touch.view isFirstResponder])
{
//start editing
[(UITextField *)touch.view becomeFirstResponder];
}
}
else
{
}
}

UISlider change values by touching its track

I am beginner in ios and I have done basic functionality with UISlider , changing its value on dragging thumb. But now in my app I want to move the thumb of slider gradually in same direction where I am touching slider's track without dragging the thumb of slider. Thumb must reach to exact touched position on the track in 2 or more steps.
I know it requires involvement of UITouch but unable to understand HOW?
You need to subclass UISlider to add this functionality. After subclassing, you need to implement touchesBegan and touchedMoved in your subclass. Well, here how i did
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGFloat maxX = CGRectGetMaxX(self.frame);
CGFloat minX = CGRectGetMinX(self.frame);
int value = (maxX - minX) /(self.maximumValue - self.minimumValue);
[self setValue:([touch locationInView:self].x - minX) / value animated:YES];
[self beginTrackingWithTouch:touch withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self continueTrackingWithTouch:[touches anyObject] withEvent:event];
}
You need more work to stabilise a little bit more when user first tap, but i hope you get the idea.

Dragging multiple images in iOS

I'm brand new to touch and drag, and I'm trying to make eight draggable images. Here's what it looks like in my ViewController.m file:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
//check for which peg is touched
if ([touch view] == darkGreyPeg){darkGreyPeg.center = location;}
else if ([touch view] == brownPeg){brownPeg.center = location;}
//there are six more else ifs
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self ifCollided];
}
If I take out the if statements, and the locationInView:self.view to locationInView:touch.view, I'm able to drag the darkGreyPeg around with no problems, and it triggers my [ifCollided] whenever appropriate.
I'm watching a youTube tutorial (Milmers Xcode tutorials, "Dragging Multiple Images"), and he has exactly this same type of code, and mine doesn't work. Can anyone point me in the direction of why?
Thanks.
Try to check the User interaction Enabled & multiple touch in your view images atribute. Without check the user interaction enabled you can't use multiple dragging. I have try this, and succes to drag multiple image, label or button.
You should not implement these methods in your ViewController, but in your View class that you want to be able to drag around instead.
I think the problem is using == to compare objects:
[touch view] == darkGreyPeg
You should use isEqual: to compare objects:
[[touch view] isEqual:darkGrayPeg]
After Edit: I think the problem is that you forgot to set userInteractionEnabled to YES for the image views. Without doing that, the touch.view will be the superview, not the image view. Also, you probably don't need all those if statements to determine which image view to move, you can just use touch.view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if (![touch.view isEqual:self.view]) touch.view.center = location;
}
If you have other views, besides self.view that you don't want to move, then you either have to exclude them too, or use a different exclusion condition (like only move the object if it's an image view, or only an object with a certain tag value).

resign keyboard when losing focus on uisearchbar

I'm making an UISearchBar option in the navigationbar of my app.
My app consists of multiple views and subviews.
I have this mainview which has 3 other views on himself. one of it is empty (for now) the other 2 have tableviews on them.
I want my keyboard to show when I'm searching and hide when i'm doing the actual search or when i touch/click outside the uisearchbar.
Im using the searchbardelegate as is required.
Im able to hide the keyboard using [searchBar resignFirstResponder] in the following ways.
When im pressing the return key.
When i cancel search manually
When i press any of the keyboard buttons to search or cancel.
When i touch an empty part of the screen using
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if ([mySearchBar isFirstResponder] && [touch view] != mySearchBar) {
[mySearchBar resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
What i not seem to be able to do is make it respond to touching one of my 2 tableviews. Or when im refilling the mainview to contain something different entirely.
Ive tried changing the touchesbegan method to resign the searchbar when touching the tableviews but it hasnt worked so far.
I've tried several other things found by my dear friend mr. google, but it all seems to be something other then I need.
Anyone have any ideas of what I might do to fix this problem?
EDIT:
It appears so that, when using breakpoints, touchesbegan-method does respond to the backgroundview but it doesnt respond when i touch either of the tableviews or the navigationbar (containing the uisearchbar).
Solved it!
- (BOOL) searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
[self.myViewController1.customView1 setUserInteractionEnabled:NO];
[self.myViewController2.customView2 setUserInteractionEnabled:NO];
[searchBar setShowsCancelButton:YES animated:[mySettings animation]];
return YES;
}
I started by shutting down the userInteraction on my 2 subviews, at the moment I start using the searchBar.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if ([self.mySearchBar isFirstResponder] && [touch view] != self.mySearchBar)
{
[self.mySearchBar resignFirstResponder];
[self.myViewController1.customView1 setUserInteractionEnabled:YES];
[self.myViewController2.customView2 setUserInteractionEnabled:YES];
}
[super touchesBegan:touches withEvent:event];
}
Then when I click/tap/touch outside the searchBar I first resign the keyboard which is first responder still AND AFTER that I set userInteraction back on for the 2 subviews.
The order of doing this is vital!
This piece of code allows u to resign the keyboard in a single mainViewController even when it is crowded with a massload of subviews.
Have you tried this,
UISearchBar *searchBar;
Next set the Getter and Setter Property of UISearchBar.
and call the any method
[searchBar resignFirstResponder];

touches in touch event handling in UIView vs UIViewControllers

So, I'm capturing multiple touches and determining the number and where each of the touches occurred and I'm seeing different behavior between a UIView UIViewController.
Basically, the event handler is coded below: (in this case the touchesEnded, but it really doesn't matter). What happens in the View, is that I get the entire collection of touches, but in the controller, I get only one touch at a time, that is there is never a collection of touches, but the TouchesEnded gets executed for each touch that occurred in the same moment.
I add the subviews as identically as possible...
Can someone 'spain to me what's going on? Why doesn't the controller version give me the collection?
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
int total = 0;
for (UITouch *touch in touches)
{
for(UIView *myView in [contentView subviews])
{
if (CGRectContainsPoint([myView frame], [touch locationInView:self.view]))
total++; // this is always 1 for the UIViewController
// but I get the full number within the UIView version
}
}
}
I suspect you are getting the touches but that the views are different. Check that contentView is the same and has the same subviews.
I am not sure that I am right but this thing always work for me.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
If(touch.view==your view)
{
NSLog("do your task here");
}
}