iOS检测点击并修改UIView

use*_*231 34 ios uitapgesturerecognizer

我遇到了一个问题,即确定如何检测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");
}
Run Code Online (Sandbox Code Playgroud)

Sur*_*gch 48

在此输入图像描述

此方法不需要子类化任何东西.您只需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()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

感谢这个想法的答案.

  • 这将阻止父滚动视图滚动 (6认同)

Hol*_*lly 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码应该放在UIView您创建的自定义子类中.然后使用此自定义视图类型而不是,UIView您将获得触摸处理.


ope*_*dge 6

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

addTarget:action:forControlEvents
Run Code Online (Sandbox Code Playgroud)

您应该为UIControlEventTouchDown添加适当的选择器,为UIControlEventTouchUpInside事件添加另一个目标/选择器.

UIControl参考

  • 问题是关于uiview而不是uicontrol (6认同)