gpt4 book ai didi

ios - 如何在 iOS 中手动触发 touchesBegan 值?

转载 作者:行者123 更新时间:2023-11-28 18:57:59 25 4
gpt4 key购买 nike

我有一个父 View Controller ,还有一个 subview (UIView)。

subview (名为xibSubView)调用其他自定义类(xib)文件。

当我点击父 View Controller 时, subview (UIView) 将移动到触摸父 View Controller 上的开始位置。

父 View Controller 有

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

下面的代码在父viewcontroller.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)代码有相同的委托(delegate)方法。

 // 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 中有很多元素。

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

所以我想将父 View Controller 的触摸传递给 xibSubView。

然后手动调用 xibSubView 中的 touchBegan 让元素运动。

所以我应该将父 View Controller 触摸位置转换为 subview 触摸位置。

subview 触及x位置和y位置会减去viewcontroller的宽高,手动调用touchbegan方法。

但是我不知道怎么把touches的x,y值改成touches。

然后调用代码:

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

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

非常感谢。

最佳答案

NSMutableArray *touchArray = [NSMutableArray new];
[touchArray addObject:[UITouch new]];
NSSet *touches = [[NSSet alloc] init];
[touches setByAddingObjectsFromArray:touchArray];
[self touchesBegan:touches withEvent:UIEventTypeTouches];

swift

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

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

关于ios - 如何在 iOS 中手动触发 touchesBegan 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30095789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com