iOS检测点击并触摸一个UIView

我坚持一个问题,确定如何检测UIView被触及和UIView被点击。 当它被触及时,我想让UIView改变它的背景颜色。 当它被触摸时,我想UIView执行某些任务。 我想知道我能够解决这个问题。

-(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"); } 

一个手势识别器可能是为了你想要的矫枉过正。 您可能只想使用-touchesBegan:withEvent:-touchesEnded:withEvent:

这是有缺陷的,但它应该给你和你想做什么的想法。

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

这段代码应该在您创build的UIView的自定义子类中。 然后使用这个自定义视图types,而不是UIView ,你会得到触摸处理。

在这里输入图像说明

这个方法不需要inheritance任何东西。 您只需将UILongPressGestureRecognizer添加到视图,并将minimumPressDuration设置为零。 然后,检查手势事件被调用时的状态,以查看触摸事件是开始还是结束。

这是上面的示例图像的整个项目代码。

 import UIKit class ViewController: UIViewController { @IBOutlet weak var myView: UIView! override func viewDidLoad() { super.viewDidLoad() // Add "long" press gesture recognizer let tap = UILongPressGestureRecognizer(target: self, action: #selector(tapHandler)) tap.minimumPressDuration = 0 myView.addGestureRecognizer(tap) } // called by gesture recognizer func tapHandler(gesture: UITapGestureRecognizer) { // handle touch down and touch up events separately if gesture.state == .Began { myView.backgroundColor = UIColor.darkGrayColor() } else if gesture.state == .Ended { myView.backgroundColor = UIColor.lightGrayColor() } } } 

感谢这个想法的答案 。

在每个UIControl子类(UIButton等)中,您可以使用它来订阅一组特定的UIControlEvent:

 addTarget:action:forControlEvents 

您应该为UIControlEventTouchDown添加适当的select器,并为UIControlEventTouchUpInside事件添加另一个目标/select器。

UIControl参考

首先,通过你的select器handleDismissDoubleTap:我假设你正在寻找一个双击解散。 要做到这一点,你应该这样做: dismissGestureRecognition.numberOfTapsRequired = 2;

其次,如果通过触摸你的意思是长时间的敲击(或“轻敲和按住”)手势,你应该使用UILongPressGestureRecognizer而不是UITapGestureRecognizer

感谢Holly的回答,我build立了一个ButtonView便利课程。

编辑:正如这个答案所说, UILongPressGestureRecognizer反应比较快,所以我更新了我的课程。

用法:

 let btn = ButtonView() btn.onNormal = { btn.backgroundColor = .clearColor() } btn.onPressed = { btn.backgroundColor = .blueColor() } btn.onReleased = yourAction // Function to be called 

类:

 /** View that can be pressed like a button */ import UIKit class ButtonView : UIView { /* Called when the view goes to normal state (set desired appearance) */ var onNormal = {} /* Called when the view goes to pressed state (set desired appearance) */ var onPressed = {} /* Called when the view is released (perform desired action) */ var onReleased = {} override init(frame: CGRect) { super.init(frame: frame) let recognizer = UILongPressGestureRecognizer(target: self, action: Selector("touched:")) recognizer.delegate = self recognizer.minimumPressDuration = 0.0 addGestureRecognizer(recognizer) userInteractionEnabled = true onNormal() } func touched(sender: UILongPressGestureRecognizer) { if sender.state == .Began { onPressed(self) } else if sender.state == .Ended { onNormal(self) onReleased() } } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }