【问题标题】:How to manual trigger the touchesBegan value in iOS?如何在 iOS 中手动触发 touchesBegan 值?
【发布时间】:2017-07-28 14:21:18
【问题描述】:

我有一个父视图控制器,并且有一个子视图(UIView)。

子视图(名为xibSubView)被称为其他自定义类(xib)文件。

当我点击父视图控制器时,子视图(UIView)将移动到父视图控制器上的开始位置。

父viewcontroller有

 // UIViewcontroller.h file
  .....    
 @property (weak, nonatomic) IBOutlet XibSubView *xibSubView;
 .....

以下代码在父视图控制器.m中

 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
    self.subViewConstraint.constant = touchLeftScreenPoint.x ;
    self.subViewConstraint.constant = touchLeftScreenPoint.y ;

 }

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

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

和xib(subview-xibSubView)代码有相同的委托方法。

 // The code is in the parent viewcontroller .m
 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
 }

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

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

在xibSubView中有很多元素。

其中一个元素将移动到子视图(xibSubView)中的点击位置。

所以我想将父视图控制器的触摸传递给 xibSubView。

并在xibSubView中手动调用touchBegan让元素运动起来。

所以我应该将父视图控制器的触摸位置转换为子视图的触摸位置。

子视图触摸x位置和y位置会减去viewcontroller的宽高,手动调用touchbegan方法。

但是我不知道怎么改变touches x, y值恢复到touches。

然后调用代码:

 [self.xibSubView touchesBegan:touches withEvent:event];

如何更改 NSSet touches x,y 变量并恢复为 touches。

非常感谢。

【问题讨论】:

  • 做到这一点很难。重定向事件时,通常是从子视图到父视图,而不是相反。不过,这似乎更像是一个架构问题。通常您只希望一个视图来处理触摸,如果您想将事件传递给子视图,请使用自定义方法(例如,仅传递位置)。这将使问题变得更容易。
  • 实际上,子视图就像操纵杆一样,因此可以从用户单击子视图中移动自己。并且我会点击父viewcontroller,摇杆会改变自己的view到touch position,并且可以立即移动。
  • 这不会改变我的评论。

标签: ios objective-c iphone uiview


【解决方案1】:
NSMutableArray *touchArray = [NSMutableArray new];
[touchArray addObject:[UITouch new]];
NSSet *touches = [[NSSet alloc] init];
[touches setByAddingObjectsFromArray:touchArray];
[self touchesBegan:touches withEvent:UIEventTypeTouches];

斯威夫特

let tArr = NSMutableArray()
tArr.add(UITouch())
let touch = NSSet()
touch.adding(tArr)

touchesBegan(touch as! Set<UITouch>, with: nil)

【讨论】:

  • 我必须转换坐标。父视图控制器中的触摸 x,y 与子视图不同。那么如何转换为 touches 值并恢复到 NSSet,然后传递给 touchesBegan。
  • 无法以编程方式创建 uitouch 对象。
  • 我知道我们不能以编程方式创建一个 uitouch 对象,所以我想从父视图控制器上的用户触摸中获取父视图控制器的触摸值,然后获取触摸值以减去一些值,传递到xibsubview touches Began.So如何更改touches值并恢复?谢谢。
  • xibSubView 扩展了 UIView,它是 xib 文件。当我触摸子视图时,它被称为关于触摸开始。@@
  • 假设触摸 x 位置是减去父视图控制器 x 位置。如何更改触摸 x 值传递到子视图 touchbegan。我知道如何调用 subview touchbegan,但我不知道如何更改 touches CGpoint 值。
【解决方案2】:

ViewController.m

#import "ViewController.h"
#import "view.h"

@interface ViewController ()
{
 view *v;
}
@end

@implementation ViewController
- (void)viewDidLoad {
   v=[[view alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
   [self.view addSubview:v];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   [v touchesBegan:touches withEvent:event];
}
@end

view.h

#import <UIKit/UIKit.h>

@interface view : UIView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
@end

查看.m

#import "view.h"

@implementation view

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  NSLog(@"%@",touches);
}
@end

【讨论】:

  • 这将触发 UIViewController 到 UIView 的触摸动作。
  • 或者你可以反过来,在 UIView 中触摸到 UIViewController
  • 我的视图坐标与父视图控制器不同,因此在您的情况下,ViewController 的 touchbegan 方法 get touches 变量将转换 x,y 坐标(如 substract touchesPoint.x ),然后调用 [v touchesBegan:touches withEvent:事件];问题出在我不知道何时转换某些值,如何保存到 touches 变量,然后调用 [v touchesBegan:touches withEvent:event];谢谢
  • 不要转换成uitouch,参考上面的链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多