Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Detect tap down and touch up of a UIView

I am stuck with a problem of determining how to detect a UIView being touched down and UIView being tapped. When it is touched down, I want the UIView to change its background color. When it is touched, I would like the UIView to perform certain tasks. I would like to know how I am able to fix this problem.

-(void)viewDidLoad {             UITapGestureRecognizer *dismissGestureRecognition = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDismissDoubleTap:)];     dismissGestureRecognition.numberOfTapsRequired = 1;     [sectionDismissDoubleView addGestureRecognizer:dismissGestureRecognition];      UITapGestureRecognizer *dismissGestureDownRecognition = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissGestureDownRecognition:)];     dismissGestureRecognition.numberOfTouchesRequired = 1;     [sectionDismissDoubleView addGestureRecognizer:dismissGestureDownRecognition]; }  - (void)handleDismissDoubleTap:(UIGestureRecognizer*)tap {     SettingsDismissDoubleViewController *settingsDouble = [[SettingsDismissDoubleViewController alloc] initWithNibName:@"SettingsDismissDoubleViewController" bundle:nil];     [self.navigationController pushViewController:settingsDouble animated:YES]; }  - (void)dismissGestureDownRecognition:(UIGestureRecognizer*)tap {     NSLog(@"Down"); } 
like image 638
user288231 Avatar asked Oct 26 '13 20:10

user288231


2 Answers

enter image description here

This method does not require subclassing anything. You just add a UILongPressGestureRecognizer to the view and set the minimumPressDuration to zero. Then you check the state when the gesture events are called to see if the touch event is beginning or ending.

Here is the entire project code for the example image above.

import UIKit class ViewController: UIViewController {     @IBOutlet weak var myView: UIView!     override func viewDidLoad() {         super.viewDidLoad()         let tap = UILongPressGestureRecognizer(target: self, action: #selector(tapHandler))         tap.minimumPressDuration = 0         myView.addGestureRecognizer(tap)     }      @objc func tapHandler(gesture: UITapGestureRecognizer) {                  // there are seven possible events which must be handled          if gesture.state == .began {             myView.backgroundColor = UIColor.darkGray             return         }          if gesture.state == .changed {             print("very likely, just that the finger wiggled around while the user was holding down the button. generally, just ignore this")             return         }          if gesture.state == .possible || gesture.state == .recognized {             print("in almost all cases, simply ignore these two, unless you are creating very unusual custom subclasses")             return         }          // the three remaining states are         // .cancelled, .failed, and .ended         // in all three cases, must return to the normal button look:         myView.backgroundColor = UIColor.lightGray     } } 

Thanks to this answer for the idea.

like image 158
Suragch Avatar answered Sep 20 '22 13:09

Suragch


A Gesture Recognizer is probably overkill for what you want. You probably just want to use a combination of -touchesBegan:withEvent: and -touchesEnded:withEvent:.

This is flawed, but it should give you and idea of what you want to do.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {     self.touchDown = YES;     self.backgroundColor = [UIColor redColor]; }  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {     // Triggered when touch is released     if (self.isTouchDown) {         self.backgroundColor = [UIColor whiteColor];         self.touchDown = NO;     } }  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {     // Triggered if touch leaves view     if (self.isTouchDown) {         self.backgroundColor = [UIColor whiteColor];         self.touchDown = NO;     } } 

This code should go in a custom subclass of UIView that you create. Then use this custom view type instead of UIView and you'll get touch handling.

like image 38
Holly Avatar answered Sep 18 '22 13:09

Holly