Button event when end touch - ios

I want to make voice recorder app. The
recording should start when long touch begins and the
recording must end when user stops the gesture on the button.
UIGestureRecognizer *longGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(startrecording)];
How can I handle that when the user leaves the button?

in viewDidLoad method
UILongPressGestureRecognizer *longPressOnButton = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressOnButton:)];
longPressOnButton.delegate = self;
btn.userInteractionEnabled = YES;
[btn addGestureRecognizer:longPressOnButton];
- (void)longPressOnButton:(UILongPressGestureRecognizer*)gesture
{
// When you start touch the button
if (gesture.state == UIGestureRecognizerStateBegan)
{
//start recording
}
// When you stop touch the button
if (gesture.state == UIGestureRecognizerStateEnded)
{
//end recording
}
}
Also you can try or use the touchEvent concept
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)e {
// show touch-began state
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)e {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)e {
UITouch *touch = [touches anyObject];
.....
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)e {
}

As user3182143 said, using a UILongPressGestureRecorgnizer will solve your problem, but if you are interested there is another way using a UIButton. No need to add a UILongPressGestureRecorgnizer!
From your storyboard, drag an IBAction for a UIButton. And while you are adding its name, change the event to Touch Down.
Now drag another IBAction for the same UIButton and while changing its name, change the event to Touch up Inside (if it isn't that already).
- (IBAction)touchDownButtonAction:(UIButton *)sender {
NSLog(#"Start");
}
- (IBAction)touchUpInsideButtonAction:(UIButton *)sender {
NSLog(#"End");
}
Handle your recording based on the actions!
Here is a screenshot just in case:

Related

How to add UITapGesture and touch both to a single UIImageView in objective c?

when i double tap on image then the image will show in complete view and at the same time i have to drag the image from one place to another in the same view.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
Using the above methods the drag and drop of image is implemented successfully but the UITapGesture is not working now. So, how can I implement both ? ``
By just using UILongPressGrstureRecognizer, it is surprising that it is able to implement tap and drag.
As you just drag first Action will be Tap by default.
You have to:
set the numberOfTapsRequired to 1 to detect the initial tap.
set the minimumDuration something smaller to detect drags quicker without waiting
e.g.:
UILongPressGestureRecognizer *mouseDrag = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleDrag:)];
mouseDrag.numberOfTapsRequired=1;
mouseDrag.minimumPressDuration=0.05;
[clickLeft requireGestureRecognizerToFail:mouseDrag];
to handle the drag, you must determine the state to handle it appropriately as a continuous gesture.
For tap gesture you can simply use
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
[self.imageView addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.delegate = self;
While you tap on image this method invoke
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer
{
//Code to handle the gesture
}
Did you try to add the simultaneous recognition?
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
For Single Tap in viewDidLoad method,first set the tapGesture Recognizer
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap)];
singleTap.numberOfTapsRequired = 1;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:singleTap];
Then method
-(void)handleSingleTap
{
NSLog(#"The single tap happened");
}
Touch Events
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (touch.tapCount == 1) {
NSLog(#"The single tap happened now");
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
NSLog(#"The tap count is - %lu",(unsigned long)touch.tapCount);
if (touch.tapCount == 1)
{
NSLog(#"The single tap Ended now");
}
}

Method 'while' touching screen

I'm making a game using Xcode for iOS. This is a segment of code I have that makes the sprite jump up when the screen is tapped:
//tap/touch to jump (& play sound)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event{
[self playSound];
jumpUp = 16;
}
How can I implement it so that the sprite just keeps going up whist you are touching the screen instead of just a single tap?
//Pseudo code:
while touchingScreen {
jumpUp +=1;
}
You need to start some kind of loop in touchesBegan that runs while the touch is lasting. Then in touchesEnded (and cancelled!) make that loop stop. You can use a repeating NSTimer or something like the following. You may need to do some adjustments to make it smooth enough for a game.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
// touching is a BOOL that keeps track of the touch event
// YES means that a touch is happening at the moment
touching = YES;
dispatch_async(dispatch_get_main_queue(), ^{
[self performSelector:#selector(up) withObject:nil afterDelay:.0];
});
}
- (void)up {
// move your sprite further up
NSLog(#"up");
if (touching) {
// if the user is still touching repeat moving the sprite up
[self performSelector:#selector(up) withObject:nil afterDelay:0.1];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
// The finger was lifted, stop the up movement
touching = NO;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
touching = NO;
}

IOS detect when user stops touching UIImageView

Any idea how to do this? I have a UIImageView inside of each cell in my UITableView and I want to disable scroll when the user starts touching the UIImageView and then enable it once the user stops dragging their finger on the photo.
I just give my logic.
Add UIPanGestureRecognizer to each cell of UITableView.
UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanFrom:)];
[cell addGestureRecognizer:panGestureRecognizer];
And in method name is handlePanFrom:
- (void)handlePanFrom:(UIPanGestureRecognizer*)recognizer
{
CGPoint translation = [recognizer translationInView:recognizer.view];
CGPoint velocity = [recognizer velocityInView:recognizer.view];
if (recognizer.state == UIGestureRecognizerStateBegan)
{
/// track began
tableView.userInteractionEnabled = NO;
}
else if (recognizer.state == UIGestureRecognizerStateChanged)
{
// track the movement
} else if (recognizer.state == UIGestureRecognizerStateEnded)
{
// final position
tableView.userInteractionEnabled = YES;
}
}
Make sure your UIIMageView must be set as a userInteractionEnabled = YES;. Because by default UIIMageView have to set userInteractionEnabled = NO;.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch.view isKindOfClass:[UIImageView class]]) {
NSLog(#"self.tableView.scrollEnabled = NO");
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch.view isKindOfClass:[UIImageView class]]) {
NSLog(#"self.tableView.scrollEnabled = YES");
}
}
There are two ways to implement this, either include UITapGestureRecognizer or UIPanGestureRecognizer to your UIImageView and set the target and action where target is your custom UITableViewCell class or include
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
methods in the UIImageView custom class and set the delegate via protocol to your custom UITableViewCell.
for UIGestureRecognizer you can check its state property UIGestureRecognizer Class Reference to know the state with the help of switch
typedef enum {
UIGestureRecognizerStatePossible,
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
UIGestureRecognizerStateCancelled,
UIGestureRecognizerStateFailed,
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
}
Finally, by toggling the UITableView's scrollEnabled property to stop and start scrolling where UITableView is a sub class of UIScrollView

Detect a touch on a certain object in Xcode

What's the Xcode code for this pseudocode?
if ([Any UIPickerView in my ViewController isTouched]) {
[AnyUIView setHidden:NO];
}
if ([Any UIPickerView in my ViewController is__NOT__Touched__Anymore__]) {
[AnyUIView setHidden:YES];
}
Tried it with the -(void)touchesBeganmethod, it detects the touches but I was not able to make it object-specific. Thanks
P.S. I want to display a hint on the display while the UIPickerViewis touched.
This is just from the top of my head...... but you should be able to get the idea...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
if ([touch.view isKindOfClass: UIPickerView.class]) {
//your touch was in a uipickerview ... do whatever you have to do
}
}
..and do the same with touchesEnded:withEvent:

Detecting Tap & Hold in UITableView cells

How do we detect a tap & hold on a UITableViewCell?
In iOS 3.2 or later you can use UILongPressGestureRecognizer
Here's the code lifted straight from my app. You should add these methods (and a boolean _cancelTouches member) to a class you derive from UITableViewCell.
-(void) tapNHoldFired {
self->_cancelTouches = YES;
// DO WHATEVER YOU LIKE HERE!!!
}
-(void) cancelTapNHold {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(tapNHoldFired) object:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self->_cancelTouches = NO;
[super touchesBegan:touches withEvent:event];
[self performSelector:#selector(tapNHoldFired) withObject:nil afterDelay:.7];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self cancelTapNHold];
if (self->_cancelTouches)
return;
[super touchesEnded:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self cancelTapNHold];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self cancelTapNHold];
[super touchesCancelled:touches withEvent:event];
}
//Add gesture to a method where the view is being created. In this example long tap is added to tile (a subclass of UIView):
// Add long tap for the main tiles
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTap:)];
[tile addGestureRecognizer:longPressGesture];
[longPressGesture release];
-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer{
NSLog(#"gestureRecognizer= %#",gestureRecognizer);
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
NSLog(#"longTap began");
}
}
You should probably handle the UIControlTouchDown event and depending on what you mean by "hold", fire a NSTimer that will count an interval since you initiated the touch and invalidate upon firing or releasing the touch (UIControlTouchUpInside and UIControlTouchUpOutside events). When the timer fires, you have your "tap & hold" detected.

Resources