【发布时间】: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