Ios 从子视图开始触摸移动
我的superview中有一个子视图。在子视图中,我有另一个子视图,名为circle 这个圆是一个可拖动的圆。我可以通过触摸圆并移动来拖动圆。问题是,如果我触摸子视图上的任何其他位置并移动,则圆将移动。我如何防止这种情况发生 以下代码放置在子视图的类中,用于移动圆Ios 从子视图开始触摸移动,ios,objective-c,Ios,Objective C,我的superview中有一个子视图。在子视图中,我有另一个子视图,名为circle 这个圆是一个可拖动的圆。我可以通过触摸圆并移动来拖动圆。问题是,如果我触摸子视图上的任何其他位置并移动,则圆将移动。我如何防止这种情况发生 以下代码放置在子视图的类中,用于移动圆 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { startPoint = [[touches anyObject] locationInVi
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
startPoint = [[touches anyObject] locationInView:circle];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches){
CGPoint newPoint = [touch locationInView:self];
newPoint.x -= startPoint.x;
newPoint.y -= startPoint.y;
CGRect frm = [circle frame];
frm.origin = newPoint;
[circle setFrame:frm];
}
}
尝试忽略圆圈视图中不存在的触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch1 = [touches anyObject];
if(touch1.view == circle)
{
startPoint = [[touches anyObject] locationInView:self];
}
else
{
startPoint = nil;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (!startPoint) //If touch didnt start on circle view then ignore it
{
return;
}
for (UITouch *touch in touches){
CGPoint newPoint = [touch locationInView:self];
newPoint.x -= startPoint.x;
newPoint.y -= startPoint.y;
CGRect frm = [circle frame];
frm.origin = newPoint;
[circle setFrame:frm];
}
}
据我所知,有三种视图SuperView-->SubView1-->子视图(圆形)。我假设代码是子视图1中的编写器。是吗?是的。代码位于子视图1中。