Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIView上的触摸事件_Ios_Objective C_Iphone_Uikit - Fatal编程技术网

Ios UIView上的触摸事件

Ios UIView上的触摸事件,ios,objective-c,iphone,uikit,Ios,Objective C,Iphone,Uikit,是否有人拥有用于检测动态创建的UIView上的触摸的示例代码?我找到了一些对touchesbreated的引用,但不知道如何实现它…从通过继承UIView创建的子类创建您自己的自定义UIView,并在子类中重写以下方法 (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ } 获取接触的一种非常通用的方法是在自定义UIView子类中重写这些方法: - (void)touchesBegan:(NSSet *)touche

是否有人拥有用于检测动态创建的UIView上的触摸的示例代码?我找到了一些对touchesbreated的引用,但不知道如何实现它…

从通过继承
UIView
创建的子类创建您自己的自定义
UIView
,并在子类中重写以下方法

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

}

获取接触的一种非常通用的方法是在自定义UIView子类中重写这些方法:

- (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
这些方法的官方文档正在响应中的触摸事件(UIView继承了这些方法,因为它是UIResponder的子类)。在中可以找到更长、更多的介绍性文档

如果您只想检测点击(在视图中触碰,然后触碰),最简单的方法是添加
ui按钮作为视图的子视图,并添加您自己的自定义方法作为该按钮的目标/操作对。默认情况下,自定义按钮不可见,因此不会影响视图的外观


如果您正在寻找更高级的交互,了解这一点也很好。

在UIView上使用UIAnimation创建触摸事件,您可以在任何地方管理UIView上的触摸

下面的代码:这里self.finalScore是一个UIView,cup是一个UIImageView。我处理

UIImageView上的触摸事件,它出现在UIView中

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch1 = [touches anyObject];
CGPoint touchLocation = [touch1 locationInView:self.finalScore];
CGRect startRect = [[[cup layer] presentationLayer] frame];
CGRectContainsPoint(startRect, touchLocation);

[UIView animateWithDuration:0.7 
                      delay:0.0 
                    options:UIViewAnimationCurveEaseOut 
                 animations:^{cup.transform = CGAffineTransformMakeScale(1.25, 0.75);} 
                 completion:^(BOOL finished) {  
                     [UIView animateWithDuration:2.0 
                                           delay:2.0 
                                         options:0
                                      animations:^{cup.alpha = 0.0;} 
                                      completion:^(BOOL finished) {
                                          [cup removeFromSuperview];
                                          cup = nil;}];                 
                 }];

}
与UIAPTgestureRecognitor类似,UIView上处理触摸事件的另一种方法

UITapGestureRecognizer *touchOnView = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(releaseButAction)] autorelease];

// Set required taps and number of touches
[touchOnView setNumberOfTapsRequired:1];
[touchOnView setNumberOfTouchesRequired:1];

// Add the gesture to the view
[[self view] addGestureRecognizer:touchOnView];

我建议将UIView父类更改为UIControl,以便它可以处理touchUpInside事件,而不是使用手势捕捉触摸

发件人:

致:

然后添加此行以供查看-

    [self addTarget:self action:@selector(viewTapped:) forControlEvents:UIControlEventTouchUpInside]; 

因为手势可能会在滚动时产生问题。

我的方法是在InterfaceBuilder IdentityInspector中将
UIView
更改为
UIControl
类,然后ConnectionsInspector会立即将触摸事件准备好挂接


零代码更改。

使用手势识别器,很容易捕捉触摸事件

从情节提要中的组件库中查找一个来宾:

例如,将轻触手势识别器拖动到视图中。它不会显示在视图UI中。您可以从序列图像板左侧面板中的文档大纲中找到它:

最后一步是将其链接(通过控制拖动)到监视触摸的代理视图,并将控制拖动作为操作链接到视图控制器类。下面是链接连接的图片

行动守则:

@IBAction func tapAction(_ sender: Any) {
    // touch is captured here.
}

截取的第二个代码对我有帮助,但更好的做法是
UITapGestureRecognizer*touchOnView=[[UITapGestureRecognizer alloc]initWithTarget:self-action:@selector(releaseButAction:)]autorelease]-(void)releaseButAction:(uigesturecognizer*)gesturecognizer
。。仅供参考!
    [self addTarget:self action:@selector(viewTapped:) forControlEvents:UIControlEventTouchUpInside]; 
@IBAction func tapAction(_ sender: Any) {
    // touch is captured here.
}