我遇到了一个问题,即确定如何检测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");
}
答案 0 :(得分:46)
此方法不需要子类化任何东西。您只需向视图添加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()
}
}
}
感谢this answer的想法。
答案 1 :(得分:41)
手势识别器可能对你想要的东西有点过分。您可能只想使用-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;
}
}
此代码应放在您创建的UIView
的自定义子类中。然后使用此自定义视图类型而不是UIView
,您将获得触摸处理。
答案 2 :(得分:6)
在每个UIControl子类(UIButton等)中,您可以使用它来订阅一组特定的UIControlEvent:
addTarget:action:forControlEvents
您应该为UIControlEventTouchDown添加适当的选择器,为UIControlEventTouchUpInside事件添加另一个目标/选择器。
答案 3 :(得分:2)
感谢Holly's answer我构建了一个ButtonView
便利类。
修改:正如answer所说,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")
}
}
答案 4 :(得分:1)
首先,通过您的选择器handleDismissDoubleTap:
,我假设您正在寻找双击以解雇。要实现这一目标,您应该这样做:dismissGestureRecognition.numberOfTapsRequired = 2;
其次,如果按下触摸即表示长时间点击(或“点击并按住”)类型的手势,则应使用UILongPressGestureRecognizer
代替UITapGestureRecognizer
。